源代码网整理以下我在用C#调用sql2005的存储过程,觉得设置参数太烦锁了,参数名、类型、宽度、输入/输出/返回值,每次都要设置,2、3个参数还好办,有时候达到十几、二十个,最多的一次40多个,代码又长又臭,人也累半死。
源代码网整理以下 为了摆脱这种体力劳动,我就尝试寻找一个简便的方法。在sql server数据库中有一些系统表,保存该库中所有的"对象",当然有存储过程名,参数名,宽度,类型等等。
源代码网整理以下请参见:SQL Server 2005中各个系统表的作用
源代码网整理以下执行一下这个sql:
源代码网整理以下SELECT B.[name], C.[name] AS [type], B.length, B.isoutparam, B.isnullable
FROM sysobjects AS A INNER JOIN";
syscolumns AS B ON A.id = B.id AND A.xtype = "P" AND A.name = "你的存储过程名" INNER JOIN
systypes C ON B.xtype = C.xtype AND C.[name] <> "sysname"
ORDER BY ROW_NUMBER() OVER (ORDER BY B.id), B.isoutparam
源代码网整理以下
看到结果了吧,此时此刻你是否有豁然开朗的感觉?
源代码网整理以下 正是源于此,我写了这个类,从此得到解放,本站正在使用中……:
源代码网整理以下using System;
using System.Collections;
using System.Text; 软件开发网 www.mscto.com
源代码网整理以下using System.Data.SqlClient;
using System.Data;
using System.Xml;
源代码网整理以下namespace fnSwordLibrary.DataBase
{
public class CEx_SqlProcedure
{
#region 数据成员
private SqlConnection _SqlConnection = null;
private String _Procedure = String.Empty;
private SqlCommand _SqlCmd = new SqlCommand();
private Hashtable _InputTable = null; // 保存input参数和值
private String _LastError = String.Empty;
#endregion
源代码网整理以下 #region 构造函数
public CEx_SqlProcedure()
{
_InputTable = new Hashtable();
_SqlCmd.CommandType = CommandType.StoredProcedure;
}
源代码网整理以下 public CEx_SqlProcedure(SqlConnection SqlConnection)
: this()
{
this.SqlConnection = SqlConnection;
}
源代码网整理以下 public CEx_SqlProcedure(String Procedure, SqlConnection SqlConnection)
: this()
{
this.SqlConnection = SqlConnection;
this.Procedure = Procedure;
}
#endregion
源代码网整理以下 #region 属性
public String LastError
{
get
{
return this._LastError;
}
}
源代码网整理以下 public Object ReturnValue
{
get
{
return _SqlCmd.Parameters["RetVal"].Value;
}
}
源代码网整理以下 public SqlConnection SqlConnection
{
set
{
this._SqlConnection = value;
_SqlCmd.Connection = this._SqlConnection;
}
}
源代码网整理以下 public String Procedure
{
set
{
this._Procedure = value;
_SqlCmd.CommandText = this._Procedure;
}
源代码网整理以下 get
{
return this._Procedure;
}
}
#endregion
源代码网整理以下 #region 公共方法
/// <summary>
/// 执行存储过程,仅返回是否成功标志
/// </summary>
/// <param name="Procedure">存储过程名</param>
/// <returns>是否成功标志</returns>
public Boolean ExecuteNonQuery(String Procedure)
{
this.Procedure = Procedure;
return ExecuteNonQuery();
}
源代码网整理以下 /// <summary>
/// 执行存储过程,仅返回是否成功标志
/// </summary>
/// <returns>是否成功标志</returns>
public Boolean ExecuteNonQuery()
{
Boolean RetValue = true;
// 绑定参数
if (Bindings() == true)
{
try
{
// 执行
_SqlCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
_LastError = "execute command error: " + ex.Message;
RetValue = false;
}
}
else
{
RetValue = false;
}
源代码网整理以下 _InputTable.Clear(); 软件开发网 www.mscto.com
源代码网整理以下 return RetValue;
}
软件开发网 www.mscto.com
源代码网整理以下 /// <summary>
/// 执行存储过程,返回SqlDataReader
/// </summary>
/// <param name="Procedure">存储过程名</param>
/// <returns>数据库读取行的只进流SqlDataReader</returns>
public SqlDataReader ExecuteReader(String Procedure)
{
this.Procedure = Procedure;
return ExecuteReader();
}
源代码网整理以下 /// <summary>
/// 执行存储过程,返回SqlDataReader
/// </summary>
/// <returns>数据库读取行的只进流SqlDataReader</returns>
public SqlDataReader ExecuteReader()
{
SqlDataReader sqlReader = null;
// 绑定参数
if (Bindings() == true)
{
try
{
// 执行
sqlReader = _SqlCmd.ExecuteReader();
}
catch (Exception ex)
{
_LastError = "execute command error: " + ex.Message;
}
}
源代码网整理以下 _InputTable.Clear();
源代码网整理以下 return sqlReader;
}
源代码网整理以下 /// <summary>
/// 执行存储过程,返回SqlDataAdapter
/// </summary>
/// <param name="Procedure">存储过程名</param>
/// <returns>SqlDataAdapter</returns>
public SqlDataAdapter ExecuteAdapter(String Procedure)
{
this.Procedure = Procedure;
return ExecuteAdapter();
}
源代码网整理以下 /// <summary>
/// 执行存储过程,返回SqlDataAdapter
/// </summary>
/// <returns>SqlDataAdapter</returns>
public SqlDataAdapter ExecuteAdapter()
{
SqlDataAdapter sqlAdapter = null;
源代码网整理以下 // 绑定参数
if (Bindings() == true)
{
try
{
// 执行
sqlAdapter = new SqlDataAdapter(_SqlCmd);
}
catch (Exception ex)
{
_LastError = "execute command error: " + ex.Message;
}
}
源代码网整理以下 _InputTable.Clear();
软件开发网 www.mscto.com
源代码网整理以下 return sqlAdapter;
}
源代码网整理以下 /// <summary>
/// 获取output的键值
/// </summary>
/// <param name="Output">output键名称</param>
/// <returns>output键值</returns>
public Object GetOutputValue(String Output)
{
return _SqlCmd.Parameters[Output].Value;
}
源代码网整理以下 /// <summary>
/// 设置Input参数值
/// </summary>
/// <param name="Key">参数名</param>
/// <param name="Value">参数值</param>
public void SetInputValue(String Key, Object Value)
{
if (Key == null)
{
return;
}
if (!Key.StartsWith("@"))
{
Key = "@" + Key;
}
源代码网整理以下 if (_InputTable.ContainsKey(Key))
{
_InputTable[Key] = Value;
}
else
{
_InputTable.Add(Key, Value);
}
} 软件开发网 www.mscto.com
源代码网整理以下 /// <summary>
/// 获取已设置的Input参数值
/// 注:存储过程被成功执行后, Input参数被清空
/// </summary>
/// <param name="Key">参数名</param>
/// <returns>参数值</returns>
public Object GetInputValue(String Key)
{
if (Key == null)
{
return null;
}
if (!Key.StartsWith("@"))
{
Key = "@" + Key;
}
源代码网整理以下 if (_InputTable.ContainsKey(Key))
{
return _InputTable[Key];
}
else
{
return null;
}
}
#endregion
源代码网整理以下 #region 私有方法
/// <summary>
/// 给SqlCommand对象绑定参数
/// </summary>
/// <returns>是否成功标志</returns>
private Boolean Bindings()
{
_SqlCmd.Parameters.Clear();
XmlReader sqlXmlReader = GetParameters();
try
{
while (sqlXmlReader.Read())
{
try
{
if (Byte.Parse(sqlXmlReader["isoutparam"]) == 1)
{
// 绑定output参数