C#连接access数据库并将数据读取到dataGridView中显示



using System.Data.OleDb;
using System.Data.SqlClient;


            string strFilePath = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=" + Application.StartupPath + "\\111.accdb";

            string sql = "select * from 111";
            //声明一个数据连接
            System.Data.OleDb.OleDbConnection con = new OleDbConnection(strFilePath);
            System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter(sql, con);

            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
                if (dt.Rows.Count >= 1)
                {
                    dataGridView1.DataSource = dt;
                }

            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                con.Close();
                con.Dispose();
                da.Dispose();
            }


简单说明:

1 建立连接,因为是access数据库,所以建立的是OleDbConnection

2 建立适配器OleDbDataAdapter da,目的是将数据库里的表fill到dt中,那为什么不让da直接有dt的功能?这就不用fill了!!

3 dt,说白了就是一张表,当然有dt.Rows.Count行数啊

4 dataGridView的成员DataSource就是一个DataTable,指明一下就ok了

5 记得关闭连接Close、Dispose和关闭适配器da.Dispose();


简单总结:

涉及到的类:

1 Application,它的StartupPath即debug文件

2 OleDbConnection

3 OleDbDataAdapter:肯定会需要的con和sql语句参数都用到了。2、3是不是可以连在一起?每次数据库操作都可以用这两个一起来,或这只是一种方式?

4 DataTable,表格了,它的行数.Rows.Count

5 DataGridView,这里只做显示用,应该还能在其上进行操作

6 Exception,常用的方法是:ex.ToString());

可以使用C#的NPOI库和DataGridView控件来将Excel数据转换并显示DataGridView。下面是一个示例代码: ```csharp using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.Data; using System.Windows.Forms; public static class ExcelUtility { public static void ExcelToDataGridView(string filePath, DataGridView dataGridView) { IWorkbook workbook = null; ISheet sheet = null; DataTable data = new DataTable(); using (var file = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { if (filePath.EndsWith(".xls")) { workbook = new HSSFWorkbook(file); } else if (filePath.EndsWith(".xlsx")) { workbook = new XSSFWorkbook(file); } if (workbook != null) { sheet = workbook.GetSheetAt(0); if (sheet != null) { var firstRow = sheet.GetRow(0); int cellCount = firstRow.LastCellNum; for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { var cell = firstRow.GetCell(i); if (cell != null) { string columnName = cell.ToString(); if (!string.IsNullOrEmpty(columnName)) { data.Columns.Add(columnName); } } } for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; ++i) { var row = sheet.GetRow(i); if (row != null) { bool emptyRow = true; DataRow dataRow = data.NewRow(); for (int j = row.FirstCellNum; j < cellCount; ++j) { if (row.GetCell(j) != null) { dataRow[j] = row.GetCell(j).ToString(); if (!string.IsNullOrEmpty(dataRow[j].ToString())) { emptyRow = false; } } } if (!emptyRow) { data.Rows.Add(dataRow); } } } } } } dataGridView.DataSource = data; } } ``` 这段代码会根据文件路径读取Excel文件,将第一个工作表转换为一个DataTable对象,并将该对象作为DataGridView控件的DataSource来显示Excel数据。你只需要将DataGridView控件添加到窗体即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值