asp net数据库操作类DBHelper

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace TicketDAL
{
public static class DBHelper2
{
private static SqlConnection connection;
///


/// 连接属性
///

public static SqlConnection Connection
{
get
{
/*
* Web.Config方法
*
*
*

*string connectionString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
*/
string connectionString = @"连接字符串";
if(connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if(connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if(connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
///
/// DataTable查询,无参数
///

///
///
public static DataTable GetTable(string safeSql)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql,Connection);
SqlDataAdapter sd = new SqlDataAdapter(cmd);
sd.Fill(ds);
return ds.Tables[0];
}
///
/// DataTable查询,有参数
///

///
///
///
public static DataTable GetTable(string safeSql, params SqlParameter[] values)
{
try
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
cmd.Parameters.AddRange(values);
SqlDataAdapter sd = new SqlDataAdapter(cmd);
sd.Fill(ds);
return ds.Tables[0];
}
catch (Exception e)
{
throw e;
}
finally
{
connection.Close();
}
}
///
/// 执行增删改方法,无参数,返回受影响行数
///

///
///
public static int ExcuteCommand(string safeSq

l)
{
try
{
SqlCommand command = new SqlCommand(safeSql, Connection);
return command.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
}
///


/// 执行增删改方法,有参数,返回受影响行数
///

///
///
///
public static int ExcuteCommand(string safeSql, params SqlParameter[] values)
{
try
{
SqlCommand command = new SqlCommand(safeSql, Connection);
command.Parameters.AddRange(values);
return command.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
}
///
/// 执行无参sql语句,返回SqlDataReader
///

///
///
public static SqlDataReader GetReader(string safeSql)
{
SqlCommand command = new SqlCommand(safeSql,Connection);
SqlDataReader reader = command.ExecuteReader();
return reader;
}
///
/// 执行有参sql语句,返回SqlDataReader
///

///
///
///
public static SqlDataReader GetReader(string safeSql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
///
/// 执行无参sql语句,返回受影响行数
///

///
///
public static int GetScalar(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
return Convert.ToInt32(cmd.ExecuteScalar());
}
///
/// 执行有参sql语句,返回受影响行数
///

///
///
///
public static int GetScalar(string safeSql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(safeSql,Connection);
cmd.Parameters.AddRange(values);
return Convert.ToInt32(cmd.ExecuteScalar());
}
}
}

相关文档
最新文档