當我按我的更新按鈕時我得到mscorlib.dll中發生未處理的異常'System.FormatException'類型。我想更新我的數據庫值。只有手機號碼是您可以更新的值。mscorlib.dll在更新按鈕中出現'system.formatexception'類型的未處理的異常
Imports MySql.Data.MySqlClient
Imports System.Data.Sql
Imports System
Imports System.Data
Public Class AddStudent
Dim connString As String = "server=localhost;userid=jared;password=jared;database=database;persistsecurityinfo=True"
Dim SQLConnection As MySqlConnection = New MySqlConnection
Dim student As New DataTable()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim iReturn As Boolean
Using SQLConnection As New MySqlConnection(connString)
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "INSERT INTO students (StudentNumber, Firstname, Surname, Number) values (@sNumber,@sName,@sLname,@sNum)"
.CommandType = CommandType.Text
.Parameters.AddWithValue("@sNumber", TextBox1sn.Text)
.Parameters.AddWithValue("@sName", TextBox1name.Text)
.Parameters.AddWithValue("@sLname", TextBox2su.Text)
.Parameters.AddWithValue("@sNum", TextBox3num.Text)
End With
Try
SQLConnection.Open()
sqlCommand.ExecuteNonQuery()
iReturn = True
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
iReturn = False
Finally
SQLConnection.Close()
End Try
End Using
End Using
End Sub
Public Function InsertStud() As Boolean
Dim iReturn As Boolean
Using SQLConnection As New MySqlConnection(connString)
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "INSERT INTO students (StudentNumber, Firstname, Surname, Number) values (@sNumber,@sName,@sLname,@sNum)"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("@sNumber", TextBox1sn.Text)
.Parameters.AddWithValue("@sName", TextBox1name.Text)
.Parameters.AddWithValue("@sLname", TextBox2su.Text)
.Parameters.AddWithValue("@sNum", TextBox3num.Text)
End With
Try
SQLConnection.Open()
sqlCommand.ExecuteNonQuery()
iReturn = True
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
iReturn = False
Finally
SQLConnection.Close()
End Try
End Using
End Using
Return iReturn
End Function
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim iReturn As Boolean
Using SQLConnection As New MySqlConnection(connString)
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "UPDATE students SET StudentNumber = @sNumber, " & TextBox1sn.Text &
"`Name`= @sName, [email protected], Number= @sNum, " & _
"College= @sCollege, Course= " & _
("Where StudentNumber = @oldNumber")
.Connection = SQLConnection
.Parameters.AddWithValue("@sNumber", SqlDbType.VarChar).Value = TextBox1sn.Text
.Parameters.Add("@sName", MySqlDbType.VarChar).Value = TextBox1name.Text
.Parameters.Add("@sLname", MySqlDbType.VarChar).Value = TextBox2su.Text
.Parameters.Add("@sNum", MySqlDbType.VarChar).Value = TextBox3num.Text
.Parameters.AddWithValue("@oldNumber", SqlDbType.VarChar).Value = TextBox1sn.Text
End With
Try
SQLConnection.Open()
sqlCommand.ExecuteNonQuery()
iReturn = True
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
iReturn = False
Finally
SQLConnection.Close()
End Try
End Using
End Using
End Sub
End Class
Dont concat SQL爲您的WHERE子句使用參數,就像您爲其他人所做的那樣; '姓名'是一個保留字,所以你必須逃避。否則,告訴我們錯誤發生的地方。年份是Db中的數字類型嗎? – Plutonix
如果'StudentNumber'是一個自動增量ID,那麼您可能不想更新它。從SQL中刪除該部分,並使用'@ sNumber'作爲WHERE子句。如果它*是一個AI ID,它不會是文本/字符/字符串類型 – Plutonix
@Plutonix你好。 StudentNumber不是自動增量ID。輸入的是隨機數。該錯誤發生在sqlCommand.ExecuteNonQuery()上。 – Redd