【问题标题】:SQLite 3 and Odbc Data Srource NameSQLite 3 和 Odbc 数据源名称
【发布时间】:2017-09-13 10:35:37
【问题描述】:

尊重,

如何通过 Odbc 连接字符串从 c# 连接 sqlite 数据库。我想通过数据源名称连接,所以我不想使用数据库的绝对路径。我创建了带有数据源名称“TestOdbc”的 ODBC DSN,并且数据库名称是我的 sqlite test.db 的完整路径,它位于 C:\Test\test.db。在 test.db 中有一个表 TestTable,记录很少。

我也尝试在 c# 和 SqliteConnection 中使用 ODBCConnection,但我没有运气。使用 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);
        }

【问题讨论】:

    标签: c# sqlite odbc sqlconnection


    【解决方案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);
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 2011-11-24
      • 2011-06-02
      • 1970-01-01
      • 2018-07-10
      • 2011-07-30
      • 1970-01-01
      相关资源
      最近更新 更多