三个基本操作:
1、新建数据库(自定义函数)
'传入1个参数,数据库全路径
Dim Path As String = Args(0)
Dim b As System.Data.SQLite.SQLiteException
'成功返回1,失败返回-1
If System.IO.File.Exists(Path) = True Then
MessageBox.show("已有同名的数据库存在.")
Return -1
End If
Using myData As New System.Data.SQLite.SQLiteConnection( "data source=" & Path)
Try
myData.Open()
myData.Close()
SaveConfigValue("目标数据库",Path )
Return 1
Catch b
Throw New System.Data.SQLite.SQLiteException(b.Message)
Return -1
End Try
End Using
2、执行SQL(自定义函数)
'传入文本型,SQL语句,返回1,失败返回-1
Dim sql As String = Args(0)
Dim Path As String = GetConfigValue("目标数据库", "无") '我是将路径存放在这里,根据需要改
If Path = "无" Then
MessageBox.show("请定位数据库.")
Return Nothing
End If
'定义一个数据库连接,使用后立即释放
Using connection As New System.Data.Sqlite.SQLiteConnection("data source=" & Path)
Using cmd As New System.Data.Sqlite.SQLiteCommand(sql, connection)
Try
connection.Open()
cmd.ExecuteNonQuery()
Return 1
Catch e As System.Data.Sqlite.SQLiteException
connection.Close()
Throw New System.Data.Sqlite.SQLiteException(e.Message)
Return -1
End Try
End Using
End Using
3、取记录集(自定义函数)
'传入文本型,SQL语句,返回DataTable,失败返回Nothing
Dim sql As String = Args(0)
Dim Path As String = GetConfigValue("目标数据库", "无")
If Path = "无" Then
MessageBox.show("请定位数据库.")
Return Nothing
End If
Using connection As New System.Data.SQLite.SQLiteConnection("data source=" & Path)
Dim ds As New System.Data.DataSet()
Try
connection.Open()
Dim command As New System.Data.SQLite.SQLiteDataAdapter(sql, connection)
command.Fill(ds, "ds")
Return ds.Tables(0)
Catch ex As System.Data.SQLite.SQLiteException
Throw New Exception(ex.Message)
Return Nothing
End Try
End Using
不过,狐表的Access不需安装就可使用,没有必要用Sqlite