关于AdoHelper的一个方便之处的代码实现细节

Posted on 2006-02-21 11:15  A.Z  阅读(1966)  评论(0编辑  收藏  举报
应该都知道在数据库里总有两张系统表对应于当前的表的结构,也有几条系统级的存储过程去对一些全局的信息去做设置和获取,这些将方便我们编程。你不需要自己去慢慢摸索,原理和分析网上比比皆是。希望对你有所启发。
这里是sql的实现。





  1     internal class SqlDeriveParameters
  2     {
  3         internal static void DeriveParameters(SqlCommand cmd)
  4         {
  5             string cmdText;
  6             SqlCommand newCommand;
  7             SqlDataReader reader;
  8             ArrayList parameterList;
  9             SqlParameter sqlParam;
 10             CommandType cmdType;
 11             string procedureSchema;
 12             string procedureName;
 13             int groupNumber;
 14             SqlTransaction trnSql = cmd.Transaction;
 15 
 16             cmdType = cmd.CommandType;
 17 
 18             if ((cmdType == CommandType.Text))
 19             {
 20                 throw new InvalidOperationException();
 21             }
 22             else if ((cmdType == CommandType.TableDirect))
 23             {
 24                 throw new InvalidOperationException();
 25             }
 26             else if ((cmdType != CommandType.StoredProcedure))
 27             {
 28                 throw new InvalidOperationException();
 29             }
 30 
 31             procedureName = cmd.CommandText;
 32             string server = null;
 33             string database = null;
 34             procedureSchema = null;
 35 
 36             // split out the procedure name to get the server, database, etc.
 37             GetProcedureTokens(ref procedureName, ref server, ref database, ref procedureSchema);
 38 
 39             // look for group numbers
 40             groupNumber = ParseGroupNumber(ref procedureName);
 41 
 42             newCommand = null;
 43 
 44             // set up the command string.  We use sp_procuedure_params_rowset to get the parameters
 45             if (database != null)
 46             {
 47                 cmdText = string.Concat("[", database, "]..sp_procedure_params_rowset");
 48                 if (server != null)
 49                 {
 50                     cmdText = string.Concat(server, ".", cmdText);
 51                 }
 52 
 53                 // be careful of transactions
 54                 if (trnSql != null)
 55                 {
 56                     newCommand = new SqlCommand(cmdText, cmd.Connection, trnSql);
 57                 }
 58                 else
 59                 {
 60                     newCommand = new SqlCommand(cmdText, cmd.Connection);
 61                 }
 62             }
 63             else
 64             {
 65                 // be careful of transactions
 66                 if (trnSql != null)
 67                 {
 68                     newCommand = new SqlCommand("sp_procedure_params_rowset", cmd.Connection, trnSql);
 69                 }
 70                 else
 71                 {
 72                     newCommand = new SqlCommand("sp_procedure_params_rowset", cmd.Connection);
 73                 }
 74             }
 75 
 76             newCommand.CommandType = CommandType.StoredProcedure;
 77             newCommand.Parameters.Add(new SqlParameter("@procedure_name", SqlDbType.NVarChar, 255));
 78             newCommand.Parameters[0].Value = procedureName;
 79 
 80             // make sure we specify 
 81             if (! IsEmptyString(procedureSchema))
 82             {
 83                 newCommand.Parameters.Add(new SqlParameter("@procedure_schema", SqlDbType.NVarChar, 255));
 84                 newCommand.Parameters[1].Value = procedureSchema;
 85             }
 86 
 87             // make sure we specify the groupNumber if we were given one
 88             if (groupNumber != 0)
 89             {
 90                 newCommand.Parameters.Add(new SqlParameter("@group_number", groupNumber));
 91             }
 92 
 93             reader = null;
 94             parameterList = new ArrayList();
 95 
 96             try
 97             {
 98                 // get a reader full of our params
 99                 reader = newCommand.ExecuteReader();
100                 sqlParam = null;
101 
102                 while (reader.Read())
103                 {
104                     // get all the parameter properties that we can get, Name, type, length, direction, precision
105                     sqlParam = new SqlParameter();
106                     sqlParam.ParameterName = (string) (reader["PARAMETER_NAME"]);
107                     sqlParam.SqlDbType = GetSqlDbType((short) (reader["DATA_TYPE"]), (string) (reader["TYPE_NAME"]));
108 
109                     if (reader["CHARACTER_MAXIMUM_LENGTH"!= DBNull.Value)
110                     {
111                         sqlParam.Size = (int) (reader["CHARACTER_MAXIMUM_LENGTH"]);
112                     }
113 
114                     sqlParam.Direction = GetParameterDirection((short) (reader["PARAMETER_TYPE"]));
115 
116                     if ((sqlParam.SqlDbType == SqlDbType.Decimal))
117                     {
118                         sqlParam.Scale = (byte) (((short) (reader["NUMERIC_SCALE"]) & 255));
119                         sqlParam.Precision = (byte) (((short) (reader["NUMERIC_PRECISION"]) & 255));
120                     }
121                     parameterList.Add(sqlParam);
122                 }
123             }
124             finally
125             {
126                 // close our reader and connection when we're done
127                 if (reader != null)
128                 {
129                     reader.Close();
130                 }
131                 newCommand.Connection = null;
132             }
133 
134             // we didn't get any parameters
135             if ((parameterList.Count == 0))
136             {
137                 throw new InvalidOperationException();
138             }
139 
140             cmd.Parameters.Clear();
141 
142             // add the parameters to the command object
143 
144             foreach (object parameter in parameterList)
145             {
146                 cmd.Parameters.Add(parameter);
147             }
148         }
149