Debian Codegraph @

mcs/class/Mono.Data.Sqlite/Test/SqliteTest.cs

raw symbols SCIP
1 //
2 // SqliteTest.cs - Test for the Sqlite ADO.NET Provider in Mono.Data.Sqlite
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
10 //
11 // There are binaries for Windows and Linux.
12 //
13 // To compile:
14 // mcs SqliteTest.cs -r System.Data.dll -r Mono.Data.Sqlite.dll
15 //
16 // Author:
17 // Daniel Morgan <[email protected]>
18 //
19
20 using System;
21 using System.Data;
22 using Mono.Data.Sqlite;
23
24 namespace Test.Mono.Data.Sqlite
25 {
26 class SqliteTest
27 {
28 [STAThread]
29 static void Main(string[] args)
30 {
31 Test(false, null);
32 Console.WriteLine();
33 Test(false, "ISO-8859-1");
34 Console.WriteLine();
35 Test(true, null);
36 }
37
38 static void Test(bool v3, string encoding) {
39 if (!v3)
40 Console.WriteLine("Testing Version 2" + (encoding != null ? " with " + encoding + " encoding" : ""));
41 else
42 Console.WriteLine("Testing Version 3");
43
44 System.IO.File.Delete("SqliteTest.db");
45
46 SqliteConnection dbcon = new SqliteConnection();
47
48 // the connection string is a URL that points
49 // to a file. If the file does not exist, a
50 // file is created.
51
52 // "URI=file:some/path"
53 string connectionString =
54 "URI=file:SqliteTest.db";
55 if (v3)
56 connectionString += ",Version=3";
57 if (encoding != null)
58 connectionString += ",encoding=" + encoding;
59 dbcon.ConnectionString = connectionString;
60
61 dbcon.Open();
62
63 SqliteCommand dbcmd = new SqliteCommand();
64 dbcmd.Connection = dbcon;
65
66 dbcmd.CommandText =
67 "CREATE TABLE MONO_TEST ( " +
68 "NID INT, " +
69 "NDESC TEXT, " +
70 "NTIME DATETIME); " +
71 "INSERT INTO MONO_TEST " +
72 "(NID, NDESC, NTIME) " +
73 "VALUES(1,'One (unicode test: \u05D0)', '2006-01-01')";
74 Console.WriteLine("Create & insert modified rows = 1: " + dbcmd.ExecuteNonQuery());
75
76 dbcmd.CommandText =
77 "INSERT INTO MONO_TEST " +
78 "(NID, NDESC, NTIME) " +
79 "VALUES(:NID,:NDESC,:NTIME)";
80 dbcmd.Parameters.Add( new SqliteParameter("NID", 2) );
81 dbcmd.Parameters.Add( new SqliteParameter(":NDESC", "Two (unicode test: \u05D1)") );
82 dbcmd.Parameters.Add( new SqliteParameter(":NTIME", DateTime.Now) );
83 Console.WriteLine("Insert modified rows with parameters = 1, 2: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());
84
85 dbcmd.CommandText =
86 "INSERT INTO MONO_TEST " +
87 "(NID, NDESC, NTIME) " +
88 "VALUES(3,'Three, quoted parameter test, and next is null; :NTIME', NULL)";
89 Console.WriteLine("Insert with null modified rows and ID = 1, 3: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());
90
91 dbcmd.CommandText =
92 "INSERT INTO MONO_TEST " +
93 "(NID, NDESC, NTIME) " +
94 "VALUES(4,'Four with ANSI char: ü', NULL)";
95 Console.WriteLine("Insert with ANSI char ü = 1, 4: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());
96
97 dbcmd.CommandText =
98 "INSERT INTO MONO_TEST " +
99 "(NID, NDESC, NTIME) " +
100 "VALUES(?,?,?)";
101 dbcmd.Parameters.Clear();
102 IDbDataParameter param1 = dbcmd.CreateParameter();
103 param1.DbType = DbType.DateTime;
104 param1.Value = 5;
105 dbcmd.Parameters.Add(param1);
106 IDbDataParameter param2 = dbcmd.CreateParameter();
107 param2.Value = "Using unnamed parameters";
108 dbcmd.Parameters.Add(param2);
109 IDbDataParameter param3 = dbcmd.CreateParameter();
110 param3.DbType = DbType.DateTime;
111 param3.Value = DateTime.Parse("2006-05-11 11:45:00");
112 dbcmd.Parameters.Add(param3);
113 Console.WriteLine("Insert with unnamed parameters = 1, 5: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());
114
115 dbcmd.CommandText =
116 "SELECT * FROM MONO_TEST";
117 SqliteDataReader reader;
118 reader = dbcmd.ExecuteReader();
119
120 Console.WriteLine("read and display data...");
121 while(reader.Read())
122 for (int i = 0; i < reader.FieldCount; i++)
123 Console.WriteLine(" Col {0}: {1} (type: {2}, data type: {3})",
124 i, reader[i] == null ? "(null)" : reader[i].ToString(), reader[i] == null ? "(null)" : reader[i].GetType().FullName, reader.GetDataTypeName(i));
125
126 dbcmd.CommandText = "SELECT NDESC FROM MONO_TEST WHERE NID=2";
127 Console.WriteLine("read and display a scalar = 'Two': " + dbcmd.ExecuteScalar());
128
129 dbcmd.CommandText = "SELECT count(*) FROM MONO_TEST";
130 Console.WriteLine("read and display a non-column scalar = 3: " + dbcmd.ExecuteScalar());
131
132 Console.WriteLine("read and display data using DataAdapter/DataSet...");
133 SqliteDataAdapter adapter = new SqliteDataAdapter("SELECT * FROM MONO_TEST", connectionString);
134 DataSet dataset = new DataSet();
135 adapter.Fill(dataset);
136 foreach(DataTable myTable in dataset.Tables){
137 foreach(DataRow myRow in myTable.Rows){
138 foreach (DataColumn myColumn in myTable.Columns){
139 Console.WriteLine(" " + myRow[myColumn]);
140 }
141 }
142 }
143
144 /*Console.WriteLine("read and display data using DataAdapter/DataTable...");
145 DataTable dt = new DataTable();
146 adapter.Fill(dt);
147 DataView dv = new DataView(dt);
148 foreach (DataRowView myRow in dv) {
149 foreach (DataColumn myColumn in myRow.Row.Table.Columns) {
150 Console.WriteLine(" " + myRow[myColumn.ColumnName]);
151 }
152 }*/
153
154 try {
155 dbcmd.CommandText = "SELECT NDESC INVALID SYNTAX FROM MONO_TEST WHERE NID=2";
156 dbcmd.ExecuteNonQuery();
157 Console.WriteLine("Should not reach here.");
158 } catch (Exception e) {
159 Console.WriteLine("Testing a syntax error: " + e.GetType().Name + ": " + e.Message);
160 }
161
162 /*try {
163 dbcmd.CommandText = "SELECT 0/0 FROM MONO_TEST WHERE NID=2";
164 Console.WriteLine("Should not reach here: " + dbcmd.ExecuteScalar());
165 } catch (Exception e) {
166 Console.WriteLine("Testing an execution error: " + e.GetType().Name + ": " + e.Message);
167 }*/
168
169 dataset.Dispose();
170 adapter.Dispose();
171 reader.Close();
172 dbcmd.Dispose();
173 dbcon.Close();
174 }
175 }
176 }