欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 操作 access 数据库的实例代码

程序员文章站 2023-12-18 23:32:40
随笔: (1)   命名空间           ...

随笔:

(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数据库链接字符串: 

 privatestatic stringconnstr =@"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 =new oledbconnection(connstr);

(4)   使用oledbcommand类来执行sql语句:      

 oledbcommand cmd = new oledbcommand(sql, tempconn); 
      tempconn.open();
      cmd.executenonquery();

栗子(工具类):

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);
    }
    /// <summary>
    /// 执行增加、删除、修改指令
    /// </summary>
    /// <param name="sql">增加、删除、修改的sql语句</param>
    /// <param name="param">sql语句的参数</param>
    /// <returns></returns>
    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());
        }
      }
    }
    /// <summary>
    /// 执行查询指令,获取返回的首行首列的值
    /// </summary>
    /// <param name="sql">查询sql语句</param>
    /// <param name="param">sql语句的参数</param>
    /// <returns></returns>
    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());
        }
      }
    }
    /// <summary>
    /// 执行查询指令,获取返回的datareader
    /// </summary>
    /// <param name="sql">查询sql语句</param>
    /// <param name="param">sql语句的参数</param>
    /// <returns></returns>
    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));
    }
    /// <summary>
    /// 执行查询指令,获取返回datatable
    /// </summary>
    /// <param name="sql">查询sql语句</param>
    /// <param name="param">sql语句的参数</param>
    /// <returns></returns>
    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 数据库的实例代码,希望对大家有所帮助

上一篇:

下一篇: