2017-09-13 35 views
0

尊重,SQLite 3和Odbc Data Srource名稱

如何從c#連接sqlite數據庫到Odbc連接字符串。我想通過數據srouce名稱連接,所以我不想使用DB的絕對路徑。我創建了帶有Data Soruce Name「TestOdbc」的ODBC DSN,數據庫名稱是我的sqlite test.db的完整路徑,位於C:\ Test \ test.db。在test.db中是一個包含少量記錄的表格TestTable。

我嘗試在c#和SqliteConnection中使用ODBCConnection,但我沒有運氣。使用SqliteConnection建立連接,但是連接沒有建立到C:\ Test \ test.db我認爲新數據庫僅在:內存中創建,因爲當我嘗試從TestTable選擇記錄時出現表不存在的錯誤。

請任何sugesstion?

代碼:

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); 
     } 

回答

1

試試這個:

//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); 
      } 

     } 

enter image description here