如何在DataGridView中使用OLEDB conncetion绑定数据 [英] how to bind data using OLEDB conncetion in DataGridView

查看:66
本文介绍了如何在DataGridView中使用OLEDB conncetion绑定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有组合框。在该组合框中,从数据库中检索所有教师详细信息。编码工作正常。



然后我有一个名为View的按钮。当我点击查看按钮时,数据库中的所有数据都将显示在datagridview中使用OLE DB连接。





i使用Ms Access数据库。



。在View Button中我可以使用OLEDB编写代码并显示在Datagridview中。



注意:在windows应用程序中



datagridview.databind();

解决方案



下面的代码将从access数据库中获取数据并将其显示在网格中。请修改mdb路径和文件名,并修改选择查询。

  private   void  button1_Click( object  sender,EventArgs e)
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
// TODO:修改连接字符串并包含任何
// 数据库的其他必需属性。
conn.ConnectionString = @ Provider = Microsoft.Jet.OLEDB.4.0; +
@ 数据源= C:\ myAccessFile.mdb;
尝试
{
conn.Open();
// 插入代码以处理数据。

string sqlQuery = select * from table;
OleDbDataAdapter da = new OleDbDataAdapter(sqlQuery,conn);

DataTable dtTable = new DataTable();

da.Fill(dtTable);

dataGridView1.DataSource = dtTable;
}
catch (Exception ex)
{
MessageBox.Show( 无法连接到数据源);
}
最后
{
conn.Close();
}
}





最诚挚的问候,

Muthuraja


i have combo box.in that combo box all faculty details are retrieved from the database. that coding is working.

then i have one button called View.when i click the view button all the data''s from the database will be displayed in the datagridview using OLE DB connection.


i am using Ms Access Database.

in that View Button.how can i write the code using OLEDB and displayed in to the Datagridview.

Note:in windows application

datagridview.databind();

解决方案

Hi,
Below code will get the data from access database and show it to the grid. Please modify the mdb path and file name and also modify the select query.

private void button1_Click(object sender, EventArgs e)
       {
           System.Data.OleDb.OleDbConnection conn = new   System.Data.OleDb.OleDbConnection();
           // TODO: Modify the connection string and include any
           // additional required properties for your database.
           conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
               @"Data source= C:\myAccessFile.mdb";
           try
           {
               conn.Open();
               // Insert code to process data.

               string sqlQuery = "select * from table";
               OleDbDataAdapter da = new OleDbDataAdapter(sqlQuery, conn);

               DataTable dtTable = new DataTable();

               da.Fill(dtTable);

               dataGridView1.DataSource = dtTable;
           }
           catch (Exception ex)
           {
               MessageBox.Show("Failed to connect to data source");
           }
           finally
           {
               conn.Close();
           }
       }



Best Regards,
Muthuraja


这篇关于如何在DataGridView中使用OLEDB conncetion绑定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆