C# SQLite 3和Odbc数据源名称
尊重, 如何通过Odbc连接字符串从c连接sqlite数据库。我想通过数据源名称进行连接,所以我不想对数据库使用绝对路径。我已经创建了ODBC DSN,其数据名为TestOdbc,数据库名是我的sqlite test.db的完整路径,它位于C:\test\test.db。在test.db中,只有一个表TestTable,记录很少 我也尝试在c和SqliteConnection中使用ODBCConnection,但我没有运气。使用SqliteConnection,我建立了连接,但是没有建立到C:\Test\Test.db的连接,我认为新数据库只在:内存中创建,因为当我尝试从TestTable中选择记录时,我得到了一个错误,即表不存在 有什么建议吗 代码: 试试这个:C# SQLite 3和Odbc数据源名称,c#,sqlite,odbc,sqlconnection,C#,Sqlite,Odbc,Sqlconnection,尊重, 如何通过Odbc连接字符串从c连接sqlite数据库。我想通过数据源名称进行连接,所以我不想对数据库使用绝对路径。我已经创建了ODBC DSN,其数据名为TestOdbc,数据库名是我的sqlite test.db的完整路径,它位于C:\test\test.db。在test.db中,只有一个表TestTable,记录很少 我也尝试在c和SqliteConnection中使用ODBCConnection,但我没有运气。使用SqliteConnection,我建立了连接,但是没有建立到C:\
//create table and insert data
private void button1_Click(object sender, EventArgs e)
{
// We use these three SQLite objects:
SQLiteConnection conn;
SQLiteCommand sqlite_cmd;
SQLiteDataReader sqlite_datareader;
// create a new database connection:
conn = new SQLiteConnection("Driver = SQLite3 ODBC Driver; Datasource = TestOdbc;Version = 3;New=True;Compress=True;");
// open the connection:
conn.Open();
// create a new SQL command:
sqlite_cmd = conn.CreateCommand();
// Let the SQLiteCommand object know our SQL-Query:
sqlite_cmd.CommandText = "CREATE TABLE test2 (id integer primary key, text varchar(100));";
// Now lets execute the SQL ;D
sqlite_cmd.ExecuteNonQuery();
// Lets insert something into our new table:
sqlite_cmd.CommandText = "INSERT INTO test2 (id, text) VALUES (1, 'Test Text 1');";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
label4.Text = "test2";
label5.Text = "TestOdbc";
// We are ready, now lets cleanup and close our connection:
conn.Close();
}
//show inseted value from sqlite
private void button2_Click(object sender, EventArgs e)
{
try
{
conn.ConnectionString = "Driver=SQLite3 ODBC Driver;Datasource=TestOdbc;Version = 3;New=True;Compress=True;";
conn.Open();
SQLiteCommand comm = new SQLiteCommand();
comm.Connection = conn;
comm.CommandText = "SELECT * FROM test2";
SQLiteDataReader created = comm.ExecuteReader();
MessageBox.Show("connection opened!!!");
while (created.Read()) // Read() returns true if there is still a result line to read
{
// Print out the content of the text field:
string myreader = "";
try
{
myreader = created[1].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
label3.Text=" "+myreader;
}
comm.Dispose();
conn.Close();
}
catch (SQLiteException ex)
{
MessageBox.Show(ex.Message);
}
catch (InvalidOperationException ex)
{
MessageBox.Show(ex.Message);
}
}
试试这个:
//create table and insert data
private void button1_Click(object sender, EventArgs e)
{
// We use these three SQLite objects:
SQLiteConnection conn;
SQLiteCommand sqlite_cmd;
SQLiteDataReader sqlite_datareader;
// create a new database connection:
conn = new SQLiteConnection("Driver = SQLite3 ODBC Driver; Datasource = TestOdbc;Version = 3;New=True;Compress=True;");
// open the connection:
conn.Open();
// create a new SQL command:
sqlite_cmd = conn.CreateCommand();
// Let the SQLiteCommand object know our SQL-Query:
sqlite_cmd.CommandText = "CREATE TABLE test2 (id integer primary key, text varchar(100));";
// Now lets execute the SQL ;D
sqlite_cmd.ExecuteNonQuery();
// Lets insert something into our new table:
sqlite_cmd.CommandText = "INSERT INTO test2 (id, text) VALUES (1, 'Test Text 1');";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
label4.Text = "test2";
label5.Text = "TestOdbc";
// We are ready, now lets cleanup and close our connection:
conn.Close();
}
//show inseted value from sqlite
private void button2_Click(object sender, EventArgs e)
{
try
{
conn.ConnectionString = "Driver=SQLite3 ODBC Driver;Datasource=TestOdbc;Version = 3;New=True;Compress=True;";
conn.Open();
SQLiteCommand comm = new SQLiteCommand();
comm.Connection = conn;
comm.CommandText = "SELECT * FROM test2";
SQLiteDataReader created = comm.ExecuteReader();
MessageBox.Show("connection opened!!!");
while (created.Read()) // Read() returns true if there is still a result line to read
{
// Print out the content of the text field:
string myreader = "";
try
{
myreader = created[1].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
label3.Text=" "+myreader;
}
comm.Dispose();
conn.Close();
}
catch (SQLiteException ex)
{
MessageBox.Show(ex.Message);
}
catch (InvalidOperationException ex)
{
MessageBox.Show(ex.Message);
}
}