求助
//声明字符串用来接收表中输入信息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,但未提供该参数”