我在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行?另外,我相信你在設置'dataGridView1.DataSource'屬性後缺少'dataGridView1.DataBind();'。 –
當我使用單詞「single」執行查詢時,SQL Server Management Studio中返回1行。在C#應用程序中,我什麼也沒得到 –