Я создаю программу в VB.NET, где перед созданием пользователя я хочу проверить, существует ли эта запись.
Вот мой код:
Imports MySql.Data.MySqlClient
Public Class WindowsAdvancedStudyStartingForms
Dim FirstName As String = ""
Dim SecondName As String = ""
Dim FullName As String = ""
Dim StudentClassReal As String = ""
Dim StudentClassValue As String = ""
Dim Address As String = ""
Dim Username As String = ""
Dim Password As String = ""
Dim SuccessfulMessage As Integer = 0
'MySql
Dim ServerString As String = "Server=myServer;User ID=myID;Passwordmy=Password;Database=myDatabase;SSLMode=None"
Dim SQLConnection As MySqlConnection = New MySqlConnection
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FirstName = AFirstNameTextBox.Text
SecondName = ASecondNameTextBox.Text
FullName = FirstName + " " + SecondName
StudentClassValue = ASelectClassComboBox.SelectedItem
Address = AAddressTextBox.Text
Username = AUsernameTextBox.Text
Password = APasswordTextBox.Text
If StudentClassValue = "Class IX" Then
StudentClassReal = 9
Else
MessageBox.Show("You have selected a Wrong Class", "Wrong Class", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If FirstName = "" And FirstName.Count = 1 Then
MessageBox.Show("You didn't enter your First Name", "First Name", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
'Nothing
End If
If SecondName = "" Then
MessageBox.Show("You didn't enter your Second Name", "Second Name", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
'Nothing
End If
If Address = "" Then
MessageBox.Show("You didn't enter your Address", "Address", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
'Nothing
End If
If Username = "" Then
MessageBox.Show("You didn't enter your Username", "Username", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
'Nothing
End If
If Password = "" Then
MessageBox.Show("You didn't enter your Password", "Password", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
'Nothing
End If
If StudentClassReal = "9" Then
Dim StudentInformationVerification As Integer = MessageBox.Show("Are you sure that these are your information?" & vbCrLf & "I am " + FullName + ", and I study at Class " + StudentClassReal + ". I live in " + Address + ". My Advanced Windows Study Username is " + Username + ", and my password is " + Password, "Information Verification", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If StudentInformationVerification = DialogResult.Yes Then
Dim SQLStatement As String = "INSERT INTO people(FirstName, SecondName, Class, Address, Username, Password) VALUES('" & FirstName & "','" & SecondName & "', '" & StudentClassReal & "', '" & Address & "', '" & Username & "', '" & Password & "')"
SaveName(SQLStatement)
My.Computer.Registry.LocalMachine.SetValue("Study", "1")
SuccessfulMessage = 1
Me.Close()
End If
If StudentInformationVerification = DialogResult.No Then
End If
Else
'Nothing
End If
End Sub
'MYSQL Connection
Private Sub WindowsAdvancedStudyStartingForms_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim RegistryCheck As String = My.Computer.Registry.LocalMachine.GetValue("Study")
If RegistryCheck = 1 Then
LoginForm1.Show()
Me.Close()
End If
APasswordTextBox.UseSystemPasswordChar = True
SQLConnection:
SQLConnection.ConnectionString = ServerString
Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
If SuccessfulMessage = 0 Then
MessageBox.Show("Connected to Windows Advanced Study Database", "Connection to Database Passed", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
SQLConnection.Close()
MessageBox.Show("Could not Connect to Windows Advanced Study Database", "Connection to Database Failed", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)
If DialogResult.Retry Then
GoTo SqlConnection
End If
If DialogResult.Cancel Then
Close()
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub SaveName(ByRef SQLStatement As String)
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
End With
MessageBox.Show("Welcome to Advanced Windows Studying", "Authentication Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Class