我有一個關於phpmyadmin的節省了用戶名到MySQL數據庫的SQL語句就像一個魅力:MySQL聲明將密碼保存到數據庫?
Dim SQLStatement As String = "INSERT INTO accountinfodb(`Usernames`) VALUES ('" & txtUsername.Text & "')"
然而
我還索要用戶密碼,我D喜歡存儲以及,所以我雖然這將是有道理的:
Dim SQLStatement As String = "INSERT INTO accountinfodb(`Usernames`, `Passwords`) VALUES ('" & txtUsername.Text & txtPasswd.Text & "')"
不幸的是,這段代碼不起作用,我得到關於VA的錯誤蓋並打開連接,或者在我的MySQL語法中出現語法錯誤。所以我想知道是否有人知道將用戶名和密碼存儲到我的數據庫中的正確方法?
這是我的完整vb.net代碼。
Imports MySql.Data.MySqlClient
Public Class frmSignup
Dim ServerString As String = "Server=localhost;User Id=root;Password=;Database=accountinfo"
Dim SQLConnection As MySqlConnection = New MySqlConnection
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SQLConnection.ConnectionString = ServerString
Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
MsgBox("Successfully connected to DB")
Else
SQLConnection.Close()
MsgBox("Failed to connect to DB")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub SaveAccountInformation(ByRef SQLStatement As String)
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
End With
SQLConnection.Close()
SQLConnection.Dispose()
End Sub
Private Sub btnSignup_Click(sender As Object, e As EventArgs) Handles btnSignup.Click
If txtPasswd.Text = txtPasswd2.Text Then
MessageBox.Show("Passwords Match!")
Dim SQLStatement As String = "INSERT INTO accountinfodb(`Usernames`, `Passwords`) VALUES ('" & txtUsername.Text & txtPasswd.Text & "')"
SaveAccountInformation(SQLStatement)
MessageBox.Show("Account Successfully Registered")
Else
MessageBox.Show("Passwords Do Not Match!")
txtPasswd.Text = Focus()
txtPasswd.Clear()
txtPasswd2.Text = Focus()
txtPasswd2.Clear()
End If
End Sub
End Class
編輯1
@Rahul
我加了你的SQL語句,我的代碼,我給出以下錯誤,當我輸入一個隨機的用戶名/ passwd文件
未處理異常類型'MySql.Data.MySqlClient.MySqlException' 發生在MySql.Data.dll中
其他信息:您的SQL語法有錯誤;檢查 對應於您MariaDB的服務器版本在線路附近使用「測試」)」 1
谷歌與原因有很多參數的SQL語句 –
@SirRufo你能至少解釋爲什麼我的SQL語句不起作用? – Jcrow
您承諾DB 2項目,然後將兩個項目粘合在一起,只發送一個;那是語法錯誤。使用SQL參數,你所做的更清晰(如果用戶名是'D'Angelo'或更糟糕的話,它不會崩潰)。你不打開連接,所以這是另一個錯誤 – Plutonix