Mysql 列';名字';VB.Net错误不能为空
我正在创建一个系统。现在在我创建的登记表中卡住了。我使用Visual Studio 2008,并使用VB作为编程语言,使用MySql连接器5.1.1将其连接到MySql数据库 我有这些,它不是空的,但当我在我的注册表中输入数据时,我得到了这个 当我在数据库的表结构中将其更改为Null时,我在创建的注册表中输入的数据在数据库中变为Null 我不知道我的代码或数据库中是否有错误。我只是一个初学者,希望有人能帮助我 这是我按钮中的代码,用于在我的注册表中注册新用户帐户Mysql 列';名字';VB.Net错误不能为空,mysql,vb.net,Mysql,Vb.net,我正在创建一个系统。现在在我创建的登记表中卡住了。我使用Visual Studio 2008,并使用VB作为编程语言,使用MySql连接器5.1.1将其连接到MySql数据库 我有这些,它不是空的,但当我在我的注册表中输入数据时,我得到了这个 当我在数据库的表结构中将其更改为Null时,我在创建的注册表中输入的数据在数据库中变为Null 我不知道我的代码或数据库中是否有错误。我只是一个初学者,希望有人能帮助我 这是我按钮中的代码,用于在我的注册表中注册新用户帐户 Private Sub
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
Dim fname As String = txtboxfname.Text
Dim lname As String = txtboxlname.Text
Dim uname As String = txtboxuname.Text
Dim pass As String = txtboxpass.Text
Dim cpass As String = txtboxcpass.Text
If txtboxfname.Text() = "" Or txtboxlname.Text() = "" Or txtboxuname.Text() = "" Or txtboxpass.Text() = "" Then
MessageBox.Show("One Or More Fields Are Empty", "Missing Data", MessageBoxButtons.OK, MessageBoxIcon.Stop)
ElseIf Not String.Equals(pass, cpass) Then
MessageBox.Show("Wrong Confirmation Password", "Password Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf unameExist(uname) Then
MessageBox.Show("This Username Already Exists, Choose Another One", "Duplicate Username", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Dim conn As New DBCon()
Dim command As New MySqlCommand("INSERT INTO `login`(`first_name`, `last_name`, `username`, `password`) VALUES (@fn, @ln, @usn, @pwd)", conn.getConnection())
command.Parameters.Add("@fn", MySqlDbType.VarChar).Value = fname
command.Parameters.Add("@ln", MySqlDbType.VarChar).Value = lname
command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = uname
command.Parameters.Add("@pwd", MySqlDbType.VarChar).Value = pass
conn.openConnection()
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("Registration Completed Successfully", "User Added", MessageBoxButtons.OK, MessageBoxIcon.Information)
conn.closeConnection()
Else
MessageBox.Show("Something Happened", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
conn.closeConnection()
End If
End If
End Sub
这是我把VB连接到MySql的代码
Imports MySql.Data.MySqlClient
Public Class DBCon
Private conn As New
MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=blood_elements")
ReadOnly Property getConnection() As MySqlConnection
Get
Return conn
End Get
End Property
Sub openConnection()
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
End Sub
Sub closeConnection()
If conn.State = ConnectionState.Open Then
conn.Close()
End If
End Sub
End Class
你的系统有一个大问题。连接和其他一些数据库对象不仅需要关闭,还需要释放。它们使用非托管代码,当您调用.Dispose时,它们会释放该非托管代码中的资源。在使用连接的方法中创建连接。将数据访问代码与用户界面代码分开。毕竟,明天管理层可能会决定他们想要一个web应用程序。您可以将数据访问代码添加到不同的用户界面 幸运的是,vb.Net和C#提供了使用块关闭和处理对象的功能 事件子类中应该只有很少的代码。所有消息框都显示在用户界面代码中,而不是数据访问代码中
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not ValidateNewUserInput() Then
Exit Sub
End If
Dim Success = DataAccess.InsertUser(txtboxfname.Text, txtboxlname.Text, txtboxuname.Text, txtboxpass.Text)
If Success Then
MessageBox.Show("Registration Completed Successfully", "User Added", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Something Happened", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
在验证代码中,我对修剪过的字符串使用了IsNullOrEmpty。这应该覆盖空白,如选项卡或文本框中的空格。我还使用了OrElse,它是一种短路操作员。If语句将在找到True后立即停止计算条件
因为UserNameExists是一个共享方法,所以不需要创建DataAccess类的实例
Private Function ValidateNewUserInput() As Boolean
If String.IsNullOrEmpty(txtboxfname.Text.Trim) OrElse String.IsNullOrEmpty(txtboxlname.Text.Trim OrElse String.IsNullOrEmpty(txtboxuname.Text.Trim OrElse String.IsNullOrEmpty(txtboxpass.Text.Trim) Then
MessageBox.Show("One Or More Fields Are Empty", "Missing Data", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Return False
ElseIf txtboxpass.Text <> txtboxcpass Then
MessageBox.Show("Wrong Confirmation Password", "Password Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
ElseIf DataAccess.UserNameExists(txtboxuname.Text) Then
MessageBox.Show("This Username Already Exists, Choose Another One", "Duplicate Username", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return False
End If
Return True
End Function
我看不到errro fname似乎为空或NULL,请检查它!我使用了你的代码,但仍然存在相同的问题。表的结构不是空的,当我在注册表中输入数据时,仍然会出现错误。我还将数据库中的表结构更改为Null,但当我在注册表中输入数据时,数据库中的数据仍然为Null。
Public Class DataAccess
Private Shared ConStr As String = "datasource=localhost;port=3306;username=root;password=;database=blood_elements"
Public Shared Function InsertUser(FirstName As String, LastName As String, UserName As String, PWord As String) As Boolean
Using conn As New MySqlConnection(ConStr),
command As New MySqlCommand("INSERT INTO `login`(`first_name`, `last_name`, `username`, `password`) VALUES (@fn, @ln, @usn, @pwd)", conn)
command.Parameters.Add("@fn", MySqlDbType.VarChar).Value = FirstName
command.Parameters.Add("@ln", MySqlDbType.VarChar).Value = LastName
command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = UserName
command.Parameters.Add("@pwd", MySqlDbType.VarChar).Value = PWord
conn.Open()
If command.ExecuteNonQuery() = 1 Then
Return True
Else
Return False
End If
End Using
End Function
Public Shared Function UserNameExists(UName As String) As Boolean
Using conn As New MySqlConnection(ConStr),
command As New MySqlCommand("Select `first_name` From `login` Where `username` = @UName", conn)
command.Parameters.AddWithValue("@UName", UName)
conn.Open()
Using reader = command.ExecuteReader
If reader.HasRows Then
Return True
Else
Return False
End If
End Using
End Using
End Function
End Class