vb里往access数据表中插入数据,insert语句老是说有语法错误。
ImportsSystem.Data.OleDbPublicClassForm1DimMyConAsOleDbConnectionPrivateSubButton1_Cl...
Imports System.Data.OleDb
Public Class Form1
Dim MyCon As OleDbConnection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim AppString As String '存放要执行的SQL语句的字符串
Dim MyCom As New OleDbCommand() '建立执行SQL语句的Command对象
MyCom.Connection = MyCon '设置命令对象的Connection属性
MyCom.CommandType = CommandType.Text '设置命令类型
AppString = "Insert into [userInfo] (Username,Password,Sex,Email,Bank,Identify) values('" + _
TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + _
TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')"
MyCom.CommandText = AppString '把该字符串作为命令文本
MyCom.ExecuteNonQuery() '执行SQL-INSERT命令
MessageBox.Show("数据插入成功")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim CString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.MDB"
MyCon = New OleDbConnection(CString) '创建连接对象
Try
MyCon.Open() '打开连接
Catch ex As Exception
MessageBox.Show("未连接数据库")
Exit Sub
End Try
End Sub
End Class 展开
Public Class Form1
Dim MyCon As OleDbConnection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim AppString As String '存放要执行的SQL语句的字符串
Dim MyCom As New OleDbCommand() '建立执行SQL语句的Command对象
MyCom.Connection = MyCon '设置命令对象的Connection属性
MyCom.CommandType = CommandType.Text '设置命令类型
AppString = "Insert into [userInfo] (Username,Password,Sex,Email,Bank,Identify) values('" + _
TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + _
TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')"
MyCom.CommandText = AppString '把该字符串作为命令文本
MyCom.ExecuteNonQuery() '执行SQL-INSERT命令
MessageBox.Show("数据插入成功")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim CString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.MDB"
MyCon = New OleDbConnection(CString) '创建连接对象
Try
MyCon.Open() '打开连接
Catch ex As Exception
MessageBox.Show("未连接数据库")
Exit Sub
End Try
End Sub
End Class 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询