Esempio n. 1
0
        public void TestCommands()
        {
            // In-memory database only exists while the connection is open
            var connection = new SQLiteConnection("DataSource=:memory:");

            {
                var cmd = connection.AssureOpen().CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "CREATE TABLE tester (id INTEGER, name varchar(100))";
                cmd.ExecuteNonQuery();
            }
            {
                var cmd = connection.AssureOpen().CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "INSERT INTO tester (id, name) VALUES(@id, @name)";
                cmd.AddCommandParameter("id", 1)
                .AddCommandParameter("@name", "hello");
                cmd.ExecuteNonQuery();
            }
            {
                var cmd = connection.AssureOpen().CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "SELECT id as Id, name as Name FROM tester where id=@id and name <> @name";
                cmd.AddCommandParameter("id", 1);
                cmd.AddParametersFromEntity(new { name = "not hello" });

                var dt = cmd.AsArrayOf <TestData>();
                Assert.AreEqual(dt.Length, 1);
                Assert.AreEqual(dt[0].Name, "hello");
                Assert.AreEqual(dt[0].Id, 1);
            }
            {
                var cmd = connection.AssureOpen().CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "SELECT id as Id, name as Name FROM tester";
                var ds = cmd.ReadAsDataSet();
                Assert.AreEqual(ds.Tables.Count, 1);
                Assert.AreEqual(ds.Tables[0].Rows[0]["name"], "hello");
            }
        }