您好我无法保存到我的数据库,它说我的数据库出现语法错误。我应该在我的号码列中使用什么数据类型,我将输入11位数的手机号码。
以下是代码
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 scitech 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 scitech ('name', 'Surname', 'Number', 'College', 'Course', 'Year') values (@sName,@sLname,@sNum,@sCollege,@sCourse,@sYr)"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("@sName", TextBox1name.Text)
.Parameters.AddWithValue("@sLname", TextBox2su.Text)
.Parameters.AddWithValue("@sNum", TextBox3num.Text)
.Parameters.AddWithValue("@sCollege", ComboBox1.SelectedItem)
.Parameters.AddWithValue("@sCourse", ComboBox2.SelectedItem)
.Parameters.AddWithValue("@sYr", ComboBox3.SelectedItem)
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
InsertStud()
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 database.scitech ('name', 'Surname', 'Number', 'College', 'Course', 'Year') values (@sName,@sLname,@sNum,@sCollege,@sCourse,@sYr)"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("@sName", TextBox1name.Text)
.Parameters.AddWithValue("@sLname", TextBox2su.Text)
.Parameters.AddWithValue("@sNum", TextBox3num.Text)
.Parameters.AddWithValue("@sCollege", ComboBox1.SelectedItem)
.Parameters.AddWithValue("@sCourse", ComboBox2.SelectedItem)
.Parameters.AddWithValue("@sYr", ComboBox3.SelectedItem)
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
答案 0 :(得分:0)
您的表格中未定义名称,请尝试删除列名称周围的瑕疵。
答案 1 :(得分:0)
您正在使用'而不是`为字段名称。 单引号'仅适用于值字符串