我无法将数据导入我的dataGridView [英] I am unable to get Data into my 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);
}
}
私有SqlCommand createSQLQuery(SqlCommand命令)
{
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]。 [clientTable]。[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(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命令;
}
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个单一结果(预期)。相同的查询在我的datagrid中不会产生任何行。
我不知道发生了什么?我需要使用VS2012的GUI绑定任何东西吗?
我注意到你正在做一个LIKE搜索,但是当你添加你的参数,你没有使用%。尝试添加以下内容:
command.Parameters.AddWithValue(name,%+ word +%);
希望这有帮助。
BTW - -
祝你好运。 / p>
I ran the SQL Query in SQL Server Management Studio and it worked.
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
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
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.
I can't figure out whats going on? Do I need to bind anything using the GUI for VS2012?
I notice you're doing a LIKE search, but when you're adding your parameters, you're not using "%". Try adding this:
command.Parameters.AddWithValue(name, "%" + word +"%");
Hope this helps.
BTW -- The DataBind
method is not used in win forms for gridviews, just in web forms.
Good luck.
这篇关于我无法将数据导入我的dataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!