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;
}
}
}