-
-
-
-
-
-
-
-
- using System;
- using System.Collections;
- using System.Collections.Specialized;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- using System.Data.Common;
- using System.Collections.Generic;
- namespace Legalsoft.Wizard.DBUtility
- {
- public enum EffentNextType
- {
-
-
-
- None,
-
-
-
- WhenHaveContine,
-
-
-
- WhenNoHaveContine,
-
-
-
- ExcuteEffectRows,
-
-
-
- SolicitationEvent
- }
- public class CommandInfo
- {
- public object ShareObject = null;
- public object OriginalData = null;
- event EventHandler _solicitationEvent;
- public event EventHandler SolicitationEvent
- {
- add
- {
- _solicitationEvent += value;
- }
- remove
- {
- _solicitationEvent -= value;
- }
- }
- public void OnSolicitationEvent()
- {
- if (_solicitationEvent != null)
- {
- _solicitationEvent(this,new EventArgs());
- }
- }
- public string CommandText;
- public System.Data.Common.DbParameter[] Parameters;
- public EffentNextType EffentNextType = EffentNextType.None;
- public CommandInfo()
- {
- }
- public CommandInfo(string sqlText, SqlParameter[] para)
- {
- this.CommandText = sqlText;
- this.Parameters = para;
- }
- public CommandInfo(string sqlText, SqlParameter[] para, EffentNextType type)
- {
- this.CommandText = sqlText;
- this.Parameters = para;
- this.EffentNextType = type;
- }
- }
-
-
-
-
- public abstract class SQLHelper
- {
-
- public static string connectionString = "";
- public SQLHelper()
- {
- connectionString = @"DATA SOURCE=(local);UID=sa;PWD=111111;DATABASE=LegalSoft";
- }
- #region 公用方法
-
-
-
-
-
- public object LoadImage(int id)
- {
- SqlConnection myConnection = new SqlConnection(connectionString);
- SqlCommand myCommand = new SqlCommand("SELECT [bImage] FROM [tblCAIPIAO_NEWS_IMG] WHERE id=@id", myConnection);
- myCommand.CommandType = CommandType.Text;
- myCommand.Parameters.Add(new SqlParameter("@id", id));
- myConnection.Open();
- object result = myCommand.ExecuteScalar();
- try
- {
- return new System.IO.MemoryStream((byte[])result);
- }
- catch (ArgumentNullException e)
- {
- return null;
- }
- finally
- {
- myConnection.Close();
- }
- }
-
-
-
-
-
-
- public static bool ColumnExists(string tableName, string columnName)
- {
- string sql = "select count(1) from syscolumns where [id]=object_id("" + tableName + "") and [name]="" + columnName + """;
- object res = GetSingle(sql);
- if (res == null)
- {
- return false;
- }
- return Convert.ToInt32(res) > 0;
- }
- public static int GetMaxID(string FieldName, string TableName)
- {
- string strsql = "select max(" + FieldName + ")+1 from " + TableName;
- object obj = SQLHelper.GetSingle(strsql);
- if (obj == null)
- {
- return 1;
- }
- else
- {
- return int.Parse(obj.ToString());
- }
- }
- public static bool Exists(string strSql)
- {
- object obj = SQLHelper.GetSingle(strSql);
- int cmdresult;
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- cmdresult = 0;
- }
- else
- {
- cmdresult = int.Parse(obj.ToString());
- }
- if (cmdresult == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
-
-
-
-
-
- public static bool TabExists(string TableName)
- {
- string strsql = "select count(*) from sysobjects where id = object_id(N"[" + TableName + "]") and OBJECTPROPERTY(id, N"IsUserTable") = 1";
-
- object obj = SQLHelper.GetSingle(strsql);
- int cmdresult;
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- cmdresult = 0;
- }
- else
- {
- cmdresult = int.Parse(obj.ToString());
- }
- if (cmdresult == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public static bool Exists(string strSql, params SqlParameter[] cmdParms)
- {
- object obj = SQLHelper.GetSingle(strSql, cmdParms);
- int cmdresult;
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- cmdresult = 0;
- }
- else
- {
- cmdresult = int.Parse(obj.ToString());
- }
- if (cmdresult == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- #endregion
- #region 执行简单SQL语句
-
-
-
-
-
- public static int ExecuteSql(string SQLString)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- using (SqlCommand cmd = new SqlCommand(SQLString, connection))
- {
- try
- {
- connection.Open();
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- connection.Close();
- throw e;
- }
- }
- }
- }
- public static int ExecuteSqlByTime(string SQLString, int Times)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- using (SqlCommand cmd = new SqlCommand(SQLString, connection))
- {
- try
- {
- connection.Open();
- cmd.CommandTimeout = Times;
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- connection.Close();
- throw e;
- }
- }
- }
- }
-
-
-
-
-
-
- public static int ExecuteSqlTran(List<CommandInfo> list, List<CommandInfo> oracleCmdSqlList)
- {
- using (SqlConnection conn = new SqlConnection(connectionString))
- {
- conn.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- SqlTransaction tx = conn.BeginTransaction();
- cmd.Transaction = tx;
- try
- {
- foreach (CommandInfo myDE in list)
- {
- string cmdText = myDE.CommandText;
- SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters;
- PrepareCommand(cmd, conn, tx, cmdText, cmdParms);
- if (myDE.EffentNextType == EffentNextType.SolicitationEvent)
- {
- if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
- {
- tx.Rollback();
- throw new Exception("违背要求" + myDE.CommandText + "必须符合select count(..的格式");
-
- }
- object obj = cmd.ExecuteScalar();
- bool isHave = false;
- if (obj == null && obj == DBNull.Value)
- {
- isHave = false;
- }
- isHave = Convert.ToInt32(obj) > 0;
- if (isHave)
- {
-
- myDE.OnSolicitationEvent();
- }
- }
- if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
- {
- if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
- {
- tx.Rollback();
- throw new Exception("SQL:违背要求" + myDE.CommandText + "必须符合select count(..的格式");
-
- }
- object obj = cmd.ExecuteScalar();
- bool isHave = false;
- if (obj == null && obj == DBNull.Value)
- {
- isHave = false;
- }
- isHave = Convert.ToInt32(obj) > 0;
- if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
- {
- tx.Rollback();
- throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须大于0");
-
- }
- if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
- {
- tx.Rollback();
- throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须等于0");
-
- }
- continue;
- }
- int val = cmd.ExecuteNonQuery();
- if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
- {
- tx.Rollback();
- throw new Exception("SQL:违背要求" + myDE.CommandText + "必须有影响行");
-
- }
- cmd.Parameters.Clear();
- }
-
-
-
-
-
-
-
-
- tx.Commit();
- return 1;
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- tx.Rollback();
- throw e;
- }
- catch (Exception e)
- {
- tx.Rollback();
- throw e;
- }
- }
- }
-
-
-
-
- public static int ExecuteSqlTran(List<String> SQLStringList)
- {
- using (SqlConnection conn = new SqlConnection(connectionString))
- {
- conn.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- SqlTransaction tx = conn.BeginTransaction();
- cmd.Transaction = tx;
- try
- {
- int count = 0;
- for (int n = 0; n < SQLStringList.Count; n++)
- {
- string strsql = SQLStringList[n];
- if (strsql.Trim().Length > 1)
- {
- cmd.CommandText = strsql;
- count += cmd.ExecuteNonQuery();
- }
- }
- tx.Commit();
- return count;
- }
- catch
- {
- tx.Rollback();
- return 0;
- }
- }
- }
-
-
-
-
-
-
- public static int ExecuteSql(string SQLString, string content)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- SqlCommand cmd = new SqlCommand(SQLString, connection);
- System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
- myParameter.Value = content;
- cmd.Parameters.Add(myParameter);
- try
- {
- connection.Open();
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- throw e;
- }
- finally
- {
- cmd.Dispose();
- connection.Close();
- }
- }
- }
-
-
-
-
-
-
- public static object ExecuteSqlGet(string SQLString, string content)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- SqlCommand cmd = new SqlCommand(SQLString, connection);
- System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
- myParameter.Value = content;
- cmd.Parameters.Add(myParameter);
- try
- {
- connection.Open();
- object obj = cmd.ExecuteScalar();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- throw e;
- }
- finally
- {
- cmd.Dispose();
- connection.Close();
- }
- }
- }
-
-
-
-
-
-
- public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- SqlCommand cmd = new SqlCommand(strSQL, connection);
- System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Image);
- myParameter.Value = fs;
- cmd.Parameters.Add(myParameter);
- try
- {
- connection.Open();
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- throw e;
- }
- finally
- {
- cmd.Dispose();
- connection.Close();
- }
- }
- }
-
-
-
-
-
- public static object GetSingle(string SQLString)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- using (SqlCommand cmd = new SqlCommand(SQLString, connection))
- {
- try
- {
- connection.Open();
- object obj = cmd.ExecuteScalar();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- connection.Close();
- throw e;
- }
- }
- }
- }
- public static object GetSingle(string SQLString, int Times)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- using (SqlCommand cmd = new SqlCommand(SQLString, connection))
- {
- try
- {
- connection.Open();
- cmd.CommandTimeout = Times;
- object obj = cmd.ExecuteScalar();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- connection.Close();
- throw e;
- }
- }
- }
- }
-
-
-
-
-
- public static SqlDataReader ExecuteReader(string strSQL)
- {
- SqlConnection connection = new SqlConnection(connectionString);
- SqlCommand cmd = new SqlCommand(strSQL, connection);
- try
- {
- connection.Open();
- SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- return myReader;
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- throw e;
- }
- }
-
-
-
-
-
- public static DataSet Query(string SQLString)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- DataSet ds = new DataSet();
- try
- {
- connection.Open();
- SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
- command.Fill(ds, "ds");
- }
- catch (System.Data.SqlClient.SqlException ex)
- {
- throw new Exception(ex.Message);
- }
- return ds;
- }
- }
-
-
-
-
-
-
- public static DataSet Query(string SQLString, int Times)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- DataSet ds = new DataSet();
- try
- {
- connection.Open();
- SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
- command.SelectCommand.CommandTimeout = Times;
- command.Fill(ds, "ds");
- }
- catch (System.Data.SqlClient.SqlException ex)
- {
- throw new Exception(ex.Message);
- }
- return ds;
- }
- }
- #endregion
- #region 执行带参数的SQL语句
-
-
-
-
-
- public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- using (SqlCommand cmd = new SqlCommand())
- {
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- int rows = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- return rows;
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- throw e;
- }
- }
- }
- }
-
-
-
-
- public static void ExecuteSqlTran(Hashtable SQLStringList)
- {
- using (SqlConnection conn = new SqlConnection(connectionString))
- {
- conn.Open();
- using (SqlTransaction trans = conn.BeginTransaction())
- {
- SqlCommand cmd = new SqlCommand();
- try
- {
-
- foreach (DictionaryEntry myDE in SQLStringList)
- {
- string cmdText = myDE.Key.ToString();
- SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
- PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
- int val = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- }
- trans.Commit();
- }
- catch
- {
- trans.Rollback();
- throw;
- }
- }
- }
- }
-
-
-
-
- public static int ExecuteSqlTran(System.Collections.Generic.List<CommandInfo> cmdList)
- {
- using (SqlConnection conn = new SqlConnection(connectionString))
- {
- conn.Open();
- using (SqlTransaction trans = conn.BeginTransaction())
- {
- SqlCommand cmd = new SqlCommand();
- try
- {
- int count = 0;
-
- foreach (CommandInfo myDE in cmdList)
- {
- string cmdText = myDE.CommandText;
- SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters;
- PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
- if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
- {
- if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
- {
- trans.Rollback();
- return 0;
- }
- object obj = cmd.ExecuteScalar();
- bool isHave = false;
- if (obj == null && obj == DBNull.Value)
- {
- isHave = false;
- }
- isHave = Convert.ToInt32(obj) > 0;
- if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
- {
- trans.Rollback();
- return 0;
- }
- if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
- {
- trans.Rollback();
- return 0;
- }
- continue;
- }
- int val = cmd.ExecuteNonQuery();
- count += val;
- if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
- {
- trans.Rollback();
- return 0;
- }
- cmd.Parameters.Clear();
- }
- trans.Commit();
- return count;
- }
- catch
- {
- trans.Rollback();
- throw;
- }
- }
- }
- }
-
-
-
-
- public static void ExecuteSqlTranWithIndentity(System.Collections.Generic.List<CommandInfo> SQLStringList)
- {
- using (SqlConnection conn = new SqlConnection(connectionString))
- {
- conn.Open();
- using (SqlTransaction trans = conn.BeginTransaction())
- {
- SqlCommand cmd = new SqlCommand();
- try
- {
- int indentity = 0;
-
- foreach (CommandInfo myDE in SQLStringList)
- {
- string cmdText = myDE.CommandText;
- SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters;
- foreach (SqlParameter q in cmdParms)
- {
- if (q.Direction == ParameterDirection.InputOutput)
- {
- q.Value = indentity;
- }
- }
- PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
- int val = cmd.ExecuteNonQuery();
- foreach (SqlParameter q in cmdParms)
- {
- if (q.Direction == ParameterDirection.Output)
- {
- indentity = Convert.ToInt32(q.Value);
- }
- }
- cmd.Parameters.Clear();
- }
- trans.Commit();
- }
- catch
- {
- trans.Rollback();
- throw;
- }
- }
- }
- }
-
-
-
-
- public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList)
- {
- using (SqlConnection conn = new SqlConnection(connectionString))
- {
- conn.Open();
- using (SqlTransaction trans = conn.BeginTransaction())
- {
- SqlCommand cmd = new SqlCommand();
- try
- {
- int indentity = 0;
-
- foreach (DictionaryEntry myDE in SQLStringList)
- {
- string cmdText = myDE.Key.ToString();
- SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
- foreach (SqlParameter q in cmdParms)
- {
- if (q.Direction == ParameterDirection.InputOutput)
- {
- q.Value = indentity;
- }
- }
- PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
- int val = cmd.ExecuteNonQuery();
- foreach (SqlParameter q in cmdParms)
- {
- if (q.Direction == ParameterDirection.Output)
- {
- indentity = Convert.ToInt32(q.Value);
- }
- }
- cmd.Parameters.Clear();
- }
- trans.Commit();
- }
- catch
- {
- trans.Rollback();
- throw;
- }
- }
- }
- }
-
-
-
-
-
- public static object GetSingle(string SQLString, params SqlParameter[] cmdParms)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- using (SqlCommand cmd = new SqlCommand())
- {
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- object obj = cmd.ExecuteScalar();
- cmd.Parameters.Clear();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- throw e;
- }
- }
- }
- }
-
-
-
-
-
- public static SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms)
- {
- SqlConnection connection = new SqlConnection(connectionString);
- SqlCommand cmd = new SqlCommand();
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- cmd.Parameters.Clear();
- return myReader;
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- throw e;
- }
-
-
-
-
-
- }
-
-
-
-
-
- public static DataSet Query(string SQLString, params SqlParameter[] cmdParms)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- SqlCommand cmd = new SqlCommand();
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- using (SqlDataAdapter da = new SqlDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- try
- {
- da.Fill(ds, "ds");
- cmd.Parameters.Clear();
- }
- catch (System.Data.SqlClient.SqlException ex)
- {
- throw new Exception(ex.Message);
- }
- return ds;
- }
- }
- }
- private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
- {
- if (conn.State != ConnectionState.Open)
- conn.Open();
- cmd.Connection = conn;
- cmd.CommandText = cmdText;
- if (trans != null)
- cmd.Transaction = trans;
- cmd.CommandType = CommandType.Text;
- if (cmdParms != null)
- {
- foreach (SqlParameter parameter in cmdParms)
- {
- if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
- (parameter.Value == null))
- {
- parameter.Value = DBNull.Value;
- }
- cmd.Parameters.Add(parameter);
- }
- }
- }
- #endregion
- #region 存储过程操作
-
-
-
-
-
-
- public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
- {
- SqlConnection connection = new SqlConnection(connectionString);
- SqlDataReader returnReader;
- connection.Open();
- SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
- command.CommandType = CommandType.StoredProcedure;
- returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
- return returnReader;
- }
-
-
-
-
-
-
-
- public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- DataSet dataSet = new DataSet();
- connection.Open();
- SqlDataAdapter sqlDA = new SqlDataAdapter();
- sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
- sqlDA.Fill(dataSet, tableName);
- connection.Close();
- return dataSet;
- }
- }
- public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- DataSet dataSet = new DataSet();
- connection.Open();
- SqlDataAdapter sqlDA = new SqlDataAdapter();
- sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
- sqlDA.SelectCommand.CommandTimeout = Times;
- sqlDA.Fill(dataSet, tableName);
- connection.Close();
- return dataSet;
- }
- }
-
-
-
-
-
-
-
- private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
- {
- SqlCommand command = new SqlCommand(storedProcName, connection);
- command.CommandType = CommandType.StoredProcedure;
- foreach (SqlParameter parameter in parameters)
- {
- if (parameter != null)
- {
-
- if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
- (parameter.Value == null))
- {
- parameter.Value = DBNull.Value;
- }
- command.Parameters.Add(parameter);
- }
- }
- return command;
- }
-
-
-
-
-
-
-
- public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- int result;
- connection.Open();
- SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
- rowsAffected = command.ExecuteNonQuery();
- result = (int)command.Parameters["ReturnValue"].Value;
-
- return result;
- }
- }
-
-
-
-
-
-
- private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
- {
- SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
- command.Parameters.Add(new SqlParameter("ReturnValue",
- SqlDbType.Int, 4, ParameterDirection.ReturnValue,
- false, 0, 0, string.Empty, DataRowVersion.Default, null));
- return command;
- }
- #endregion
- }
- }
posted on
2010-07-02 17:51
冰云
阅读(
684)
评论()
编辑
收藏
举报