C 操作Access数据库步骤

               

本教程介绍了如何通过ADO.NET的OleDb相关类来操作Access数据库的相关步骤。

1     软件环境

开发平台:Microsoft Visual Studio 2010

数据库:Microsoft Office Access 2007

2     建立数据库步骤

(1) 打开Microsoft Office Access 2007,选择空白数据库,然后输入数据库名称和存放路径,这里数据库名称为login.accdb,路径在D盘下,单机创建。

 C 操作Access数据库步骤_第1张图片

(2)选中表1,右键选择设计视图,然后输入表名,这里为tb_user,单击确定。

C 操作Access数据库步骤_第2张图片 

(3)填写字段名称和数据类型。

C 操作Access数据库步骤_第3张图片 

(4)选中tb_user表,单击右键—>打开,在里面添加数据即可。

C 操作Access数据库步骤_第4张图片

3     C#代码操作步骤

(1)   命名空间

            using System.Data.OleDb;

(2)   连接字符串

            private staticstring connStr = @"Provider= Microsoft.Ace.OLEDB.12.0;Data Source = d:\login.accdb";

            注意:

             a、DataSource = 数据库存放的路径(这里数据库放到了D盘目录下)

             b、 2003版本的Access数据库链接字符串:  privatestaticstringconnStr =@"Provider = Microsoft.Jet.OLEDB.4.0;DataSource = d:\login.mdb";

                   2007版本的Access数据库链接字符串:privatestaticstring connStr =@"Provider= Microsoft.Ace.OLEDB.12.0;Data Source = d:\login.accdb";

(3)   建立连接:

            OleDbConnection tempconn =newOleDbConnection(connStr);

(4)   使用OleDbCommand类来执行Sql语句:

           OleDbCommand cmd = new OleDbCommand(sql, tempconn);  

           tempconn.Open();

           cmd.ExecuteNonQuery();

(5)   代码

  

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.OleDb;using System.Windows.Forms;using System.Data; namespace test{    class AccessHelper    {        private static string connStr = @"Provider = Microsoft.Ace.OLEDB.12.0;Data Source = d:\login.accdb";         public static OleDbConnection GetConn()        {            OleDbConnection tempconn = new OleDbConnection(connStr);            MessageBox.Show(tempconn.DataSource);            tempconn.Open();            MessageBox.Show(tempconn.State.ToString());            return (tempconn);        }         ///         /// 执行增加、删除、修改指令        ///         /// 增加、删除、修改的sql语句        /// sql语句的参数        ///         public static int ExecuteNonQuery(string sql, params OleDbParameter[] param)        {            using (OleDbConnection conn = new OleDbConnection(connStr))            {                using (OleDbCommand cmd = new OleDbCommand(sql,conn))                {                    if (param != null)                    {                        cmd.Parameters.AddRange(param);                    }                    conn.Open();                    return(cmd.ExecuteNonQuery());                }            }        }         ///         /// 执行查询指令,获取返回的首行首列的值        ///         /// 查询sql语句        /// sql语句的参数        ///         public static object ExecuteScalar(string sql, params OleDbParameter[] param)        {            using (OleDbConnection conn = new OleDbConnection(connStr))            {                using (OleDbCommand cmd = new OleDbCommand(sql, conn))                {                    if (param != null)                    {                        cmd.Parameters.AddRange(param);                    }                    conn.Open();                    return (cmd.ExecuteScalar());                }            }        }         ///         /// 执行查询指令,获取返回的datareader        ///         /// 查询sql语句        /// sql语句的参数        ///         public static OleDbDataReader ExecuteReader(string sql, params OleDbParameter[] param)        {            OleDbConnection conn = new OleDbConnection(connStr);            OleDbCommand cmd = conn.CreateCommand();            cmd.CommandText = sql;            cmd.CommandType = CommandType.Text;            if (param != null)            {                cmd.Parameters.AddRange(param);            }            conn.Open();            return (cmd.ExecuteReader(CommandBehavior.CloseConnection));         }         ///         /// 执行查询指令,获取返回datatable        ///         /// 查询sql语句        /// sql语句的参数        ///         public static DataTable ExecuteDatable(string sql, params OleDbParameter[] param)        {            using (OleDbConnection conn = new OleDbConnection(connStr))            {                using (OleDbCommand cmd = new OleDbCommand(sql, conn))                {                    if (param != null)                    {                        cmd.Parameters.AddRange(param);                    }                    DataTable dt = new DataTable();                    OleDbDataAdapter sda = new OleDbDataAdapter(cmd);                    sda.Fill(dt);                    return (dt);                }            }         }    }}


           

你可能感兴趣的:(C 操作Access数据库步骤)