Как заполнить сетку данных из SQLite? - PullRequest
0 голосов
/ 11 ноября 2018

После того, как я создал базу данных 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();
        }

Все хорошо, база данных создана, таблица создана и некоторые данные вставлены в таблицу. (Я проверяю все с помощью SQLite Manager на MozzilaFirefox)

Моя цель - заполнить 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 исключает, что таблицы «аэропорты» не существует, и на самом деле вся база данных становится пустой.

Как это исправить?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...