-
C#教程之c# 数据库的 sql 参数封装类的编写
数据库的 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;
}
/// <summary>
/// 获取数据根据sql语句
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
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();
}
/// <summary>
/// 获取数据根据sql语句 带参数 的
/// </summary>
/// <param name="sql"></param>
/// <param name="pas"></param>
/// <returns></returns>
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();
}
/// <summary>
/// 根据sql语句返回跟新状态
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 根据sql语句返回跟新状态带参数的
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="pas">参数的集合</param>
/// <returns></returns>
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;
}
/// <summary>
/// 根据sql语句返回第一个单元格的数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 根据sql语句返回第一个单元格的数据带参数的
/// </summary>
/// <param name="sql"></param>
/// <param name="pas"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 返回数据的DataReader
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 返回数据的DataReader带参数的
/// </summary>
/// <param name="sql"></param>
/// <param name="pas"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 打开连接
/// </summary>
public void OpenConn()
{
if (conn.State != ConnectionState.Open)
{
try
{
conn.Open();
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return;
}
}
}
/// <summary>
/// 关闭连接
/// </summary>
public void CloseConn()
{
if (conn.State != ConnectionState.Closed)
{
try
{
conn.Close();
cmd = null;
conn = null;
}
catch (Exception ex)
{
this.ShowError(ex.Message);
return;
}
}
}
/// <summary>
/// 弹出错误的信息
/// </summary>
/// <param name="err"></param>
public void ShowError(string err)
{
System.Web.HttpContext.Current.Response.Write(Script(err, ""));
}
/// <summary>
/// 显示信息
/// </summary>
/// <param name="err"></param>
public void ShowMessage(string mes, string loc)
{
System.Web.HttpContext.Current.Response.Write(Script(mes, loc));
}
/// <summary>
/// javascript脚本
/// </summary>
/// <param name="mess"></param>
/// <param name="loc"></param>
/// <returns></returns>
public string Script(string mess, string loc)
{
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("alter('");
sb.Append(mess);
sb.Append("');");
sb.Append(loc);
sb.Append("</script>");
return sb.ToString();
}
}
}
最新更新
Objective-C语法之代码块(block)的使用
VB.NET eBook
Add-in and Automation Development In VB.NET 2003 (F
Add-in and Automation Development In VB.NET 2003 (8
Add-in and Automation Development in VB.NET 2003 (6
Add-in and Automation Development In VB.NET 2003 (5
AddIn Automation Development In VB.NET 2003 (4)
AddIn And Automation Development In VB.NET 2003 (2)
Addin and Automation Development In VB.NET 2003 (3)
AddIn And Automation Development In VB.NET 2003 (1)
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
武装你的WEBAPI-OData入门
武装你的WEBAPI-OData便捷查询
武装你的WEBAPI-OData分页查询
武装你的WEBAPI-OData资源更新Delta
5. 武装你的WEBAPI-OData使用Endpoint 05-09
武装你的WEBAPI-OData之API版本管理
武装你的WEBAPI-OData常见问题
武装你的WEBAPI-OData聚合查询
OData WebAPI实践-OData与EDM
OData WebAPI实践-Non-EDM模式