C# 我无法将数据获取到dataGridView中
我在SQLServerManagementStudio中运行了SQL查询,它成功了 这是我的密码C# 我无法将数据获取到dataGridView中,c#,sql-server,winforms,datagridview,datatable,C#,Sql Server,Winforms,Datagridview,Datatable,我在SQLServerManagementStudio中运行了SQL查询,它成功了 这是我的密码 private void buttonRunQuery_Click(object sender, EventArgs e) { if (connection == null) { connection = ConnectionStateToSQLServer(); SqlCommand comm
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
更新后的查询将在SQLServerManagementStudio中产生一个结果(这是预期的结果)。同一查询不会在我的datagrid中生成任何行
我搞不懂发生了什么事?是否需要使用VS2012的GUI绑定任何内容?您应该在DataGrid上调用方法DataBind(),以实际将数据源中的数据绑定到网格
dataGridView1.DataBind();
您应该在DataGrid上调用方法DataBind(),以实际将数据从数据源绑定到网格
dataGridView1.DataBind();
我注意到您正在进行类似的搜索,但在添加参数时,您没有使用“%”。尝试添加以下内容:
command.Parameters.AddWithValue(name, "%" + word +"%");
希望这有帮助
顺便说一句,DataBind
方法不用于GridView的win表单,只用于web表单
祝你好运。我注意到你在做一个类似的搜索,但是当你添加参数时,你没有使用“%”。尝试添加以下内容:
command.Parameters.AddWithValue(name, "%" + word +"%");
希望这有帮助
顺便说一句,DataBind
方法不用于GridView的win表单,只用于web表单
祝你好运。查询正在执行,但可能返回0行吗?另外,我认为您缺少一个
dataGridView1.DataBind()
设置dataGridView1.DataSource
属性后。当我使用单词“single”执行查询时,在SQL Server Management Studio中返回1行。在C#应用程序中,我什么都没有得到:查询正在执行,但可能返回0行?另外,我认为您缺少一个dataGridView1.DataBind()
设置dataGridView1.DataSource
属性后。当我使用单词“single”执行查询时,在SQL Server Management Studio中返回1行。在C#应用程序中,我没有得到任何错误1'System.Windows.Forms.DataGridView'不包含'DataBind'的定义,并且找不到接受'System.Windows.Forms.DataGridView'类型的第一个参数的扩展方法'DataBind'(是否缺少using指令或程序集引用?),我以为它是Web窗体。您是对的,对于Windows窗体,您只需要设置数据源。那就奇怪了,它不起作用了。可能您的查询格式不正确,尝试将查询设置为与您在Management Studio中使用的相同的静态文本。任何其他建议@YuriyError 1'System.Windows.Forms.DataGridView'不包含'DataBind'的定义,并且找不到接受'System.Windows.Forms.DataGridView'类型的第一个参数的扩展方法'DataBind'(是否缺少using指令或程序集引用?)我认为这是Web表单。你是对的,对于Windows表单,你只需要设置DataSource。然后奇怪的是,它不起作用。可能你的查询格式不正确,请尝试将查询设置为与你在Management Studio中使用的相同的静态文本。是否有其他建议@YuriyNice,sgedes。因为该查询包含多个类似项,而没有“%”,它试图同时找到几个不同单词相等的值,这当然是不可能的。我根据@sgeddes的建议添加了%但没有返回任何数据。如何从参数化的SqlCommand获取输出?(基本上我想检查查询,看它是否100%有效)很好的发现,sgeddes。由于查询包含多个没有“%”的like,它试图同时找到几个不同单词相等的值,这当然是不可能的。我根据@sgeddes的建议添加了%但没有得到任何数据。我如何从参数化的SqlCommand获得输出?(基本上我想检查查询是否100%有效)