Create and Connect to SQLite Database (DB) in C#

Create and Connect to SQLite Database (DB) in C#

rvtquestions
Advocate Advocate
2,525 Views
5 Replies
Message 1 of 6

Create and Connect to SQLite Database (DB) in C#

rvtquestions
Advocate
Advocate

As the title suggests I need to create and connect to a SQL DB to start. I know there are many specific forums for it but I am here because I have gotten it to fully work in the Revit context via Python but having difficulty translating it to C# in context to the Revit API. 

 

I can create a new db by:

                string filePath = @"C:\Users\User\Desktop\Test.db";
                
                SQLiteConnection.CreateFile(filePath);

But when I try to open the connection, it does not work.  (Code just terminates/fails).

string ds = "Data Source=" + filePath + ";Version=3;";
SQLiteConnection c = new SQLiteConnection(ds);
c.Open();

I've tried many configurations, such as:

  • placing the code in a try/catch block
  • formatting the ds string different ways
  • writing the connection line with a using statement

but they all fail. This should be the simplest step but it appears I am missing something. Can anyone who has gotten SQLite connections to work via C# shed any light to this? Thank you.

0 Likes
2,526 Views
5 Replies
Replies (5)
Message 2 of 6

jeremy_tammik
Alumni
Alumni

Fundamentally there is no significant difference between .NET in Python and C#.

 

Can you share a minimal complete Python sample code snippet that successfully completes the db connection?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 6

MarryTookMyCoffe
Collaborator
Collaborator

I use sqlite in my revit app and it work like charm, but I only put path to connection. What nuget do you use?
try doing it in application out side revit to see where problem is.

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug
0 Likes
Message 4 of 6

rvtquestions
Advocate
Advocate

Hi Jeremy! Sure, in Python the aforementioned steps would be easily achieved by writing:

 

import sqlite3

conn = sqlite3.connect(r'C:\Users\User\Desktop\Test.db')

and then you can follow by executing lines of commands but that is beyond the scope of the initial question.

0 Likes
Message 5 of 6

rvtquestions
Advocate
Advocate

I believe the packages are as follows:

 

System.Data.SQLite v.1.0.115

System.Data.SQLite.Core v.1.0.115.5

0 Likes
Message 6 of 6

jeremy_tammik
Alumni
Alumni

I would suggest taking a look at the internal implementation of the `connect` method in the Python sqlite3 package. It is open source, isn't it? Then, you can simply reproduce the same steps in C#.

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes