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

  }

}

上一篇:C++程序设计实践指导1.1删除序列中相同的数改写要求实现


下一篇:CentOS7 下 Hadoop 分布式部署