C# 联接 Access

 

Access  另存为 .mdb 数据库; 在winform 中使用 Datagridview 控件,可以显示所有的源数据

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb; //using 注意
namespace Access_CS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=j:\\a.mdb"); //Jet OLEDB:Database Password=
            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from main";
            conn.Open();
            OleDbDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            if (dr.HasRows)
            {
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    dt.Columns.Add(dr.GetName(i));
                }
                dt.Rows.Clear();
            }
            while (dr.Read())
            {
                DataRow row = dt.NewRow();
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    row[i] = dr[i];
                }
                dt.Rows.Add(row);
            }
            cmd.Dispose();
            conn.Close();
            dataGridView1.DataSource = dt;
 
        }
    }
}
 
--------------------------------------------
另一种代码 Winform中,Program.CS中的代码
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Access_CS
{
 
        public class AccessDbClass
        {
            public OleDbConnection conn;
            public string ConnString;
            public AccessDbClass(string Dbpath)
            {
                ConnString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
                ConnString += Dbpath;
                conn = new OleDbConnection(ConnString);
                conn.Open();
            }
            public OleDbConnection DbConn()
            {
                conn.Open();
                return conn;
            }
            public void Close()
            {
                conn.Close();
            }
        ///
        /// SelectToDataTable(SQL 查询语句)
        ///

        ///
        ///
            public DataTable SelectToDataTable(string SQL)
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                OleDbCommand command = new OleDbCommand(SQL, conn);
                adapter.SelectCommand = command;
                DataTable Dt = new DataTable();
                adapter.Fill(Dt);
                return Dt;
            }
            public DataSet SelectToDataSet(string SQL, string subtableName)//根据SQL命令返回数据dataset数据集,表可做为 data
                //data gridview 的数据源, subtablename 是是返回的数据集中,所填加的表的名称
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                OleDbCommand command = new OleDbCommand(SQL, conn);
                adapter.SelectCommand = command;
                DataSet Ds = new DataSet();
                Ds.Tables.Add(subtableName);
                adapter.Fill(Ds, subtableName);
                return Ds;
            }
            public DataSet SelectToDataSet(string SQL, string subtableName, DataSet DataSetName)//上面的代码在数据集中添加有指定名称的表
                //存在覆盖的风险,返回操作之前 的数据表; subtable Name:添加的表的名称; Datassetname 被添加的数据名称
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                OleDbCommand command = new OleDbCommand(SQL, conn);
                adapter.SelectCommand = command;
                DataTable Dt = new DataTable();
                DataSet Ds = new DataSet();
                Ds = DataSetName;
                adapter.Fill(DataSetName, subtableName);
                return Ds;
            }
            public OleDbDataAdapter SelectToOleDbDataAdapter(string SQL)//根据SQL命令返回OldDbDataAdapter
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                OleDbCommand command = new OleDbCommand(SQL, conn);
                adapter.SelectCommand = command;
                return adapter;
            }
 
        ///
        /// 应用程序的主入口点。
        ///

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
...............................................
Form1.Button1中的代码
 private void button1_Click(object sender, EventArgs e)
        {
            AccessDbClass mydb = new AccessDbClass(@"J:\a.mdb");
            DataTable dt = new DataTable();
            dt = mydb.SelectToDataTable(@"select * from main");
            dataGridView1.DataSource = dt;
          
 
        }
 

你可能感兴趣的:(C# 联接 Access)