我正在使用jQuery回发值并将其成功发回服务器我使用Mozilla FireBug进行了检查。我在.CS文件中的插入查询中使用这些值来在表中插入数据。相同的查询在SQL Server Management Studio中成功运行,但是当我在.CS文件中使用此查询时,它没有运行。
这是我的代码:
public static bool SaveCell(string row, string column)
{
var con = new SqlConnection("Data Source=local;Initial Catalog=Test;Integrated Security=True");
using (con)
using (var command = new SqlCommand("Insert into Match_Subcategory_BusinessSector5(SubCategoryID, BusinessSector5ID)"+
"Values("+
"(Select [SubCategory].ID from SubCategory where Kategorie = '@SubCategory')," +
"(SELECT [BusinessSector5].ID FROM BusinessSector5 where Description_DE = '@BusinessSector5'));",con))
{
command.Parameters.AddWithValue("@BusinessSector5", row);
command.Parameters.AddWithValue("@SubCategory", column);
con.Open();
command.ExecuteNonQuery();
}
return true;
}
我收到此错误:
无法将值NULL插入到SubCategoryID列Test.dbo.Match_Subcategory_BusinessSector5表中。该列不允许空值。
答案 0 :(得分:1)
Chnage
'@SubCategory'
到
@SubCategory
和
'@BusinessSector5'
到
@BusinessSector5
使用参数化查询时,您不需要在参数名称周围添加任何内容,它不会在您的代码中合并,而是单独发送到服务器(它在您编写时发送sql和参数列表) 。因此,您可以保护sql注入和相关问题。
答案 1 :(得分:0)
"(Select [SubCategory].ID from SubCategory where Kategorie = @SubCategory)," +
"(SELECT [BusinessSector5].ID FROM BusinessSector5 where Description_DE = @BusinessSector5));",con))
从查询中删除引号