SQLHelper收集(一)
来源:互联网 发布:ubuntu安装网页播放器 编辑:程序博客网 时间:2024/06/08 08:32
- 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.ComponentModel;
- using System.Collections;
- using System.Diagnostics;
- using System.Data.SqlClient;
- using System.Configuration;
- namespace Union.SQLHelper
- {
-
-
-
-
-
-
-
-
- public class Database : IDisposable
- {
-
- 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)
- {
- 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);
- dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
-
- }
-
-
-
-
-
-
- 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["ConnectionString"]);
- }
- if (con.State == System.Data.ConnectionState.Closed)
- 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 MakeReturnParam(string ParamName, SqlDbType DbType, int Size)
- {
- return MakeParam(ParamName, DbType, Size, ParameterDirection.ReturnValue, 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;
- }
- }
- }