我正在使用数据库,今天在尝试使用 SQLCompactEdition 数据库时,我创建了一个名为 UserDB 的本地数据库,并在其中有一个名为 User 的表。
我已经使用我的应用程序(这是另一个数据库)登录到管理员,并通过管理员创建了一个用户。
我用于此目的的代码是:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connStr As New SqlCeConnection("Data Source=c:\users\babi\documents\visual studio 2012\Projects\ShopManagement\ShopManagement\" & "User\UserDB.sdf; Password =")
Dim name, pass, repass As String
name = TextBox1.Text
If (name.Length = 0) Then
MessageBox.Show("Enter user-name!!")
Exit Sub
End If
pass = TextBox2.Text
If (pass.Length = 0) Then
MessageBox.Show("Enter password!!")
Exit Sub
End If
repass = TextBox3.Text
If (repass.Length = 0) Then
MessageBox.Show("Re-enter password!!")
Exit Sub
End If
If (getMD5Hash(pass) = getMD5Hash(repass)) Then
Dim cmd As New SqlCeCommand("INSERT INTO User(uname, upass)VALUES(" & name & "," & getMD5Hash(pass) & ");", connStr)
connStr.Open()
cmd.ExecuteNonQuery()
connStr.Close()
MessageBox.Show("User Created!")
Exit Sub
Else
MessageBox.Show("Passwords donot match!!")
Exit Sub
End If
End Sub
当我调试时,我发现查询字符串是:
"INSERT INTO User(uname, upass)VALUES(abcd,827ccb0eea8a706c4c34a16891f84e7b);"
发生的异常是:
An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
我不明白是什么导致了这个错误。如果需要更多信息,请询问。
编辑
固定代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connStr As New SqlCeConnection("Data Source=c:\users\babi\documents\visual studio 2012\Projects\ShopManager\ShopManager\" & "ManagementDB.sdf; Password =")
Dim name, pass, repass As String
name = TextBox1.Text
If (name.Length = 0) Then
MessageBox.Show("Enter user-name!!")
Exit Sub
End If
pass = TextBox2.Text
If (pass.Length = 0) Then
MessageBox.Show("Enter password!!")
Exit Sub
End If
repass = TextBox3.Text
If (repass.Length = 0) Then
MessageBox.Show("Re-enter password!!")
Exit Sub
End If
If (getMD5Hash(pass) = getMD5Hash(repass)) Then
Dim cmd As New SqlCeCommand("INSERT INTO [User](uname, upass) VALUES('" & name & "','" & getMD5Hash(pass) & "');", connStr)
connStr.Open()
cmd.ExecuteNonQuery()
connStr.Close()
MessageBox.Show("User Created!")
Exit Sub
Else
MessageBox.Show("Passwords donot match!!")
Exit Sub
End If
End Sub
问题的解决方案是由两个解决方案的组合给出的!所以谢谢你们两个:)
问候普里亚布拉塔