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 con;
// 不帶參數的輸入存儲過程-----默認爲輸入存儲過程
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
// 帶參數的輸入存儲過程
public int RunProc(string procName, SqlParameter[] prams)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["createCon"]);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
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)
{
// SqlDataReader dataReader=null;
SqlCommand cmd = CreateCommand(procName, prams);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
public int RunProcduce(string procName, SqlParameter[] prams)
{
int DeliverNameID = 0;
SqlCommand cmd = CreateCommand(procName, prams);
DeliverNameID = Convert.ToInt32(cmd.ExecuteScalar());
return DeliverNameID;
}
// 帶輸出參數的存儲過程------返回 DataTable
public void RunProc(string procName, SqlParameter[] prams, out DataTable dt)
{
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = new SqlCommand();
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
mySqlDataAdapter.SelectCommand.Parameters.Add(parameter);
}
}
dt = new DataTable();
Open();
mySqlDataAdapter.SelectCommand.Connection = con;
mySqlDataAdapter.SelectCommand.CommandText = procName;
mySqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
mySqlDataAdapter.Fill(dt);
}
//帶輸出參數的存儲過程------返回 DataSet
public void RunProc(string procName, SqlParameter[] prams, out DataSet ds)
{
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = new SqlCommand();
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
mySqlDataAdapter.SelectCommand.Parameters.Add(parameter);
}
}
ds = new DataSet();
Open();
mySqlDataAdapter.SelectCommand.Connection = con;
mySqlDataAdapter.SelectCommand.CommandText = procName;
mySqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
mySqlDataAdapter.Fill(ds, "Tab");
}
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
Open();
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
// 添加存儲過程參數
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
// 返回參數
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return cmd;
}
// 打開數據庫連結
private void Open()
{
if (con == null)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["createCon"]);
con.Open();
}
}
// 關閉數據庫連結
public void Close()
{
if (con != null)
{
con.Close();
}
}
// 釋放佔有的資源
public void Dispose()
{
if (con != null)
{
con.Dispose();
con = null;
}
}
// 創造輸入存儲過程
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
// 創造輸出存儲過程
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
// 判斷存儲過程是輸入還是輸出
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;
}
}
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 con;
// 不帶參數的輸入存儲過程-----默認爲輸入存儲過程
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
// 帶參數的輸入存儲過程
public int RunProc(string procName, SqlParameter[] prams)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["createCon"]);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
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)
{
// SqlDataReader dataReader=null;
SqlCommand cmd = CreateCommand(procName, prams);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
public int RunProcduce(string procName, SqlParameter[] prams)
{
int DeliverNameID = 0;
SqlCommand cmd = CreateCommand(procName, prams);
DeliverNameID = Convert.ToInt32(cmd.ExecuteScalar());
return DeliverNameID;
}
// 帶輸出參數的存儲過程------返回 DataTable
public void RunProc(string procName, SqlParameter[] prams, out DataTable dt)
{
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = new SqlCommand();
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
mySqlDataAdapter.SelectCommand.Parameters.Add(parameter);
}
}
dt = new DataTable();
Open();
mySqlDataAdapter.SelectCommand.Connection = con;
mySqlDataAdapter.SelectCommand.CommandText = procName;
mySqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
mySqlDataAdapter.Fill(dt);
}
//帶輸出參數的存儲過程------返回 DataSet
public void RunProc(string procName, SqlParameter[] prams, out DataSet ds)
{
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = new SqlCommand();
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
mySqlDataAdapter.SelectCommand.Parameters.Add(parameter);
}
}
ds = new DataSet();
Open();
mySqlDataAdapter.SelectCommand.Connection = con;
mySqlDataAdapter.SelectCommand.CommandText = procName;
mySqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
mySqlDataAdapter.Fill(ds, "Tab");
}
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
Open();
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
// 添加存儲過程參數
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
// 返回參數
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return cmd;
}
// 打開數據庫連結
private void Open()
{
if (con == null)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["createCon"]);
con.Open();
}
}
// 關閉數據庫連結
public void Close()
{
if (con != null)
{
con.Close();
}
}
// 釋放佔有的資源
public void Dispose()
{
if (con != null)
{
con.Dispose();
con = null;
}
}
// 創造輸入存儲過程
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
// 創造輸出存儲過程
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
// 判斷存儲過程是輸入還是輸出
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;
}
}