Уважать,
Как подключить базу данных sqlite из c # через строку подключения Odbc. Я хочу подключиться через имя источника данных, поэтому я не хочу использовать абсолютный путь для БД. Я создал ODBC DSN с именем источника данных «TestOdbc», а имя базы данных - это полный путь к моему sqlite test.db, который находится в C: \ Test \ test.db. В test.db есть одна таблица TestTable с несколькими записями.
Я пытаюсь использовать ODBCConnection в C # и SqliteConnection, но мне не повезло. С SqliteConnection я устанавливаю соединение, но соединение не было установлено с C: \ Test \ test.db. Я думаю, что новая база данных создается только в: memory, потому что, когда я пытаюсь выбрать записи из TestTable, я получил ошибку, что таблица не существует.
Пожалуйста, есть какие-нибудь предложения?
Код:
try
{
SQLiteConnection conn = new SQLiteConnection();
conn.ConnectionString = "Driver=SQLite3 ODBC Driver;Datasource=TestOdbc;";
conn.Open();
SQLiteCommand comm = new SQLiteCommand();
comm.Connection = conn;
comm.CommandText = "SELECT * FROM TestTable";
SQLiteDataReader created = comm.ExecuteReader();
comm.Dispose();
conn.Close();
Console.WriteLine("connection opened!!!");
}
catch(SQLiteException ex)
{
Console.WriteLine(ex.Message);
}
catch(InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}