簡體   English   中英

嘗試將字母數字值插入數據庫時​​,列名無效

[英]Invalid column name when trying to insert alphanumeric value into database

基本上,我有一些采用(1)字母數字或(2)數字序列號並將其遞增的代碼。 一切都適用於數字序列號,但是當我嘗試插入字母數字序列號時,會出現“無效列名”錯誤。

我在這里查看過很多“無效的列名”帖子,但它們似乎都無法回答我的問題。 我已經設置了斷點並針對兩種情況(數字和字母數字)運行了代碼,並且得到了相同的數據類型。 基本上一切似乎都正確排列,所以我很茫然。

以下代碼顯示了兩種情況下我如何遞增。 請注意,對於字母數字增量,我正在調用方法IncrementAlphaNumeric,該方法采用變量“輸出”,這是對表進行排序並獲取最后一個序列號的SQL查詢的結果。

// Increment Numeric Serial Numbers
if (isNum)
{
    int lastNumber = Int32.Parse(Output);
    int[] ints = Enumerable.Range(lastNumber + 1, printQuantity).Select(i => (int)i / 1).ToArray();
    increments = ints.Select(x => x.ToString()).ToArray();
    output.AppendText("Serial numbers to print: " + string.Join(", ", increments)); 
}

// Increment AlphaNumeric Serial Numbers
if (!isNum)
{
    for (int i = 0; i < printQuantity; i++)
    {
         increments[i] = IncrementAlphaNumeric(Output);
         snList.Add(increments[i]);
         Output = increments[i];
    }
    output.AppendText("Serial numbers to print: " + string.Join(", ", increments));
 }

最后,我使用Stringbuilder來將數據插入數據庫,如下所示:

// (5) Store new SNs in Database
StringBuilder sb = new StringBuilder();
foreach (string newSns in increments)
{
    sb.AppendLine("INSERT INTO [Manufacturing].[dbo].[Device.Devices]([SerialNumber],[DeviceTypeID]) VALUES(" + newSns + "," + dType +")");
}

using (SqlCommand insertCommand = new SqlCommand(sb.ToString(), cnn))
{
    var executeNonQuery = insertCommand.ExecuteNonQuery();
}

同樣,它適用於數字,但不適用於字母數字。 當我將斷點放入代碼並逐步執行代碼時,每種情況下的數據類型(字符串)都是相同的,即數字和字母數字。

我收到的錯誤消息再次是“無效的列名”。 基本上,預期結果應該是將序列號(無論是數字還是字母數字)插入基於設備類型(dType)的數據庫的正確表中。

正如注釋中所解釋的那樣,您永遠不要串聯字符串來構建sql語句。
在您的情況下,我假設您要使用一條語句在數據庫中插入多個記錄。 也可以使用參數並手動構建VALUES部分或查詢來完成此操作(可從Sql Server 2008獲得此語法)

// Sample values, replace them with your code that builds the increments array
string[] increments = new string[] {"VALUE1", "VALUE2","VALUE3", "VALUE4"};

// Invariant part of your query
string baseQuery = "INSERT INTO [Manufacturing].[dbo].[Device.Devices]([SerialNumber],[DeviceTypeID]) VALUES"; 

// Fixed value for the type 
string dType = "42";

List<SqlParameter> prms = new List<SqlParameter>();
List<string> placeHolders = new List<String>();

// Build a list of parameter placeholders and a list of those parameter and their values
for(int x = 0; x < increments.Length; x++)
{
    placeHolders.Add($"(@p{x},{dType})");
    prms.Add(new SqlParameter { ParameterName = $"@p{x}", SqlDbType = SqlDbType.NVarChar, Value = increments[x]});
}

// Put the text together
string queryText = baseQuery + string.Join(",", placeHolders);
// This should be the final text
// INSERT INTO [Manufacturing].[dbo].[Device.Devices]([SerialNumber],[DeviceTypeID])  
// VALUES(@p0,42),(@p1,42),(@p2,42),(@p3,42)


using (SqlCommand insertCommand = new SqlCommand(queryText, cnn))
{
    // Add all parameters to the command...
    insertCommand.Parameters.AddRange(prms.ToArray());
    var executeNonQuery = insertCommand.ExecuteNonQuery();
}

暫無
暫無

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

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