簡體   English   中英

錯誤:無法綁定多部分標識符,C#控制台應用程序

[英]ERROR : The multi-part identifier could not be bound, c# console Application

SqlConnection cn = new SqlConnection("user id=ID;" +
                       "password=PASS;server=svr;" +
                       "Trusted_Connection=no;" +
                       "database=db; " +
                       "connection timeout=30");
                       cn.Open();
            SqlCommand command1 = new SqlCommand();

            command1.Connection = cn;
                     Console.WriteLine(ListofOrders.Count);
              for (int i = 0; i < ListofOrders.Count; i++)
                 command1.CommandText += string.Format("update table set Status='Expired' where GUID={0};", ListofOrders[i].ToString());

            command1.ExecuteNonQuery();
                    // LogicHandler.UpdateActiveOrders();
            Console.WriteLine("DONE", ConsoleColor.Cyan);

在此步驟獲取錯誤: command1.ExecuteNonQuery(); Error Message: The multi-part identifier could not be bound.

我在這里嘗試的是我正在運行一個選擇查詢,並將該數據輸入到ListofOrders列表中,而我想對列表中的那些數據運行更新。

請幫忙

如果您使用如table這樣的保留關鍵字 ,則必須將其包裝在方括號中: [table] 但是最好不要首先使用它們。

我想您需要像GUID='{0}'那樣用撇號將Guid換行。 但是,應該始終使用sql-parameters而不是字符串連接。 這樣也可以防止sql-injection。

string update = @"update tablename -- or [Table] but i wouldnt do that
                  set Status='Expired' 
                  where GUID=@GUID";
command1.CommandText = update;
command1.Parameters.Add("@GUID", SqlDbType.UniqueIdentifier).Value = new Guid(ListofOrders[i].ToString());

command1.CommandText += ,為什么要使用command1.CommandText +=而不是僅使用command1.CommandText = 至少這令人困惑,如果重用該命令,也可能導致錯誤。

暫無
暫無

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

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