如何从SQLite填充数据网格?

时间:2018-11-11 09:01:22

标签: c# database wpf sqlite datagrid

在按钮上单击创建SQLite数据库后,代码如下:

private void button_Click(object sender, RoutedEventArgs e)
        {
            SQLiteConnection sqlite_conn;
            SQLiteCommand sqlite_cmd;
            SQLiteDataReader sqlite_datareader;

            // create a new database connection:
            sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");

            // open the connection:
            sqlite_conn.Open();

            // create a new SQL command:
            sqlite_cmd = sqlite_conn.CreateCommand();

            // Let the SQLiteCommand object know our SQL-Query:
            sqlite_cmd.CommandText = "CREATE TABLE airports (id integer primary key , airportName varchar(100) not null, airportCity varchar(45) not null, active tinyint(1));";

            // Now lets execute the SQL ;D
            sqlite_cmd.ExecuteNonQuery();

            // Lets insert something into our new table:
            sqlite_cmd.CommandText = "INSERT INTO airports (id, airportName, airportCity, active) VALUES (1, 'TestAirport1', 'TestCity1', 1);";

            // And execute this again ;D
            sqlite_cmd.ExecuteNonQuery();

            // ...and inserting another line:
            sqlite_cmd.CommandText = "INSERT INTO airports (id, airportName, airportCity, active) VALUES (2, 'TestAirport2', 'TestCity2', 1);";

            // And execute this again ;D
            sqlite_cmd.ExecuteNonQuery();

            // But how do we read something out of our table ?
            // First lets build a SQL-Query again:
            sqlite_cmd.CommandText = "SELECT * FROM airports";

            // Now the SQLiteCommand object can give us a DataReader-Object:
            sqlite_datareader = sqlite_cmd.ExecuteReader();

            // The SQLiteDataReader allows us to run through the result lines:
            while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
            {
                // Print out the content of the text field:
                //System.Console.WriteLine(sqlite_datareader["text"]);
            }

            // We are ready, now lets cleanup and close our connection:
            sqlite_conn.Close();
        }

一切都很好,可以创建数据库,可以创建表,并且可以向表中插入一些数据。 (我在MozzilaFirefox上使用SQLite Manager进行了全部检查)

我的目标是在按钮单击时用以下数据库中的数据填充DataGrid:

private void btnShow_Click(object sender, RoutedEventArgs e)
        {

            string database_connection = "Data Source=database.db;Version=3;New=True;";
            string query = String.Format("SELECT * FROM airports WHERE active = '1'");

            SQLiteConnection connection = new SQLiteConnection(database_connection);
            connection.Open();

            SQLiteCommand command = new SQLiteCommand(query, connection);
            SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);

            DataTable data = new DataTable();
            dataAdapter.Fill(data);
            dgAirports.DataContext = data;

        }

但是随后Visual Studio抛出异常,表'airports'不存在,实际上,整个数据库都为空。

该如何解决?

0 个答案:

没有答案