[学习公共库]Database类 |
麦林 发表于 2007/5/31 21:43:29 | using System;using System.Data;using System.Data.SqlClient;using System.Configuration;using System.ComponentModel;using System.Diagnostics;using System.Collections;/// <summary>/// Data 的摘要说明/// </summary>public class Database : IDisposable { // connection to data source 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); }
public void RunProc(string procName, SqlParameter[] prams, out DataSet dataset) { SqlCommand cmd = CreateCommand(procName, prams); //create the DataAdapter SqlDataAdapter da = new SqlDataAdapter(cmd); //fill the DataSet using default values for DataTable names, etc. dataset = new DataSet(); da.Fill(dataset); this.Close(); }
public void RunProc(string procName, out DataSet dataset) { SqlCommand cmd = CreateCommand(procName, null); //create the DataAdapter SqlDataAdapter da = new SqlDataAdapter(cmd); //fill the DataSet using default values for DataTable names, etc. dataset = new DataSet(); da.Fill(dataset); this.Close(); }
private SqlCommand CreateCommand(string procName, SqlParameter[] prams) { // make sure connection is open Open();
//command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) ); SqlCommand cmd = new SqlCommand(procName, con); cmd.CommandType = CommandType.StoredProcedure;
// 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; }
private void Open() { // open connection if (con == null) { con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]); con.Open(); } }
public void Close() { if (con != null) con.Close(); }
public void Dispose() { // make sure connection is closed 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; } }
|
|
|

.: 公告
|
« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
|
.: 我的分类(专题)
|

.: 最新日志
.: 最新回复
|

blog名称:栗色?蓝色? 日志总数:449 评论数量:201 留言数量:37 访问次数:2260233 建立时间:2006年5月16日 |
|

.: 留言板
|

.: 链接
|

|