.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;

  }

}

.net 关于数据库的链接

上一篇:SQL0668N 由于表 "db2inst1.test" 上的原因代码 "3",所以不允许操作(解因为LOAD引起的LOAD暂挂状态锁)


下一篇:NATIVE SQL的用法