2014-01-06 171 views
0

我剛剛切換到使用mysql查詢參數,並擔心我找不到如何將結果保存到數據表中。我想將此選擇查詢的結果保存到數據表「表」中。以前我會做將mysql結果保存到數據表

adptr = New MySqlDataAdapter("SELECT * from users WHERE uname='" & MySqlHelper.EscapeString(UsernameTextBox.Text) & "' and pword=md5('" & MySqlHelper.EscapeString(PasswordTextBox.Text) & "')", con) 
    Try 
     adptr.Fill(table) 
    Catch err As Exception 
     Dim strError As String = "Exception: & err.ToString()" 
    End Try 

現在我有

Public table As New DataTable 
    Dim cb As New MySqlConnectionStringBuilder 
    cb.Database = dbfile 
    cb.Server = dbserver 
    cb.UserID = dbuser 
    cb.Password = dbpassw 

    Using cnn As New MySqlConnection(cb.ConnectionString) 
     Using cmd As New MySqlCommand("SELECT * from users WHERE uname=&uname and pword=md5(&pword)", cnn) 
      cmd.Parameters.AddWithValue("@uname", UsernameTextBox.Text) 
      cmd.Parameters.AddWithValue("@pword", PasswordTextBox.Text) 

      Try 
       cnn.Open() 
       cmd.ExecuteNonQuery() 
      Catch ex As Exception 
       MessageBox.Show(ex.ToString) 
      End Try 
     End Using 
    End Using 

我怎麼將結果保存到表?

回答

1

使用命令來初始化適配器,然後像以前一樣

Using cnn As New MySqlConnection(cb.ConnectionString) 
    Using cmd As New MySqlCommand("SELECT * from users WHERE uname=&uname and pword=md5(&pword)", cnn) 
     cnn.Open 
     cmd.Parameters.AddWithValue("&uname", UsernameTextBox.Text) 
     cmd.Parameters.AddWithValue("&pword", PasswordTextBox.Text) 
     adptr = New MySqlDataAdapter(cmd) 
     adptr.Fill(table) 
    End Using 
End Using 

的ExecuteNonQuery應該用於更改數據庫表(INSERT/UPDATE/DELETE)或其他DDL命令查詢工作。你的情況準備的命令可用於個MySqlDataAdapter的overladed構造的一個(看看這個文檔爲SqlDataAdapter它們具有相同的邏輯效果)

而且我不知道該了MySQLParameter前綴是&(我有總是使用@?

+0

如果使用&uname參數也必須更改 - @也可以工作,雖然 - 謝謝你的答案 –