| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 782 人关注过本帖
标题:求助
只看楼主 加入收藏
天煞小新
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-7-12
收藏
 问题点数:0 回复次数:6 
求助
//声明字符串用来接收表中输入信息
                   string pub_id;
                string sup_id;

                string book_id = txtBookID.Text.Trim();

                string book_name = txtBookName.Text.Trim();
               
                string price;
                if (txtCostPrice.Text.Trim() != "")
                    price = txtCostPrice.Text.Trim();
                else
                    price = null;
               
                string quan;
                if (txtQuantity.Text.Trim() != "")
                    quan = txtQuantity.Text.Trim();
                else
                    quan = null;

                string sale_price;
                if (txtSalePrice.Text.Trim() != "")
                    sale_price = txtSalePrice.Text.Trim();
                else
                    sale_price = null;

                string otherInfo;
                if (richtxtInfo.Text.Trim() != "")
                    otherInfo = richtxtInfo.Text.Trim();
                otherInfo = null;

                try
                {
                    //根据type_name获得type_id
                    string selectString1 = "select type_id from bookTypes where type_name = @type_name";
                     = selectString1;
                    sc.Parameters.Add(new SqlParameter("@type_name", cbbBookType.SelectedItem.ToString()));
                    string type_id = sc.ExecuteScalar().ToString();

                    //根据pub_name获得pub_id
                    if (cbbPubisher.SelectedItem != null)
                    {
                        string selectString2 = "select pub_id from publishers where pub_name = @pub_name";
                         = selectString2;
                        sc.Parameters.Add(new SqlParameter("@pub_name", cbbPubisher.SelectedItem.ToString()));
                        pub_id = sc.ExecuteScalar().ToString();
                    }
                    else
                        pub_id = null;

                    //根据sup_name获得sup_id
                    if (cbbSupplier.SelectedItem != null)
                    {
                        string selectString3 = "select sup_id from suppliers where sup_name = @sup_name";
                         = selectString3;
                        sc.Parameters.Add(new SqlParameter("@sup_name", cbbSupplier.SelectedItem.ToString()));
                        sup_id = sc.ExecuteScalar().ToString();
                    }
                    else
                        sup_id = null;                    

                    //执行插入语句
                        string insertString = "insert books (book_id,book_name,type_id,sup_id,sup_date,price,notes,unitsInStock,sale_price,pub_id,stock) values(@book_id,@book_name,@type_id,@sup_id,@sup_date,@price,@notes,@unitsInStock,@sale_price,@pub_id,@stock)";
                     = insertString;
                    sc.Parameters.Add(new SqlParameter("@book_id", book_id));
                    sc.Parameters.Add(new SqlParameter("@book_name", book_name));
                    sc.Parameters.Add(new SqlParameter("@type_id", type_id));
                    sc.Parameters.Add(new SqlParameter("@sup_id", sup_id));
                    sc.Parameters.Add(new SqlParameter("@sup_date", dtpickerStockTime.Value.Date));
                    sc.Parameters.Add(new SqlParameter("@price", price));
                    sc.Parameters.Add(new SqlParameter("@notes",otherInfo));
                    sc.Parameters.Add(new SqlParameter("@unitsInStock", quan));
                    sc.Parameters.Add(new SqlParameter("@sale_price", sale_price));
                    sc.Parameters.Add(new SqlParameter("@pub_id", pub_id));
                    sc.Parameters.Add(new SqlParameter("@stock", quan));
                    int i = sc.ExecuteNonQuery();

                    dr = MessageBox.Show("已向数据库中成功插入该图书信息!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);


向bookStore中的books表中插入图书信息
其中表books中的book_id,book_name,type_id,unitsInStore,sale_price字段可以为null
为什么当输入price的文本框中没有输入任何信息时,向数据库中插入信息不成功,提示“被准备语句'(@type_name nvchar(2),@pub_name nvchar(2),@sup_name nvchar'需要参数@price,但未提供该参数”
搜索更多相关主题的帖子: 字符串 price null 信息 
2007-12-31 09:13
天煞小新
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-7-12
收藏
得分:0 
哪个大大帮忙看看撒
2007-12-31 09:58
xf12839356
Rank: 1
等 级:新手上路
帖 子:89
专家分:0
注 册:2007-11-6
收藏
得分:0 
看是看了啊`
 但是 不怎么懂啊`
2008-01-04 11:10
doughty
Rank: 1
等 级:新手上路
帖 子:372
专家分:0
注 册:2007-10-18
收藏
得分:0 
你写数据库中的字段里参数和你给的的参数不一致....你好好看看..
你这样会不会太烦了,,用个存储过程写不更好些,,,
2008-01-04 11:17
tonybai
Rank: 1
来 自:陕西
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-7-21
收藏
得分:0 
学习,帮LZ顶一下
2008-01-04 14:25
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 
sc.parameters[N].value=......

public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2008-01-04 22:42
freele_china
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:JiNan
等 级:版主
威 望:6
帖 子:352
专家分:0
注 册:2007-4-6
收藏
得分:0 
try{}
catch{}

Insert 的时候,数值型的不能为空值,你可以试着默人Price为0

雁过留声,人过留名
2008-01-05 09:32
快速回复:求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017188 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved