存储过程实体类

    技术2025-03-13  43

    数据库为:MS SQLServer。

    假设现在有一个存储过程:GetNewID,

    那么这个实体类是:

    using System; using System.Data; using System.Collections.Generic; using System.Data.SqlClient; using System.ComponentModel; using SSystem.Database; namespace Entity {     public class GetNewID     {         private IDbConnection _icon_entity = null;         public IDbConnection ICon_Entity         {             get { return _icon_entity; }             set { _icon_entity = value; }         }         private int _newid = 0;         private string _tablename = string.Empty;         private readonly int _maxLen_newid = 4;         private readonly int _maxLen_tablename = 254;         ///<summary>         /// @NewID         /// INT(4)         /// Output         /// </summary>         public int @NewID         {             get { return _newid; }             set { _newid = value; }         }         ///<summary>         /// @TableName         /// VARCHAR(254)         /// Input         /// </summary>         public string @TableName         {             get { return _tablename; }             set { _tablename = value; }         }         ///<summary>         ///执行一个存储过程         /// </summary>         ///<typeparam name="T">只支持如下类型: object,IDataReader,DataSet,Dictionary&lt;string,string&gt;[]         ///                                    <para>object:返回第一行第一列的值;</para>         ///                                    <para>IDataReader:返回一个只读器,关闭这个对象则同时关闭数据库连接;注意,此方式无法获取标示为output的参数</para>         ///                                    <para>DataSet:返回一个离线数据集合</para>         ///                                    <para>Dictionary&lt;string,string&gt;[]:返回一个泛型的散列表</para>         ///</typeparam>         ///<param name="error">return a error message</param>         ///<returns></returns>         public T Exec<T>(out string error)         {             error = "";             List<IDataParameter> sets = new List<IDataParameter>(2);             IDataParameter p1 = null;             p1 = new SqlParameter("@NewID", SqlDbType.Int, _maxLen_newid);             p1.Value = @NewID;             p1.Direction = ParameterDirection.Output;             sets.Add(p1);             IDataParameter p2 = null;             p2 = new SqlParameter("@TableName", SqlDbType.VarChar, _maxLen_tablename);             p2.Value = @TableName;             sets.Add(p2);             string typeName = typeof(T).Name.ToLower();             T result = default(T);             switch (typeName)             {                 case "object":                     result = (T)DBUtil.ExecuteScalarSp(_icon_entity, this.GetType().Name, sets, out error);                     break; ;                 case "idatareader":                     result = (T)DBUtil.GetDataReaderSp(_icon_entity, this.GetType().Name, sets, out error);                     break;                 case "dataset":                     result = (T)(IListSource)DBUtil.GetDataSetSp(_icon_entity, this.GetType().Name, sets, out error);                     break;                 default:                     if (typeof(Dictionary<string, string>[]).Name.ToLower() == typeName)                     {                         IDataReader r = DBUtil.GetDataReaderSp(_icon_entity, this.GetType().Name, sets, out error);                         List<Dictionary<string, string>> dyn = new List<Dictionary<string, string>>();                         while (r.Read())                         {                             Dictionary<string, string> tmp = new Dictionary<string, string>(r.FieldCount);                             for (int i = 0; i < r.FieldCount; i++)                             {                                 tmp.Add(r.GetName(i), Convert.ToString(r.GetValue(i)));                             }                             dyn.Add(tmp);                         }                         r.Close();                         result = (T)(object)dyn.ToArray();                     }                     break;             }             this._newid = Convert.ToInt32(p1.Value);             return result;         }     } } 上面这个代码是通过CodeSmith自动生成的。这个实体类的调用方式:

    GetNewID obj=new GetNewID();

    obj.@TableName="User";

    string error;

    object oID = obj.Exec<object>(out error);

    如果调用存储过程不抛出异常,则oID就能获取一个新的主键。

     

    生成存储过程实体类的Codesmith文件在我的资源中,大家可以去下载。此方法能大大的简化调用存储过程。

    最新回复(0)