**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.Data.SqliteClient / Test / SqliteTest.cs
blobe397579d06c533bcfb109e8074049b5f68d9a333
1 //
2 // SqliteTest.cs - Test for the Sqlite ADO.NET Provider in Mono.Data.SqliteClient
3 // This provider works on Linux and Windows and uses the native
4 // sqlite.dll or sqlite.so library.
5 //
6 // Modify or add to this test as needed...
7 //
8 // SQL Lite can be downloaded from
9 // http://www.hwaci.com/sw/sqlite/download.html
11 // There are binaries for Windows and Linux.
13 // To compile:
14 // mcs SqliteTest.cs -r System.Data.dll -r Mono.Data.SqliteClient.dll
16 // Author:
17 // Daniel Morgan <danmorg@sc.rr.com>
20 using System;
21 using System.Data;
22 using Mono.Data.SqliteClient;
24 namespace Test.Mono.Data.SqliteClient
26 class SqliteTest
28 [STAThread]
29 static void Main(string[] args)
31 Console.WriteLine("If this test works, you should get:");
32 Console.WriteLine("Data 1: 5");
33 Console.WriteLine("Data 2: Mono");
35 Console.WriteLine("create SqliteConnection...");
36 SqliteConnection dbcon = new SqliteConnection();
38 // the connection string is a URL that points
39 // to a file. If the file does not exist, a
40 // file is created.
42 // "URI=file:some/path"
43 string connectionString =
44 "URI=file:SqliteTest.db";
45 Console.WriteLine("setting ConnectionString using: " +
46 connectionString);
47 dbcon.ConnectionString = connectionString;
49 Console.WriteLine("open the connection...");
50 dbcon.Open();
52 Console.WriteLine("create SqliteCommand to CREATE TABLE MONO_TEST");
53 SqliteCommand dbcmd = new SqliteCommand();
54 dbcmd.Connection = dbcon;
56 dbcmd.CommandText =
57 "CREATE TABLE MONO_TEST ( " +
58 "NID INT, " +
59 "NDESC TEXT )";
60 Console.WriteLine("execute command...");
61 dbcmd.ExecuteNonQuery();
63 Console.WriteLine("set and execute command to INSERT INTO MONO_TEST");
64 dbcmd.CommandText =
65 "INSERT INTO MONO_TEST " +
66 "(NID, NDESC )"+
67 "VALUES(5,'Mono')";
68 dbcmd.ExecuteNonQuery();
70 Console.WriteLine("set command to SELECT FROM MONO_TEST");
71 dbcmd.CommandText =
72 "SELECT * FROM MONO_TEST";
73 SqliteDataReader reader;
74 Console.WriteLine("execute reader...");
75 reader = dbcmd.ExecuteReader();
77 Console.WriteLine("read and display data...");
78 while(reader.Read()) {
79 Console.WriteLine("Data 1: " + reader[0].ToString());
80 Console.WriteLine("Data 2: " + reader[1].ToString());
83 Console.WriteLine("read and display data using DataAdapter...");
84 SqliteDataAdapter adapter = new SqliteDataAdapter("SELECT * FROM MONO_TEST", connectionString);
85 DataSet dataset = new DataSet();
86 adapter.Fill(dataset);
87 foreach(DataTable myTable in dataset.Tables){
88 foreach(DataRow myRow in myTable.Rows){
89 foreach (DataColumn myColumn in myTable.Columns){
90 Console.WriteLine(myRow[myColumn]);
96 Console.WriteLine("clean up...");
97 dataset.Dispose();
98 adapter.Dispose();
99 reader.Close();
100 dbcmd.Dispose();
101 dbcon.Close();
103 Console.WriteLine("Done.");