CommandType的使用

CommandType.Text代表执行的是SQL语句 
CommandType.StoreProcedure代表执行的是存储过程 
CommandType代表要执行的类型 

使用文件: 
string username = "ZBC"; 
string password= Utils.MD5(password); 
SqlParameter[] parms = {   
                                       new SqlParameter("@username",username), 
                                       new SqlParameter("@password",password) 
   }; 
string query= "select TOP 1 [userID],[password]  from adminuser where [userLogName]=@username and [password]=@password"; 
SqlDataReader reader = DataAccess.ExecuteReader(CommandType.Text,query, parms); 
string smtpto = " "; 
while (reader.Read()) 

     smtpto = reader[0].ToString(); 


命名空间中的函数: 

         /// <summary> 
         /// 执行指定数据库连接字符串的数据阅读器,指定参数. 
         /// </summary> 
         /// <remarks> 
         /// 示例:   
         ///  SqlDataReader dr = ExecuteReader(CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24)); 
         /// </remarks> 
         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
         /// <param name="commandText">存储过程名或SQL语句</param> 
         /// <param name="commandParameters">SqlParamter参数数组(new DbParameter("@prodid", 24))</param> 
         /// <returns>返回包含结果集的SqlDataReader</returns> 
         public static SqlDataReader ExecuteReader(CommandType commandType, string commandText, SqlParameter[] coll) 
         { 
             SqlDataReader dr = null; 
             try 
             { 
                 OpenConnection(); 
                 comm.Parameters.Clear(); 
                 for (int i = 0; i < coll.Length; i++) 
                 { 
                     comm.Parameters.Add(coll ); 
                 } 
                 comm.CommandText = commandText; 
                 comm.CommandType = commandType; 
                 dr = comm.ExecuteReader(CommandBehavior.CloseConnection); 
             } 
             catch 
             { 
                 try 
                 { 
                     if (dr != null && !dr.IsClosed) 
                         dr.Close(); 
                     CloseConnection(); 
                 } 
                 catch 
                 { 

                 } 
             } 
             return dr; 
         }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值