c# ado.net sqlite

0

Witam, chciałbym stworzyć plik .db by w Visualu 2015 w WPF w języku c# mógł obsługiwać SQlite.

  1. Pobrałem https://sourceforge.net/projects/sqlite-dotnet2/
  2. Dodałem referencje i z usingowałem using System.Data.SQLite;
  3. Jak wygenerować ów bazę?

pomysł pod buttonem:

private void button_Click(object sender, RoutedEventArgs e)
        {
            SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString);
            try
            {
                sqliteCon.Open();
                string Query = "??";
                SQLiteCommand createCommand = new SQLiteCommand(Query, sqliteCon);
                createCommand.ExecuteNonQuery();
                SQLiteDataReader dr = createCommand.ExecuteReader();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            sqliteCon.Close();
        } 

Tylko co wpisać w stringa te pytajniki? Create dabase? Bardzo prosze o jakiś krótki kod z jedną tabelką chodziaż :(
Pozdrawiam Serdecznie i dziękuję za każą pomoc!

1

http://stackoverflow.com/questions/15292880/create-sqlite-database-and-table

 SQLiteConnection.CreateFile("MyDatabase.sqlite");

Tam gdzie masz ExecuteNonQuery to tak nie działa. Ta metoda robi coś w bazie ale to co robi nie daje żadnego output'a np. jak robisz SELECT * FROM ... to masz jakiś output (rezultat zapytania). Tam gdzie ?? mają być zapytania które np. dodają tabelę (create table ...) usuwają, robią update itp.

0
            SQLiteConnection.CreateFile("MyDatabase.sqlite");

            var m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
             m_dbConnection.Open();

            string sql = "create table highscores (name varchar(20), score int)";

            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

             sql = "insert into highscores (name, score) values ('Me', 9001)";

             command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            m_dbConnection.Close(); 

Próbowałem stworzyć coś takiego kierując się stackiem, ale ciągle pojawia się exception:

An unhandled exception of type 'System.BadImageFormatException' occurred in PresentationCore.dll

Additional information: Could not load file or assembly 'System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. Próbowano załadować program w niepoprawnym formacie.

0

Podmieniłem biblioteki:

System.Data.SQLite.dll -> System.Data.SQLite.DLL // (x64)
Jest jakiś chyba problem z architekturą dla 64 bitów, nie wypluwa błędu na bibliotece dla 32 bitów. stworzyło => bin\debug\MyDatabase.sqlite

Czemu tak jest? Widać że są niezgodności z frameworkiem. Zatem korzystać z (x84) dll'ki? Teoretycznie mój komp ma architekture x64 więc powinna ta druga DLL działać.

0

Ponadto pojawia się kolejne pytanie. Kod działa dla 32bit dll'ki, ale tworzyć moge wyłącznie pliki.sqlite, nie mogę natomiast .db.

error:

An unhandled exception of type 'System.BadImageFormatException' occurred in PresentationCore.dll

Additional information: Could not load file or assembly 'System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. Próbowano załadować program w niepoprawnym formacie.

Nie ma możliwości tworzenia w .db? Wydawało by się że powinno działać.

 SQLiteConnection.CreateFile("MyDatabase2.db");//sqlite

            var m_dbConnection = new SQLiteConnection("Data Source=MyDatabase2.db;Version=3;");
             m_dbConnection.Open();

            string sql = "create table highscores (name varchar(20), score int)";

            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

             sql = "insert into highscores (name, score) values ('Me', 9001)";

             command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            m_dbConnection.Close(); 

1 użytkowników online, w tym zalogowanych: 0, gości: 1