一个通用的数据库类
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;
/// <summary>
/// DataBase 的摘要说明
/// </summary>
public class DataBase
{
private SqlConnection conn;
private SqlCommand sc;
private SqlDataAdapter sa;
//返回当前的连接,事务时使用
public SqlConnection Conn
{
get { return conn; }
}
public DataBase()
{
//当对象一但被创建则建立连接
this.conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get("conStr"));
this.conn.Open();
}
//关闭数据库连接
public void dis_connect()
{
if (conn != null)
conn.Close();
}
//查询数据库
public DataSet query(string cmd)
{
sc = new SqlCommand(cmd, conn); //用当前对象的连接建立数据命令
sa = new SqlDataAdapter(sc);
DataSet myds = new DataSet();
myds = new DataSet();
sa.Fill(myds); //执行查询命令,并将查询结果保存在DataSet中.
return myds;
}
//新增记录
public int update(string cmd)
{
sc = new SqlCommand(cmd, conn);
int re;
try
{
re = sc.ExecuteNonQuery(); //执行其它命令(insert,update,delete)
}
catch (Exception e)
{
Console.Write(e.Message);
re = -5; //数据库操作有异常
}
return re;
}
/*调用查询类的存储过程
* prceName: 存储过程名
* parames[]:参数数组
*/
public DataSet call_que_proce(string prceName,params SqlParameter[] parames)
{
sa = new SqlDataAdapter();
sc = new SqlCommand();
DataSet ds = new DataSet();
sc.Connection = conn;
sc.CommandText = prceName;
sc.CommandType = CommandType.StoredProcedure;
sa.SelectCommand = sc;
foreach (SqlParameter sp in parames)
{
sa.SelectCommand.Parameters.Add(sp);
}
sa.Fill(ds);
sc.Parameters.Clear();
sa.Dispose();
return ds;
}
/*调用非查询类的存储过程
* prceName
*/
public int call_noqeury_proce(string prceName, params SqlParameter[] parames)
{
sc = new SqlCommand();
int result = 0;
sc.Connection = conn;
sc.CommandText = prceName;
sc.CommandType = CommandType.StoredProcedure;
sa.SelectCommand = sc;
foreach (SqlParameter sp in parames)
{
sa.SelectCommand.Parameters.Add(sp);
}
result = sc.ExecuteNonQuery();
sc.Parameters.Clear();
return result;
}
}
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;
/// <summary>
/// DataBase 的摘要说明
/// </summary>
public class DataBase
{
private SqlConnection conn;
private SqlCommand sc;
private SqlDataAdapter sa;
//返回当前的连接,事务时使用
public SqlConnection Conn
{
get { return conn; }
}
public DataBase()
{
//当对象一但被创建则建立连接
this.conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get("conStr"));
this.conn.Open();
}
//关闭数据库连接
public void dis_connect()
{
if (conn != null)
conn.Close();
}
//查询数据库
public DataSet query(string cmd)
{
sc = new SqlCommand(cmd, conn); //用当前对象的连接建立数据命令
sa = new SqlDataAdapter(sc);
DataSet myds = new DataSet();
myds = new DataSet();
sa.Fill(myds); //执行查询命令,并将查询结果保存在DataSet中.
return myds;
}
//新增记录
public int update(string cmd)
{
sc = new SqlCommand(cmd, conn);
int re;
try
{
re = sc.ExecuteNonQuery(); //执行其它命令(insert,update,delete)
}
catch (Exception e)
{
Console.Write(e.Message);
re = -5; //数据库操作有异常
}
return re;
}
/*调用查询类的存储过程
* prceName: 存储过程名
* parames[]:参数数组
*/
public DataSet call_que_proce(string prceName,params SqlParameter[] parames)
{
sa = new SqlDataAdapter();
sc = new SqlCommand();
DataSet ds = new DataSet();
sc.Connection = conn;
sc.CommandText = prceName;
sc.CommandType = CommandType.StoredProcedure;
sa.SelectCommand = sc;
foreach (SqlParameter sp in parames)
{
sa.SelectCommand.Parameters.Add(sp);
}
sa.Fill(ds);
sc.Parameters.Clear();
sa.Dispose();
return ds;
}
/*调用非查询类的存储过程
* prceName
*/
public int call_noqeury_proce(string prceName, params SqlParameter[] parames)
{
sc = new SqlCommand();
int result = 0;
sc.Connection = conn;
sc.CommandText = prceName;
sc.CommandType = CommandType.StoredProcedure;
sa.SelectCommand = sc;
foreach (SqlParameter sp in parames)
{
sa.SelectCommand.Parameters.Add(sp);
}
result = sc.ExecuteNonQuery();
sc.Parameters.Clear();
return result;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述