2013-02-10 65 views
0
SqlCommand cmd = new SqlCommand(); 
cmd.Connection = con; 
cmd.CommandType = CommandType.Text; 
cmd = CreateParameterizedQuery(); 
SqlDataAdapter dap = new SqlDataAdapter(); 
dap.SelectCommand = cmd; 
DataTable tbl = new DataTable(); 
dap.Fill(tbl); 
if (tbl.Rows.Count > 0) 
{ 
    grid.DataSource = tbl; 
} 

實際的SQL查詢將在SQL Management Studio中產生結果。但是我得到0行數據。我在tbl.Rows.Count處設置了一個斷點,我發現它是0,並且步進將跳過必要的代碼來設置數據源。SqlDataAdapter給我的結果爲0

private SqlCommand CreateParameterizedQuery() 
    { 
     SqlCommand command = new SqlCommand(); 
     string[] allTheseWords; 
     if (textBoxAllTheseWords.Text.Length > 0) 
     { 
      allTheseWords = textBoxAllTheseWords.Text.Split(' '); 
      string SQLQuery = "SELECT distinct [databaseName].[dbo].[customerTable].[name], [databaseName].[dbo].[customerTable].[dos], [databaseName].[dbo].[customerTable].[ACC], [databaseName].[dbo].[reportTable].[id], [databaseName].[dbo].[reportTable].[ACC], [databaseName].[dbo].[reportTable].[fullreport] FROM [databaseName].[dbo].[reportTable], [databaseName].[dbo].[customerTable] WHERE "; 
      int i = 0; 
      foreach (string word in allTheseWords) 
      { 
       var name = "@word" + (i++).ToString(); 
       command.Parameters.AddWithValue(name, "'%" + word + "%'"); 
       SQLQuery = SQLQuery + String.Format(" [databaseName].[dbo].[reportTable].[fullreport] LIKE {0} AND ", name); 
      } 
      SQLQuery = SQLQuery + " [databaseName].[dbo].[customerTable].[ACC] = [databaseName].[dbo].[reportTable].[ACC]"; 
      command.CommandText = SQLQuery; 
     } 
     return command; 
    } 

我使用與C#的WinForm在Windows 8


的的SQLQuery變量調試

SELECT distinct [databaseName].[dbo].[customerTable].[name], [databaseName].[dbo].[customerTable].[dos], [databaseName].[dbo].[customerTable].[ACC], [databaseName].[dbo].[reportTable].[customerID], [databaseName].[dbo].[reportTable].[ACC], [databaseName].[dbo].[reportTable].[fullreport] FROM [databaseName].[dbo].[reportTable], [databaseName].[dbo].[customerTable] WHERE [databaseName].[dbo].[reportTable].[fullreport] LIKE @word0 AND [databaseName].[dbo].[customerTable].[ACC] = [databaseName].[dbo].[reportTable].[ACC] 

debugMySQL是吐出來的是SQL查詢使用的方法包含此數據取代參數

public void debugMySQL() 
    { 
     string query = command.CommandText; 
     foreach (SqlParameter p in command.Parameters) 
     { 
      query = query.Replace(p.ParameterName, p.Value.ToString()); 
     } 
     textBox1.Text = query; 
    } 

輸出類似於

SELECT distinct [databaseName].[dbo].[customerTable].[name], [databaseName].[dbo].[customerTable].[dos], [databaseName].[dbo].[customerTable].[ACC], [databaseName].[dbo].[reportTable].[id], [databaseName].[dbo].[reportTable].[ACC], [databaseName].[dbo].[reportTable].[fullreport] FROM [databaseName].[dbo].[reportTable], [databaseName].[dbo].[customerTable] WHERE [databaseName].[dbo].[reportTable].[fullreport] LIKE '%single%' AND [databaseName].[dbo].[customerTable].[ACC] = [databaseName].[dbo].[reportTable].[ACC] 
+1

使用事件探查器來查看發送到SQL Server的真實查詢。 – 2013-02-10 19:20:00

+1

你在調試器中檢查過'SQLQuery'的內容嗎?你能在你的問題中發佈完整的SQL字符串嗎? – 2013-02-10 19:20:41

+0

我糾正了你的代碼中的一些錯誤,但它是什麼意思?你真的測試了什麼? – 2013-02-10 19:22:00

回答

2

你可以看到使用SQL Profiler它的參數值,參數化查詢。

enter image description here

+0

當我運行它時,它說@ word0 – 2013-02-15 03:19:15