附加信息:从对象类型system.windows.forms.textbox到已知的托管提供程序本机类型不存在映射。 [英] Additional information: no mapping exists from object type system.windows.forms.textbox to a known managed provider native type.
问题描述
我正在尝试使用参数化查询来更改(更新)表格中的信息..
I am trying to use a paramtized query to change (update) information in my table..
SqlCommand cmd = new SqlCommand("UPDATE [Product Inventory] Quantity = @Quantity", sqlcon);
cmd.Parameters.AddWithValue("@Quantity", QuantityTxtBx);
sqlcon.Open(); //open the connection
// MessageBox.Show("Adjustment has been registered");
cmd.ExecuteNonQuery();
sqlcon.Close(); // close the connection
我尝试过:
What I have tried:
SqlCommand cmd = new SqlCommand("UPDATE [Product Inventory] Quantity = @Quantity", sqlcon);
cmd.Parameters.AddWithValue("@Quantity", QuantityTxtBx);
sqlcon.Open(); //open the connection
// MessageBox.Show("Adjustment has been registered");
cmd.ExecuteNonQuery();
sqlcon.Close(); // close the connection
推荐答案
如果数量为int:
If quantity is int:
int q;
if (int.TryParse(QuantityTxtBx.Text, out q)) {
cmd.Parameters.AddWithValue("@Quantity", q);
}
else {
// Parsing error
}
永远不要相信用户输入,在使用之前总是验证它们。
此外,文本框是一个复杂的对象,它永远不能直接转录成整数或任何其他整数类型。在控件的 Text
属性上使用 TryParse
方法从中获取有效值。
Never trust user inputs, always validate them before using them.
Besides, a textbox is a complex object, which is never directly transcriptible into an integer or any other integral type. Use TryParse
method on the Text
property of the control to get a valid value out of it.
< br>
cmd.Parameters.AddWithValue("@Quantity", QuantityTxtBx);
您正在尝试将 TextBox
控件作为参数传递。您需要传递 TextBox
的内容:
You're trying to pass the TextBox
control as a parameter. You need to pass the contents of the TextBox
instead:
cmd.Parameters.AddWithValue("@Quantity", QuantityTxtBx.Text);
(请注意控件名称后的 .Text
。)
这篇关于附加信息:从对象类型system.windows.forms.textbox到已知的托管提供程序本机类型不存在映射。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!