2013-02-01 26 views
0

我在SQL Server Management Studio中運行了SQL查詢並且它工作正常。我無法將數據導入到我的dataGridView

這裏是我的代碼

private void buttonRunQuery_Click(object sender, EventArgs e) 
    { 
     if (connection == null) 
     { 
      connection = ConnectionStateToSQLServer(); 
      SqlCommand command = new SqlCommand(null, connection); 
      command = createSQLQuery(command); 
      dataGridView1.DataSource = GetData(command); 
     } 
     else 
     { 
      SqlCommand command = new SqlCommand(null, connection); 
      command = createSQLQuery(command); 
      dataGridView1.DataSource = GetData(command); 
     } 
    } 

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

    public DataTable GetData(SqlCommand cmd) 
    { 
     //SqlConnection con = new SqlConnection(connString); 
     //SqlCommand cmd = new SqlCommand(sqlcmdString, cn); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     connection.Open(); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     connection.Close(); 
     return dt; 
    } 

沒有錯誤是由VS2012給出並且沒有數據在我的DataGridView

任何建議提出?

我看到這個網站,並沒有真正幫助

http://bytes.com/topic/c-sharp/answers/530616-datagridview-combobox-column-databound-item-list 

我使用的是SQL Server 2012的

更新的查詢就會在SQL Server Management Studio中1個單一的結果(預計)。相同的查詢不會在我的數據網格中產生任何行。

我無法弄清楚發生了什麼事?我需要使用VS2012的GUI來綁定任何東西嗎?

+0

查詢是否正在執行,但可能返回0行?另外,我相信你在設置'dataGridView1.DataSource'屬性後缺少'dataGridView1.DataBind();'。 –

+0

當我使用單詞「single」執行查詢時,SQL Server Management Studio中返回1行。在C#應用程序中,我什麼也沒得到 –

回答

4

我注意到你正在做一個LIKE搜索,但是當你添加你的參數時,你沒有使用「%」。嘗試添加此:

command.Parameters.AddWithValue(name, "%" + word +"%"); 

希望這有助於。

順便說一句 - DataBind方法不用於網格視圖的勝利形式,只是在網頁表單中。

祝你好運。

+0

不錯的斑點,sgeddes。由於查詢包含多個沒有「%」的LIKE,因此它會嘗試查找同時等於**幾個不同單詞的值,這當然是不可能的。 – Yuriy

+0

我按照@sgeddes的建議添加了%,但我沒有收到任何數據。 –

+0

如何從我的Parameterized SqlCommand中獲取輸出? (基本上我想檢查查詢是否100%有效) –

1

您應該在DataGrid上調用方法DataBind()以實際將數據源中的數據綁定到網格。

dataGridView1.DataBind(); 
+0

錯誤'System.Windows.Forms.DataGridView'沒有包含'DataBind'的定義,也沒有接受類型爲「System.Windows.Forms」的第一個參數的擴展方法'DataBind' .DataGridView'可以找到(你是否缺少使用指令或程序集引用?) –

+0

我以爲它是Web窗體。你是對的,對於Windows窗體你只需要設置DataSource。然後它很奇怪,它不起作用。也許您的查詢形成不正確,請嘗試將查詢設置爲靜態文本,與您在Management Studio中使用的查詢相同。 – Yuriy

+0

任何其他建議@Yuriy –

相關問題