3

I am using SQLite ADO.NET Provider.

I want to create a database with 2 tables via code in vb.net .

Please provide code for the same.

I am using VS 2010 Winforms, working under XP SP3 Pro

2 Answers 2

3

Use the SQLiteConnection's CreateFile() method.

SQLiteConnection.CreateFile("c:\\mydatabasefile.db3")

More info on the System.Data.SQLite forums

You can then send ad-hoc CREATE TABLE statements to the engine:

dim myTableCreate as String = 
                    "CREATE TABLE MyTable(CustomerID INTEGER PRIMARY KEY ASC, 
                     FirstName VARCHAR(25));"

cmd.CommandText = myTableCreate
cmd.ExecuteNonQuery()

More on SQLite CREATE TABLE.

Sign up to request clarification or add additional context in comments.

6 Comments

Are you sure this is right?, I am getting a lot of errors when I try to create a table.
Is it necessary to put a semi-colon at the end of the 1st statement
I don't think the semi-colon is strictly necessary or required. It's a command terminator. As for the CREATE statement, here's another example site: devdaily.com/android/sqlite-create-table-insert-syntax-examples
a semi-colon is good practice and is in the ANSI standard for SQL statements.. as of MSSQL2014 (??) it will be required after every statement..
@Ads the semi-colon is not required in TSQL in SQL Server 2014.
|
1

For those who need this, here is an updated working code...

SQLiteConnection.CreateFile("c:\mydatabasefile.db3")
Using Query As New SQLiteCommand()
    Connection.ConnectionString ="DataSource=c:\mydatabasefile.db3;Version=3;New=False;Compress=True;"
    Connection.Open()
        With Query 
            .Connection = Connection
            .CommandText = "CREATE TABLE MyTable(CustomerID INTEGER PRIMARY KEY ASC, FirstName VARCHAR(25))"
        End With
Query.ExecuteNonQuery()
Connection.Close()
End Using

1 Comment

I think DataSource should be Data Source. Thanks anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.