专业开发人员我正在VB.Net和MySQL数据库一起使用,我想在数据库中插入值。示例:我的数据库中的值为 Admin11 ,但是当我插入另一个值 admin11 时,出现错误,这是我的代码。顺便说一下,这两种方法在不同的类中。
''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''
这是我的代码,用于检查数据库中是否存在用户名...
公共子checkUsernameIfExist()
Dim con = New MySqlConnection
con.ConnectionString = "server=localhost;userid=root;password=alpine;port=3305;database=pos_db;pooling=false;SslMode=none"
con.Open()
Dim query As String = "SELECT Username FROM pos_db.tblusers WHERE BINARY Username=@Users"
Dim cmd As New MySqlCommand(query, con)
cmd.Parameters.AddWithValue("@Users", frmLogin.txtUser.Text)
Dim count As Integer = Convert.ToInt32(cmd.ExecuteScalar())
If count <> 0 Then
MessageBox.Show("Username is already taken. Please create a unique one!", "System", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
Else
insertDataToTblUser.insertToLogin()
End If
con.Close()
con.Dispose()
End Sub
''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
这是我在插入值中的代码……
作为MySqlConnection的昏暗提示
Public Sub insertToLogin()
con = New MySqlConnection
con.ConnectionString = "server=localhost;userid=root;password=alpine;port=3305;database=pos_db;pooling=false;SslMode=none"
con.Open()
Dim qry As String = "INSERT INTO tblUsers (Username,Password,Level) VALUES (@User,@Pass,@lvl)"
Dim cmd As New MySqlCommand(qry, con)
cmd.Parameters.AddWithValue("@User", frmLogin.txtUser.Text)
cmd.Parameters.AddWithValue("@Pass", frmLogin.txtPass.Text)
cmd.Parameters.AddWithValue("@lvl", frmLogin.cmbUserlevel.Text)
cmd.ExecuteNonQuery()
MessageBox.Show("Sign Up Successful", "System", MessageBoxButtons.OK, MessageBoxIcon.Information)
con.Close()
con.Dispose()
End Sub
请多多帮助我。
答案 0 :(得分:2)
这些解决了我的问题,感谢所有帮助我的人。
CREATE TABLE `pos_db`.`tblForLogin` (
`Username` VARCHAR(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL UNIQUE,
`Password` VARCHAR(50) NOT NULL,
`Level` VARCHAR(50) NOT NULL,
PRIMARY KEY(Username)
);
答案 1 :(得分:0)
更改查询:
Dim qry As String = "INSERT INTO tblUsers (Username,`Password`,`Level`) VALUES (@User,@Pass,@lvl)"