Per Erik Strandberg /cv /kurser /blog

Apparently Mono has Sqlite inside it from scratch! See [1] for more.

This little example illustrates this.

First create a database

We create a silly little table with one value.

C:\tmp>sqlite3 SqliteTest.db
sqlite> CREATE TABLE employee (
   ...>    firstname varchar(32),
   ...>    lastname varchar(32));
sqlite> insert into employee values("foobar","bletch");
sqlite> .exit

The code

I copy-pasted it from the mono page referenced above.

 using System;
 using System.Data;
 using Mono.Data.SqliteClient;

 public class Test
 {
    public static void Main(string[] args)
    {
       string connectionString = "URI=file:SqliteTest.db";
       IDbConnection dbcon;
       dbcon = (IDbConnection) new SqliteConnection(connectionString);
       dbcon.Open();
       IDbCommand dbcmd = dbcon.CreateCommand();
       // requires a table to be created named employee
       // with columns firstname and lastname
       // such as,
       //        CREATE TABLE employee (
       //           firstname varchar(32),
       //           lastname varchar(32));
       string sql =
          "SELECT firstname, lastname " +
          "FROM employee";
       dbcmd.CommandText = sql;
       IDataReader reader = dbcmd.ExecuteReader();
       while(reader.Read()) {
            string FirstName = reader.GetString (0);
            string LastName = reader.GetString (1);
            Console.WriteLine("Name: " +
                FirstName + " " + LastName);
       }
       // clean up
       reader.Close();
       reader = null;
       dbcmd.Dispose();
       dbcmd = null;
       dbcon.Close();
       dbcon = null;
    }
 }

I compiled it on the command line with these references:

C:\tmp>mcs --version
Mono C# compiler version 2.2.0.0

C:\tmp>mcs mono-sqlite.cs -r:System.Data.dll -r:Mono.Data.SqliteClient.dll

And it runs really nice.

C:\tmp>mono mono-sqlite.exe
Name: foobar bletch


See Also Csharp And Sqlite


Tillhör Kategori Programmering