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; 
         }
發佈了11 篇原創文章 · 獲贊 11 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章