Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot open SQLite database

Using this code:

public void InsertPlatypiRequestedRecord(string PlatypusId, string PlatypusName, DateTime invitationSentLocal)
{
    var db = new SQLiteConnection(SQLitePath);
    {
        db.CreateTable<PlatypiRequested>();
        db.RunInTransaction(() =>
            {
                db.Insert(new PlatypiRequested
                              {
                                  PlatypusId = PlatypusId,
                                  PlatypusName = PlatypusName,
                                  InvitationSentLocal = invitationSentLocal
                              });
                db.Dispose();
            });
    }
}

...I get, "SQLite.SQLiteException was unhandled by user code HResult=-2146233088 Message=Cannot create commands from unopened database"

...but attempting to add a "db.Open()" doesn't work, because there is apparently no such method.

like image 295
B. Clay Shannon-B. Crow Raven Avatar asked Dec 20 '12 02:12

B. Clay Shannon-B. Crow Raven


People also ask

How do I open a SQLite database?

Open a command prompt (cmd.exe) and 'cd' to the folder location of the SQL_SAFI. sqlite database file. run the command 'sqlite3' This should open the SQLite shell and present a screen similar to that below.

How do I fix a corrupted SQLite database?

The only possible manual method to recover a corrupt SQLite database is using the open-source DB Browser for SQL. DB Browser is an open-source utility that allows you to fix minor errors in the SQLite database files.

How do I open a .DB file in Windows 10?

Open an Access database from Windows ExplorerIn Windows Explorer, navigate to the drive or folder containing the Access database file you want to open and double-click the database. Access starts and the database is opened.


2 Answers

You are disposing the database prematurely (inside of the transaction). It is better to wrap things up inside of a "using" statement, which will dispose of the db connection:

private void InsertPlatypiRequestedRecord(string platypusId, string platypusName, DateTime invitationSentLocal)
{
    using (var db = new SQLiteConnection(SQLitePath))
    {
        db.CreateTable<PlatypiRequested>();
        db.RunInTransaction(() =>
        {
            db.Insert(new PlatypiRequested
            {
                PlatypusId = platypusId,
                PlatypusName = platypusName,
                InvitationSentLocal = invitationSentLocal
            });
        });
    }
}
like image 58
chue x Avatar answered Sep 19 '22 22:09

chue x


string connecString = @"Data Source=D:\SQLite.db;Pooling=true;FailIfMissing=false";       
/*D:\sqlite.db就是sqlite数据库所在的目录,它的名字你可以随便改的*/
SQLiteConnection conn = new SQLiteConnection(connectString); //新建一个连接
conn.Open();  //打开连接
SQLiteCommand cmd = conn.CreateCommand();

cmd.CommandText = "select * from orders";   //数据库中要事先有个orders表

cmd.CommandType = CommandType.Text;
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
   while (reader.Read())
                Console.WriteLine( reader[0].ToString());
}

you can download System.Data.SQLite.dll here

here is a chinese article for csharp connect sqlite

like image 42
yukaizhao Avatar answered Sep 19 '22 22:09

yukaizhao