Public Class DB
'数据库连接
Private conn As OleDb.OleDbConnection
Private command As OleDb.OleDbCommand
'连接字符串
Private connStr As String
Public Function openConn() As Boolean
Dim flag As Boolean
Try
conn = New OleDb.OleDbConnection(connStr)
conn.Open()
flag = True
Catch ex As Exception
Debug.Print(ex.Message.ToString)
flag = False
End Try
Return flag
End Function
Public Function add(ByVal id As String, ByVal username As String, ByVal pass As String) As Boolean
Dim flag As Boolean
Dim SQL As String
SQL = "insert into user values('" & id & "','" & username & "','" & pass & "')"
Debug.Print(SQL)
Try
command = New OleDb.OleDbCommand(SQL, conn)
command.ExecuteNonQuery()
flag = True
Catch ex As Exception
Debug.Print(ex.Message.ToString)
flag = False
End Try
End Function
Public Property dbConnStr() As String
Get
Return connStr
End Get
Set(ByVal value As String)
connStr = value
End Set
End Property
End Class
'单击添加按钮事件的代码如下
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim database As New DB()
Dim connStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Develop\Test.accdb;Persist Security Info=False;"
database.dbConnStr = connStr
database.openConn()
database.add(id_txt.Text(), txt_username.Text(), txt_password.Text())
End Sub
End Class
6 个解决方案
#1
SQL = "insert into user values('" & id & "','" & username & "','" & pass & "')"
能执行吗?
欢迎加入我的程序设计QQ群80532706哟
能执行吗?
欢迎加入我的程序设计QQ群80532706哟
#2
单步跟踪一下,看SQL 最后被程序连接成什么样子了
#3
先检查一下insert into的sql语句,可以放到access的查询分析器中执行一下,看是否正确...
#4
调试看看SQL是否正确,conn是否打开连接
#5
想学会开发,就得自己去实践
#6
user 是保留关键字应该用[]引起来哦
#1
SQL = "insert into user values('" & id & "','" & username & "','" & pass & "')"
能执行吗?
欢迎加入我的程序设计QQ群80532706哟
能执行吗?
欢迎加入我的程序设计QQ群80532706哟
#2
单步跟踪一下,看SQL 最后被程序连接成什么样子了
#3
先检查一下insert into的sql语句,可以放到access的查询分析器中执行一下,看是否正确...
#4
调试看看SQL是否正确,conn是否打开连接
#5
想学会开发,就得自己去实践
#6
user 是保留关键字应该用[]引起来哦