数据库类
来源:互联网 发布:吉林大数据在职研究生 编辑:程序博客网 时间:2024/05/21 18:35
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
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.Security.Cryptography;
namespace Components
{
public class DataBase:IDisposable
{
private SqlConnection con;
/// <summary>
/// Run stored procedure.
/// </summary>
/// <param name="procName">存储过程的名字.</param>
/// <returns>执行存储过程并返回值.</returns>
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// Run stored procedure.
/// </summary>
/// <param name="procName">存储过程的名字.</param>
/// <param name="prams">存储过程 params.</param>
/// <returns>执行存储过程并返回值.</returns>
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
//返回结果
public void RunProc(string procName, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, null);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
//执行存储过程,并返回值
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, prams);
if (cmd != null)
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
else
{
dataReader = null;
}
}
////执行存储过程,并返回值
public DataSet RunProcSet(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
SqlDataAdapter adpt = new SqlDataAdapter();
adpt.SelectCommand = cmd;
DataSet ds = new DataSet();
adpt.Fill(ds);
return ds;
}
//定义一个指令来调用存储过程
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
// 打开数据源
Open();
//command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) );
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
if (Components.GlobalFun.badword.checkBadword(prams) == false)
{
prams = null;
cmd = null;
}
// add proc parameters
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
// return param
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
}
return cmd;
}
/// <summary>
/// 连接数据库 shanguoming 03.14.2007
/// </summary>
public void Open1()
{
try
{
con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString_Community"]);
con.Open();
}
catch (Exception e)
{
Error.Log(e.Message.ToString());
}
}
/// <summary>
/// Open the connection.
/// </summary>
public void Open()
{
// open connection
if (con == null)
{
try
{
con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
con.Open();
}
catch (Exception e)
{
Error.Log(e.Message.ToString());
}
}
}
/// <summary>
/// Close the connection.
/// </summary>
public void Close()
{
if (con != null)
con.Close();
}
/// <summary>
/// Release resources.
/// </summary>
public void Dispose()
{
// make sure connection is closed
if (con != null)
{
con.Dispose();
con = null;
}
}
/// 输入 param.
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
/// <summary>
/// Make input param.
/// <returns>New parameter.</returns>
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
/// Make stored procedure param.
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if (Size > 0)
param = new SqlParameter(ParamName, DbType, Size);
else
param = new SqlParameter(ParamName, DbType);
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;
return param;
}
}
}
- 数据库类
- 数据库类
- 数据库类
- 数据库类
- 数据库类
- 数据库类
- 数据库类
- DbHelperSQL数据库类 数据库层
- 数据库操作类,可执行数据库简单操作
- 连接数据库的类
- ASP的数据库类
- 一个数据库操作类
- 数据库操作类!
- 数据库操作类
- 数据库操作类
- 数据库操作类
- 通用数据库访问类
- 数据库管理类
- 子框架中使用javascript更改当前浏览器窗口的url
- rs.open "select * from bbs",conn,1,1 参数说明
- 暂别pb
- C++中随机函数rand()和srand()的用法
- 使用gzip减少流量
- 数据库类
- 多线程处理:何时使用同步类
- WF从入门到精通(第十七章):关联及本地主机通信(下)
- Delphi 控制Excel
- 不公平的背后..
- C的复习
- Socket 类
- 64位系统上Jet 4.0未注册的解决方法
- Asp过滤指定html标签函数