选择查询sql c#如何将结果输出到DataGrid

本文关键字:结果 输出 DataGrid 查询 sql 选择 | 更新日期: 2023-09-27 17:52:50

如何将精炼查询的结果输出到名为"customerDataGridView"的数据网格视图?

string strCon = Properties.Settings.Default.PID2dbConnectionString;
using (OleDbConnection conn = new OleDbConnection(strCon)) {
   conn.Open();
   string strSql = "SELECT  * FROM customer WHERE City =Belfast";
   OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(strSql, conn));
}

谢谢你的帮助!

选择查询sql c#如何将结果输出到DataGrid

为ASP试试这样的东西。净:

DataSet ds = new DataSet();
OleDbDataAdapter oledbAdapter = New OleDbDataAdapter(strSql, connection);
oledbAdapter.Fill(ds);
customerDataGridView.DataSource = ds.Tables(0);
customerDataGridView.DataBind();

或者类似于WinForms:

DataSet ds = new DataSet();
OleDbDataAdapter oledbAdapter = New OleDbDataAdapter(strSql, connection);
oledbAdapter.Fill(ds);
customerDataGridView.DataSource = ds.Tables(0);

好运。

试试这个

DataSet ds = new DataSet();
OleDbDataAdapter oda = New OleDbDataAdapter(strSql, conn);
oda.Fill(ds);
customerDataGridView.DataSource = ds.Tables(0);

对于winform控件,您不需要DataBind()

在代码后尝试以下操作:

DataTable table = new DataTable();
adapter.Fill(table);
customerDataGridView.AutoGenerateColumns = True
customerDataGridView.Datasource = table;