当前位置: 编程技术>.net/c#/asp.net
c# 数据库的 sql 参数封装类的编写
来源: 互联网 发布时间:2014-10-13
本文导语: 数据库的 sql 参数封装类的编写 代码如下:using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlContro...
数据库的 sql 参数封装类的编写
代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
namespace ChinaSite.classes
{
public class DbAccess
{
SqlConnection conn = null;
SqlCommand cmd = null;
public DbAccess()
{
//
// TODO: 在此处添加构造函数逻辑
//
conn = new SqlConnection();
//conn.ConnectionString = "initial catalog=pubs;data source=.;user id=sa;password=";
//conn.ConnectionString = Convert.ToString(System.Configuration.ConfigurationSettings.AppSettings["datasource"]);
conn.ConnectionString = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["datasource"]);
cmd = new SqlCommand();
cmd.Connection = conn;
}
///
/// 获取数据根据sql语句
///
///
///
public DataTable GetTable(string sql)
{
DataSet ds = new DataSet();
try
{
cmd.CommandText = sql;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return null;
}
return ds.Tables[0] ?? new DataTable();
}
///
/// 获取数据根据sql语句 带参数 的
///
///
///
///
public DataTable GetTable(string sql, params SqlParameter[] pas)
{
DataSet ds = new DataSet();
try
{
cmd.CommandText = sql;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
cmd.Parameters.Clear();
foreach (SqlParameter temppa in pas)
{
cmd.Parameters.Add(temppa);
}
da.Fill(ds);
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return null;
}
return ds.Tables[0] ?? new DataTable();
}
///
/// 根据sql语句返回跟新状态
///
///
///
public bool GetState(string sql)
{
bool succ = false;
try
{
cmd.CommandText = sql;
conn.Open();
succ = cmd.ExecuteNonQuery() > 0 ? (true) : (false);
conn.Close();
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return false;
}
return succ;
}
///
/// 根据sql语句返回跟新状态带参数的
///
/// sql语句
/// 参数的集合
///
public bool GetState(string sql, params SqlParameter[] pas)
{
bool succ = false;
try
{
cmd.CommandText = sql;
cmd.Parameters.Clear();
foreach (SqlParameter temppa in pas)
{
cmd.Parameters.Add(temppa);
}
conn.Open();
succ = cmd.ExecuteNonQuery() > 0 ? (true) : (false);
conn.Close();
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return false;
}
return succ;
}
///
/// 根据sql语句返回第一个单元格的数据
///
///
///
public string GetOne(string sql)
{
string res = "";
try
{
cmd.CommandText = sql;
conn.Open();
res = cmd.ExecuteScalar() == null ? ("") : (Convert.ToString(cmd.ExecuteScalar()));
conn.Close();
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return null;
}
return res;
}
///
/// 根据sql语句返回第一个单元格的数据带参数的
///
///
///
///
public string GetOne(string sql, params SqlParameter[] pas)
{
string res = "";
try
{
cmd.CommandText = sql;
cmd.Parameters.Clear();
foreach (SqlParameter temppa in pas)
{
cmd.Parameters.Add(temppa);
}
conn.Open();
res = cmd.ExecuteScalar() == null ? ("") : (Convert.ToString(cmd.ExecuteScalar()));
conn.Close();
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return null;
}
return res;
}
///
/// 返回数据的DataReader
///
///
///
public SqlDataReader GetDataReader(string sql)
{
SqlDataReader dr = null;
try
{
conn.Open();
cmd.CommandText = sql;
dr = cmd.ExecuteReader();
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return null;
}
return dr;
}
///
/// 返回数据的DataReader带参数的
///
///
///
///
public SqlDataReader GetDataReader(string sql, params SqlParameter[] pas)
{
SqlDataReader dr = null;
try
{
conn.Open();
cmd.Parameters.Clear();
foreach (SqlParameter temppa in pas)
{
cmd.Parameters.Add(temppa);
}
cmd.CommandText = sql;
dr = cmd.ExecuteReader();
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return null;
}
return dr;
}
///
/// 打开连接
///
public void OpenConn()
{
if (conn.State != ConnectionState.Open)
{
try
{
conn.Open();
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return;
}
}
}
///
/// 关闭连接
///
public void CloseConn()
{
if (conn.State != ConnectionState.Closed)
{
try
{
conn.Close();
cmd = null;
conn = null;
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return;
}
}
}
///
/// 弹出错误的信息
///
///
public void ShowError(string err)
{
System.Web.HttpContext.Current.Response.Write(Script(err, ""));
}
///
/// 显示信息
///
///
public void ShowMessage(string mes, string loc)
{
System.Web.HttpContext.Current.Response.Write(Script(mes, loc));
}
///
/// javascript脚本
///
///
///
///
public string Script(string mess, string loc)
{
StringBuilder sb = new StringBuilder();
sb.Append("");
sb.Append("alter('");
sb.Append(mess);
sb.Append("');");
sb.Append(loc);
sb.Append("");
return sb.ToString();
}
}
}