以下代码假设创建一个数据库和一个带有“FirstName”列的表,该列被赋值为“James”
Imports System.Data.SQLite
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SQLiteConnection.CreateFile("c:\mydatabasefile.db3")
Dim sqlConnection As New SQLite.SQLiteConnection()
Dim sqlCommand As New SQLiteCommand
sqlConnection.ConnectionString = "Data Source=c:\mydatabasefile.db3"
sqlConnection.Open()
sqlCommand.CommandText = "CREATE TABLE MyTable(EmpID INTEGER PRIMARY KEY ASC, FirstName VARCHAR(25));"
sqlCommand.CommandText = "INSERT INTO MyTable(FirstName) VALUES('James');"
sqlConnection.Close()
End Sub
End Class
但由于某种原因,应用程序只创建一个大小为0字节的数据库。为什么不起作用。我正在使用我正在使用SQLite ADO.NET Provider。 ,VS 2010,Winforms
答案 0 :(得分:2)
您永远不会执行sql命令
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SQLiteConnection.CreateFile("c:\mydatabasefile.db3")
Dim sqlConnection As New SQLite.SQLiteConnection()
Dim sqlCommand As New SQLiteCommand("", sqlConnection)
sqlConnection.ConnectionString = "Data Source=c:\mydatabasefile.db3"
sqlConnection.Open()
sqlCommand.CommandText = "CREATE TABLE MyTable(EmpID INTEGER PRIMARY KEY ASC, FirstName VARCHAR(25));"
sqlCommand.ExecuteNonQuery()
sqlCommand.CommandText = "INSERT INTO MyTable(FirstName) VALUES('James');"
sqlCommand.ExecuteNonQuery()
sqlConnection.Close()
End Sub
End Class
您还需要设置命令连接,您也可以通过
来设置 sqlCommand.Connection = sqlConnection
这应该会解决你的问题
答案 1 :(得分:1)
您永远不会执行sqlCommands。你应该使用.ExecuteNonQuery()来获取数据库中的内容,否则什么都不会做。
答案 2 :(得分:1)
还要让命令知道要使用的连接。 sqlCommand.Connection = sqlConnection