2017-06-07 43 views
1

我已經成功構建了執行select命令的方法。它工作正常。然後我更改我的代碼SqlDataAdapter DA = new SqlDataAdapter();嘗試在SqlDataAdapter中傳遞SqlCommand作爲參數

我試圖在參數中通過SqlCommand作爲CommandType.Text,但我無法成功完成。我得到錯誤。如果我可以將它作爲參數傳遞,有什麼辦法嗎?請參閱我的代碼。

運行的代碼(aspx頁面代碼)

if ((!string.IsNullOrEmpty(user_login.Value)) && (!string.IsNullOrEmpty(user_pass.Value))) 
{ 
     // username & password logic 
     DataTable dt = new DataTable(); 
     string strQuery = "SELECT 1 FROM TBL_USER_INFO WHERE USERNAME = @USERNAME AND PASSWORD = @PASSWORD"; 

     SqlCommand cmd = new SqlCommand(strQuery); 
     cmd.Parameters.Add("@USERNAME", SqlDbType.VarChar).Value = user_login.Value.Trim(); 
     cmd.Parameters.Add("@PASSWORD", SqlDbType.VarChar).Value = user_pass.Value.Trim(); 

     DBConnection conn_ = new DBConnection(); 

     dt = conn_.SelectData(cmd); 

     if (conn_.SQL_dt.Rows.Count > 0) 
     { 
      Response.Redirect("Home.aspx", false); 
     } 
    } 

連接成功,類代碼

public DataTable SelectData(SqlCommand command) 
{ 
    try 
    { 
     conn.Open(); 

     SqlDataAdapter DA = new SqlDataAdapter(); 

     command.CommandType = CommandType.Text; 
     command.Connection = conn; 

     DA.SelectCommand = command; 
     DA.Fill(SQL_dt); 

     return SQL_dt; 
    } 
    catch (Exception ex) 
    { 
     return null; 
    } 
    finally 
    { 
     conn.Close(); 
    } 
} 

如何傳遞CommandType.TextSqlDataAdapter參數?

錯誤代碼

public DataTable SelectData(SqlCommand command) 
{ 
    try 
    { 
     conn.Open(); 

     SqlDataAdapter DA = new SqlDataAdapter(command.CommandType.ToString(), conn); 

     // command.CommandType = CommandType.Text; 
     // command.Connection = conn; 
     DA.SelectCommand = command; 
     DA.Fill(SQL_dt); 

     return SQL_dt; 
    } 
    catch (Exception ex) 
    { 
     return null; 
    } 
    finally 
    { 
     conn.Close(); 
    } 
} 

我收到此錯誤:

System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized.
at System.Data.Common.DbDataAdapter.GetConnection3(DbDataAdapter adapter, IDbCommand command, String method)...

回答

1
public DataTable SelectData(string query) 
     { 
      DataTable dt = new DataTable(); 
      using (SqlConnection con = new SqlConnection("Your Connection String here")) 
      { 
       con.Open(); 
       using (SqlCommand cmd = con.CreateCommand()) 
       { 
        cmd.CommandText = query; 
        cmd.CommandType = CommandType.Text; 
        using (SqlDataAdapter adp = new SqlDataAdapter(cmd)) 
        { 
         adp.Fill(dt); 
         return dt; 
        } 
       } 
      } 
     } 

要使用:

SelectData("select * from yourTable"); 
+0

你甚至可以使用try catch來捕獲錯誤返回.. –

-1

,你得到是不相關的CommandType.Text錯誤,它說你已經初始化的連接屬性SelectCommand中。基本上你應該取消註釋「command.Connection = conn;」擺脫這個錯誤。如果您仍然遇到其他問題,最好在問題中提供這些詳細信息以提供準確的答案。

+0

我已經通過該方法連接的參數''conn' SqlDataAdapter的DA =新SqlDataAdapter的(command.CommandType.ToString(),康涅狄格州)' – user4221591

0

其實你應該傳遞SQLCommand.Hope連接對象它幫助你

DBConnection conn_ = new DBConnection(); 
SqlCommand cmd = new SqlCommand(strQuery,conn_); 
1

紅人有答案。只是爲了清潔代碼一點點......

public DataTable SelectData(string query) 
    { 
     using (var connection = new SqlConnection("myConnectionString")) 
     using (var command = new SqlCommand(query, connection)) 
     using (var adapter = new SqlDataAdapter(command)) 
     { 
      var dt = new DataTable(); 

      connection.Open(); 
      adapter.Fill(dt); 

      return dt; 
     } 
    }