.net 关于数据库的链接

web.config的配置

<appSettings>

  <add key="Configpath" value="~/XmlConfig/webset.config"/>

  <add key="DbPath" value="~/App_Data/test.mdb"/>

</appSettings>

DbHelperOleDb.cs 的类方法

//数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库。

public static string connectionString=ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString()+HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["DbPath"])+";";

执行一条计算查询,返回查询结果(object)

public static object GetSingle(string SQLString)

{

  using(OleDbConnection connection=new OleDbConnection(connectionString))

    {

      using (OleDbCommand cmd=new OleDbCommand(SQLString,connection))

      {

        try

        {

          connection.Open();

          object obj=cmd.ExecuteScalar();

          if(Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))

          {

            return null;

          }

          else

          {

            return obj;

          }

        }

        catch(system.Data.OleDb.OleDbException e)

      }

    }

}

执行查询语句 返回OleDbDataReader

public static OleDbDataReader ExecuteReader(string strSQL)

{

  OleDbConnection connection =new OleDbConnection (connectionString);

  OleDbCommand cmd=new OleDbCommand(strSQL,connetction)

  try

  {

    connection.Open();

    OleDbDataReader myReader=cmd.ExecuteReader();

    return myReader;

  }

  catch(System.Data.OleDb.OleDbException e)

  {

    throw new Exception(e.Message);

  }

}

执行查询语句 , 返回DataSet

Public static DataSet Query(string SQLString)

{

  using (OleDbConnection connection=new OleDbConnection(connectionString))

  {

    DataSet ds=new DataSet();

    try

    {

      connection.Open();

      OleDbDataAdapter command=new OleDbDataAdapter(SQLString,connection);

      command.Fill(ds,"ds");

    }

    catch(System.Data.OleDb.OleDbException ex)

    {

      throw new Exception(ex.Message);

    }

    return ds;

  }

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。