C# 执行查询并在datagrid框中填充查询
我有一个winform应用程序作为数据源连接到access数据库 但在access中,我使用下面的SQL生成了一个名为trend的查询表:C# 执行查询并在datagrid框中填充查询,c#,sql,visual-studio,winforms,C#,Sql,Visual Studio,Winforms,我有一个winform应用程序作为数据源连接到access数据库 但在access中,我使用下面的SQL生成了一个名为trend的查询表: TRANSFORM Count(Complaint_Number) AS [Total Numbers] SELECT Nature_of_problem, Count(Complaint_Number) AS [Total Numbers of Issues] FROM master_table GROUP BY Nature_of_problem PIV
TRANSFORM Count(Complaint_Number) AS [Total Numbers]
SELECT Nature_of_problem, Count(Complaint_Number) AS [Total Numbers of Issues]
FROM master_table
GROUP BY Nature_of_problem
PIVOT Format(Complaint_Received_On,"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
但我不知道如何在表单加载事件(数据集:CSCdataset)的datagrid框中执行此操作并填充相同的内容,请帮助我。我想您可以在此处找到有关问题的更多信息: 这篇文章指的是以下文件: 在我的winform应用程序中,我使用SQL Server。对于Access数据库,这可能是相同的过程:
using System.Data.SqlClient;
//open connection
//cnnString = $@"Data Source=<yourServer>;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=<yourDatabase>
SqlConnection cnn = new SqlConnection(@cnnString);
cnn.Open();
//execute query
string exec = $@"<your query>";
DataSet dataSet = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(exec, cnn);
adapter.Fill(dataSet); //fill dataset
cnn.Dispose();
//read dataset
dataSet.Tables[0].Rows ...
资料来源:
要将DataTable绑定到DataGridView,只需将DataTable分配给DataGridView的DataSource属性,如下所示:
using (SqlConnection connection = new SqlConnection(
connectionString)) {
try {
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex){
//log and/or rethrow or ignore
}
}
dataGridView.DataSource = yourDataTable;
我希望我的回答能帮你一点忙。
干杯非常感谢你……你确实帮了我很多忙。很高兴我能帮上忙,如果你愿意,请给我一张选票:)