I ran the SQL Query in SQL Server Management Studio and it worked.
我在SQL Server Management Studio中运行SQL查询并且它有效。
Here is my code
这是我的代码
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;
}
No error is given by VS2012 and no data is presented in my DataGridView
VS2012没有给出错误,我的DataGridView中没有数据显示
Any suggestions?
有什么建议么?
I saw this website and it didn't really help
我看到这个网站并没有真正帮助
http://bytes.com/topic/c-sharp/answers/530616-datagridview-combobox-column-databound-item-list
I am using an SQL Server 2012
我正在使用SQL Server 2012
The updated Query brings 1 single result in the SQL Server Management Studio (which is expected). The same query does not produce any rows in my datagrid.
更新的Query在SQL Server Management Studio中提供了1个单一结果(这是预期的)。相同的查询不会在我的数据网格中生成任何行。
I can't figure out whats going on? Do I need to bind anything using the GUI for VS2012?
我无法弄清楚最近发生了什么?我是否需要使用VS2012的GUI绑定任何内容?
2 个解决方案
#1
4
I notice you're doing a LIKE search, but when you're adding your parameters, you're not using "%". Try adding this:
我注意到你正在进行LIKE搜索,但是当你添加参数时,你没有使用“%”。尝试添加此:
command.Parameters.AddWithValue(name, "%" + word +"%");
Hope this helps.
希望这可以帮助。
BTW -- The DataBind
method is not used in win forms for gridviews, just in web forms.
BTW - DataBind方法不用于网格视图的获胜形式,仅用于网络表单。
Good luck.
祝你好运。
#2
1
You should call method DataBind() on your DataGrid to actually bind data from the data source to the grid.
您应该在DataGrid上调用方法DataBind()以实际将数据源中的数据绑定到网格。
dataGridView1.DataBind();
#1
4
I notice you're doing a LIKE search, but when you're adding your parameters, you're not using "%". Try adding this:
我注意到你正在进行LIKE搜索,但是当你添加参数时,你没有使用“%”。尝试添加此:
command.Parameters.AddWithValue(name, "%" + word +"%");
Hope this helps.
希望这可以帮助。
BTW -- The DataBind
method is not used in win forms for gridviews, just in web forms.
BTW - DataBind方法不用于网格视图的获胜形式,仅用于网络表单。
Good luck.
祝你好运。
#2
1
You should call method DataBind() on your DataGrid to actually bind data from the data source to the grid.
您应该在DataGrid上调用方法DataBind()以实际将数据源中的数据绑定到网格。
dataGridView1.DataBind();