簡體   English   中英

將變量中的值傳遞給SQL Server數據庫的C#,ASP.NET中的插入查詢

[英]Passing values from variables to Insert query in C#, ASP.NET for SQL Server database

我正在使用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表中。 該列不允許為空。

通道數

'@SubCategory'

@SubCategory

'@BusinessSector5'

@BusinessSector5

使用參數化查詢時,您不需要在參數名稱周圍添加任何內容,它不會與代碼組合在一起,而是會分別發送到服務器(它會在編寫時發送sql和參數列表)。 因此,您將再次受到sql注入和相關問題的保護。

"(Select [SubCategory].ID from SubCategory where Kategorie = @SubCategory)," +
"(SELECT [BusinessSector5].ID FROM BusinessSector5 where Description_DE = @BusinessSector5));",con))

從查詢中刪除引號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM