示例#1
0
        public void ExecuteQueryWithParametersTest()
        {
            var tableName  = "testingTable" + HelperObjectFactory.GetRandomInt(0);
            var parameters = new List <SQLiteParameter>();
            var aParameter = SqliteExtensions.Create("a", DbType.String, "string1");
            var bParameter = SqliteExtensions.Create("b", DbType.Int32, 32);

            parameters.Add(aParameter);
            parameters.Add(bParameter);

            DatabaseWrapper.ExecuteSql($"CREATE TABLE {tableName}(a, b);");
            using (var c = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT a FROM {tableName} WHERE a = 'nothing';"))
            {
                Assert.IsTrue(c.IsEmpty);
            }

            DatabaseWrapper.ExecuteSqlWithParameters($"INSERT INTO {tableName}(a, b) VALUES ({aParameter.ParameterName}, {bParameter.ParameterName});", parameters);
            using (var c = DatabaseWrapper.ExecuteQueryWithParameters($"SELECT a FROM {tableName} WHERE a = {aParameter.ParameterName};", parameters))
            {
                Assert.AreEqual(c.GetString(0), "string1");
            }

            aParameter = SqliteExtensions.Create("a", DbType.String, "string2");
            bParameter = SqliteExtensions.Create("b", DbType.Int32, 64);
            DatabaseWrapper.ExecuteSqlWithParameters($"INSERT INTO {tableName}(a, b) VALUES ({aParameter.ParameterName}, {bParameter.ParameterName});", parameters);
            using (var c = DatabaseWrapper.ExecuteQueryWithParameters($"SELECT b FROM {tableName} WHERE b = {bParameter.ParameterName};", parameters))
            {
                Assert.AreEqual(32, c.GetInt(0));
            }

            DatabaseWrapper.ExecuteSql($"DROP TABLE {tableName};");
        }