2017-09-13 6 views
0

Respect,SQLite 3 et Odbc Data Srource Nom

Comment connecter la base de données sqlite à partir de la chaîne de connexion C# over Odbc. Je veux me connecter au nom de srouce de données ainsi je ne veux pas utiliser le chemin absolu pour DB. J'ai créé ODBC DSN avec Data Soruce Name "TestOdbc" et le nom de la base de données est le chemin d'accès complet à mon test.db sqlite qui se trouve à C: \ Test \ test.db. Dans test.db est une table TestTable avec quelques enregistrements.

J'essaie d'utiliser ODBCConnection dans C# et SqliteConnection mais je n'ai pas de chance. Avec SqliteConnection j'établis la connexion mais mais la connexion n'a pas été établie à C: \ Test \ test.db Je pense que la nouvelle base de données est créée seulement dans: la mémoire parce que quand j'essaye de sélectionner des enregistrements de TestTable j'ai l'erreur cette table n'existe pas.

S'il vous plaît une suggestion?

code:

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

Répondre

1

Essayez ceci:

//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