我想将组合框中的选定项索引插入SQL Server中的tinyint
列(FoodType
)。我写了下面的代码,但是我得到了一个例外:
必须声明标量变量@fType
我该怎么办?
string query = "INSERT INTO MenuTbl (FoodName,FoodType,FoodUnit, SalePrice)" +
"VALUES (@fName, @fType, @fUnit, @fPrice)";
connection = new SqlConnection(conectionString);
SqlCommand cmd = new SqlCommand(query,connection);
cmd.Parameters.AddWithValue("@fName", FNameTextBox.Text.Trim());
cmd.Parameters.AddWithValue("@fTyp", TypeComboBox.SelectedIndex + 1);
cmd.Parameters.AddWithValue("@funit", UnitComboBox.SelectedIndex +1);
cmd.Parameters.AddWithValue("@fprice", int.Parse(PriceEdit.Text));
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
答案 0 :(得分:1)
我认为你用这行代码打了一个错字
string query = "Insert into MenuTbl (FoodName,FoodType,FoodUnit, SalePrice)" +
"VALUES (@fName,@fType,@fUnit, @fPrice)";
根据你的选择查询
,它至少应该是@fTypeTypeComboBox.SelectedIndex + 1
我还认为cmd.Parameters.AddWithValue("@fTyp", TypeComboBox.SelectedIndex + 1);
只会给你索引+ 1数值而不是所选的文本内容。这就是你想要的吗?
答案 1 :(得分:1)
你在参数上错过了“e”吗?
i