2011-06-15 47 views
0

我這樣做:SQL C#更新表

var command = new SqlCommand(query, myConnection); 
       foreach (DataRow row in dt.Rows) 
       { 
        query = @"update FileLog set 
        FaxStatus=" + "'" + row.ItemArray[0].ToString() + "'," + 
        "FaxedPageCount=" + "'" + row.ItemArray[1].ToString() + "'," + 
        "dtFaxed=" + "'" + row.ItemArray[2].ToString() + "'," + 
        "RetryCount=" + "'" + row.ItemArray[4].ToString() + "' " + 
        "where JobID=" + "'" + row.ItemArray[3].ToString() + "'"; 
        command = new SqlCommand(query, myConnection); 
        command.ExecuteNonQuery(); 

       } 

JobIDuniqueidentifier

而且我收到此錯誤:

Conversion failed when converting from a character string to uniqueidentifier. 

我在做什麼錯?

的作業ID字段是這樣的:

DB9424E5-1E73-4108-A855-B252E516A2A2 
2EB17B8B-C0A1-46FE-82AF-37AEF2A8A6EC 
C24F0460-7667-4A3A-8D8F-64B9728C2359 
8DCDB020-8C7B-493E-9D21-719CBAFC16B6 
+0

首先,無論是使用'StringBuilder'或更好的'的String.Format(...)'來避免構建那些令人討厭的交錯字符串。你甚至可以同時使用!它比創建多個字符串對象只需返回一個字符串更有效。 – michael 2011-06-15 17:14:40

+0

它確實是JobID列(沒有涉及其他uniqueidentifier列)? – 2011-06-15 17:15:01

+1

另外,參數化查詢是您的朋友 - 不要將SQL構建爲字符串。寫一次查詢,然後插入參數 – 2011-06-15 17:15:52

回答

0

我找到了解決方案。原來你需要這樣做:

   var command = new SqlCommand(query, myConnection); 
       foreach (DataRow row in dt.Rows) 
       { 
        query = @"update FileLog set 
        FaxStatus=" + "'" + row.ItemArray[0].ToString() + "'," + 
        "FaxedPageCount=" + "'" + row.ItemArray[1].ToString() + "'," + 
        "dtFaxed=" + "'" + row.ItemArray[2].ToString() + "'," + 
        "BiscomCode=" + "'" + row.ItemArray[5].ToString() + "', " + 
        "RetryCount=" + "'" + row.ItemArray[4].ToString() + "' " + 
        "where CONVERT(VARCHAR(255), JobID) =" + "'" + row.ItemArray[3].ToString() + "'"; 
        command = new SqlCommand(query, myConnection); 
        command.ExecuteNonQuery(); 

       } 

你必須將其轉換爲varchar第一

+0

我知道它爲什麼有效,但我不認爲它是最佳解決方案。它可以讓數據庫改寫所有JobID,然後比較,如果可以檢查jobId是否格式正確。只要字符串是有效的GUID,就可以比較uniqueidentifier字段和文本字符串。所以,{select * from filelog其中JobID ='DB9424E5-1E73-4108-A855-B252E516A2A2'}是一個有效的查詢。 – Candide 2011-06-15 17:50:38

+0

@downvoter ...不要成爲仇敵。 ;-)評論會很高興看到我可以改進我的回答 – 2011-06-16 16:45:13

+0

不是我。但我提高了它。這不是一個壞問題。 – Candide 2011-06-17 14:19:37

-1

它很可能你的工作ID之一不是有效的GUID。

這裏有一個方法來檢查GUID:

public static bool IsGuid(string input) 
    { 
     Regex isGuid = new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled); 
     try 
     { 
      return isGuid.IsMatch(input); 
     } 
     catch 
     { 
      return false; 
     } 
    } 

所以發出查詢指令之前做的作業ID的檢查。如果不匹配,請將其轉義並記錄到稍後重訪。

+0

即時通訊不好意思,但我會如何解決這個問題? – 2011-06-15 17:20:17

+0

我已經看到這個項目的問題,如果jobid爲空或不是有效的GUID,它會拋出該錯誤。我認爲你不需要像上面的esastincy所說的那樣建立一個新的guid,因爲這只是一個正則表達式檢查。請參閱上面的方法。 – Candide 2011-06-15 17:25:40

1

這會更安全(從SQL注入安全),更易於閱讀和理解,並且更快,因爲準備好的語句獲取其執行計劃緩存。如果你有不同的sql,它不能使用緩存的執行計劃。

 SqlCommand cmd = 
     new SqlCommand(
      @"update FileLog set [email protected], [email protected], @[email protected], ......., where [email protected]") 
     {CommandType = CommandType.Text}; 
    cmd.Prepare(); 
    cmd.Connection = connection; 
    cmd.Parameters["@id"].Value = row.ItemArray[0]; 
    ... 
+0

您可能想創建一個新的Guid(row.ItemArray [3]),就像建議的@esasitincy一樣。 – 2011-06-15 17:31:46

+0

@downvoter ...不要成爲仇敵。 ;-)評論會很高興看到我可以改進我的答案。 – 2011-06-15 17:38:33