SQLServer数据库通用访问类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQLServer数据库通用访问类相关的知识,希望对你有一定的参考价值。

private static string connString=ConfigurationManager.ConnStrings["connString"].ToString(); //在app.config中configuration添加相应的数据库连接配置文件

1.格式化sql语句

//执行增删改查

public static int Update(string sql)

{

  SqlConnection conn=new SqlConnection(connString);

  SqlCommand cmd=new SqlCommand(sql,conn);

  try

  {

    conn.Open();

    return cmd.ExecuteNonQuery();

  }

  catch(Exception ex)

  {

    throw ex;

  }

  finally

  {

    conn.Close();

  }

}

//单一结果查询

public static object GetSingleResult(string sql)

{

  SqlConnection conn=new SqlConnection(connString);

  SqlCommand cmd=new SqlCommand(sql,conn);

  try

  {

     conn.Open();

    return cmd.ExecuteScalar();

  }

  catch(Exception ex)

  {

    throw ex;

  }

  finally

  {

    conn.Close();

  }

}

//只读结果集查询

public static SqlDataReader GetReader(string sql)

{

  SqlConnection conn=new SqlConnection(connString);

  SqlCommand cmd=new SqlCommand(sql,conn);

  try

  {

     conn.Open();

    return cmd.ExecuteReader(CommandBehavior.CloseConnection);

  }

  catch(Exception ex)

  {

    conn.Close();

    throw ex;

  }

}

//DataSet数据集查询

public static DataSet GetDataSet(string sql)

{

  SqlConnection conn=new SqlConnection(connString);

  SqlCommand cmd=new SqlCommand(sql,conn);

  DataSet ds=new DataSet();

  SqlDataAdapter da=new SqlDataAdapter();

  try

  {

     conn.Open();

    da.Fill(ds);

    return ds;

  }

  catch(Exception ex)

  {

    throw ex;

  }

  finally

  {

    conn.Close();

  }

}

2.带参数的sql语句

//执行增删改查

public static int Update(string sql,SqlParameter[] param)

{

  SqlConnection conn=new SqlConnection(connString);

  SqlCommand cmd=new SqlCommand(sql,conn);

  try

  {

    conn.Open();

    cmd.Parameters.AddRange(param);

    return cmd.ExecuteNonQuery();

  }

  catch(Exception ex)

  {

    throw ex;

  }

  finally

  {

    conn.Close();

  }

}

//查询单一结果

public static object GetSingleResult(string sql,SqlParameter[] param)

{

  SqlConnection conn=new SqlConnection(connString);

  SqlCommand cmd=new SqlCommand(sql,conn);

  try

  {

     conn.Open();

    cmd.Parameters.AddRange(param);

    retrurn cmd.ExecuteScalar();

  }

  catch(Exception ex)

  {

     throw ex;

  }

  finally

  {

    conn.Close();

  }

}

//查询只读结果集

public static SqlDataReader GetReader(string sql,SqlParameter[] param)

{

  SqlConnection conn=new SqlConnection(connString);

  SqlCommand cmd=new SqlCommand(sql,conn);

  try

  {

     conn.Open();

    cmd.Parameters.AddRange(param);

    return cmd.ExecuteReader(CommandBehavior.CloseConnection);

  }

  catch(Exception ex)

  {

     conn.Close();

    throw ex;

  }

}

3.带参数的存储过程

//执行增删改查

public static int UpdateByProcedure(string procedureName,SqlParameter[] param)

{

  SqlConnection conn=new SqlConnection(connString);

  SqlCommand cmd=new SqlCommand();

  cmd.Connection=conn;

  try

  {

    conn.Open();

    cmd.CommandType=CommandType.StoredProcedure;

    cmd.CommandText=procedureName;

    cmd.Parameters.AddRange(param);

    return cmd.ExecuteNonQuery();

  }

  catch(Exception ex)

  {

    throw ex;

  }

  finally

  {

    conn.Close();

  }

}

//查询单一结果

public static object GetSingleResultByProcedure(string procedureName,SqlParameter[] param)

{

  SqlConnection conn=new SqlConnection(connString);

  SqlCommand cmd=new SqlCommand();

  cmd.Connection=conn;

  try

  {

     conn.Open();

    cmd.CommandType=CommandType.StoredProcedure;

    cmd.CommandText=procedureName;

    cmd.Parameters.AddRange(param);

    retrurn cmd.ExecuteScalar();

  }

  catch(Exception ex)

  {

     throw ex;

  }

  finally

  {

    conn.Close();

  }

}

//查询只读结果集

public static SqlDataReader GetReaderByProcedure(string procedureName,SqlParameter[] param)

{

  SqlConnection conn=new SqlConnection(connString);

  SqlCommand cmd=new SqlCommand();

  cmd.Connection=conn;

  try

  {

     conn.Open();

    cmd.CommandType=CommandType.StoredProcedure;

    cmd.CommandText=procedureName;

    cmd.Parameters.AddRange(param);

    return cmd.ExecuteReader(CommandBehavior.CloseConnection);

  }

  catch(Exception ex)

  {

     conn.Close();

    throw ex;

  }

}

4.使用事务更新

public static bool UpdateByTracsaction(List<string> sqlList)

{

  SqlConnection conn=new SqlConnection(connString);

  SqlCommand cmd=new SqlCommand();

  cmd.Connection=conn;

  try

  {

    conn.Open();

    cmd.Transaction=conn.BeginTransaction();//开启事务

    foreach(string sql in sqlList)

    {

      cmd.CommandText=sql;

      cmd.ExecuteNonQuery();

    }

    cmd.Transaction.Commit();//提交事务

    return true;

  }

  catch(Exception ex)

  {

    if(cmd.Transaction!=null)

    {

      cmd.Transaction.Rollback();//回滚事务

    }

    throw ex;

  }

  finally

  {

    if(cmd.Transaction!=null)

    {

      cmd.Transaction=null;//清空事务

    }

    conn.Close();

  }

}

以上是关于SQLServer数据库通用访问类的主要内容,如果未能解决你的问题,请参考以下文章

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段

Eclipse 中的通用代码片段或模板

YBB.DBUtils用法

缺少 SQL SERVER 2014 代码片段

从片段访问父活动的数据

sqlserver数据库访问帮助类