J
J
John2018-03-13 10:06:59
C++ / C#
John, 2018-03-13 10:06:59

How in C# can you determine at the connection stage (before select) whether this is a sqlite3 database or some other file?

I'm connecting to an arbitrary file. I thought an exception would be thrown at the .Open level, but no, as if everything is fine.

SQLiteConnection dbConn = new SQLiteConnection("Data Source=" + dbPath + ";Version=3;Password=" + Pass + ";");
SQLiteCommand sqlCmd = new SQLiteCommand();
dbConn.Open();
sqlCmd.Connection = dbConn;

But already when I make a request, an exception flies that this is not a database
string sqlQuery = "...................";
SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlQuery, dbConn);
DataTab.Clear();
adapter.Fill(DataTab); //Тут вылетает исключение

Are there any options before the select to understand whether the database is sqlite3 or something else, or maybe there is a problem with access (for example, the password is not correct)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#, 2018-03-13
@mindtester

1 - according to your idea, you will have to open the file, check the signature (see cicatrix's answer), close it if successful
. if there are problems with access, there will still be exceptions, but is it the knowledge of whether this is a base? don't you get
me to the fact that the volume of ancillary work will grow, and more than it was worth
it can still just handle exceptions? As far as I remember .. access rights exceptions are quite recognized. I think the library exceptions for sqlite3 are also quite sane

C
cicatrix, 2018-03-13
@cicatrix

SQLite Database File format
See section 1.2 1.2. The Database Header
Offset 0, length 16 bytes string: "SQLite format 3\000"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question