本文整理汇总了C#中LinqToDB.Data.DataConnection.Execute方法的典型用法代码示例。如果您正苦于以下问题:C# DataConnection.Execute方法的具体用法?C# DataConnection.Execute怎么用?C# DataConnection.Execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LinqToDB.Data.DataConnection
的用法示例。
在下文中一共展示了DataConnection.Execute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EnumExecuteScalarTest
public void EnumExecuteScalarTest()
{
using (var dbm = new DataConnection())
{
var gender = dbm.Execute<Gender>("select 'M'");
Assert.That(gender, Is.EqualTo(Gender.Male));
}
}
示例2: TestBinary
public void TestBinary([IncludeDataContexts(CurrentProvider)] string context)
{
var arr1 = new byte[] { 48, 57 };
using (var conn = new DataConnection(context))
{
Assert.That(conn.Execute<byte[]>("SELECT @p", DataParameter.Binary ("p", arr1)), Is.EqualTo(arr1));
Assert.That(conn.Execute<byte[]>("SELECT @p", DataParameter.VarBinary("p", arr1)), Is.EqualTo(arr1));
Assert.That(conn.Execute<byte[]>("SELECT @p", DataParameter.Create ("p", arr1)), Is.EqualTo(arr1));
Assert.That(conn.Execute<byte[]>("SELECT @p", DataParameter.VarBinary("p", null)), Is.EqualTo(null));
Assert.That(conn.Execute<byte[]>("SELECT @p", DataParameter.VarBinary("p", new byte[0])), Is.EqualTo(new byte[0]));
Assert.That(conn.Execute<byte[]>("SELECT @p", DataParameter.Image ("p", new byte[0])), Is.EqualTo(new byte[0]));
Assert.That(conn.Execute<byte[]>("SELECT @p", new DataParameter { Name = "p", Value = arr1 }), Is.EqualTo(arr1));
Assert.That(conn.Execute<byte[]>("SELECT @p", DataParameter.Create ("p", new Binary(arr1))), Is.EqualTo(arr1));
Assert.That(conn.Execute<byte[]>("SELECT @p", new DataParameter("p", new Binary(arr1))), Is.EqualTo(arr1));
}
}
示例3: TestParameters
public void TestParameters(string context)
{
using (var conn = new DataConnection(context))
{
Assert.That(conn.Execute<string>("SELECT @p", new { p = 1 }), Is.EqualTo("1"));
Assert.That(conn.Execute<string>("SELECT @p", new { p = "1" }), Is.EqualTo("1"));
Assert.That(conn.Execute<int> ("SELECT @p", new { p = new DataParameter { Value = 1 } }), Is.EqualTo(1));
Assert.That(conn.Execute<string>("SELECT @p1", new { p1 = new DataParameter { Value = "1" } }), Is.EqualTo("1"));
Assert.That(conn.Execute<int> ("SELECT @p1 + ?p2", new { p1 = 2, p2 = 3 }), Is.EqualTo(5));
Assert.That(conn.Execute<int> ("SELECT @p2 + ?p1", new { p2 = 2, p1 = 3 }), Is.EqualTo(5));
}
}
示例4: TestDataParameterMapping3
public void TestDataParameterMapping3()
{
var ms = new MappingSchema();
ms.SetConvertExpression<TwoValues,DataParameter>(tv =>
new DataParameter
{
Value = tv == null ? (long?)null : (long)tv.Value1 << 32 | tv.Value2,
DataType = DataType.Int64
},
false);
using (var conn = new DataConnection().AddMappingSchema(ms))
{
var n = conn.Execute<long?>("SELECT @p", new { p = (TwoValues)null });
Assert.AreEqual(null, n);
}
}
示例5: TestDataParameterMapping1
public void TestDataParameterMapping1()
{
var ms = new MappingSchema();
ms.SetConvertExpression<TwoValues,DataParameter>(tv => new DataParameter { Value = (long)tv.Value1 << 32 | tv.Value2 });
using (var conn = new DataConnection().AddMappingSchema(ms))
{
var n = conn.Execute<long>("SELECT @p", new { p = new TwoValues { Value1 = 1, Value2 = 2 }});
Assert.AreEqual(1L << 32 | 2, n);
}
}
示例6: TestObject6
public void TestObject6()
{
using (var conn = new DataConnection())
{
Assert.That(conn.Execute<string>(
"SELECT @p",
new
{
p1 = new DataParameter { Name = "p", DataType = DataType.Char, Value = "123" },
p2 = 1
}), Is.EqualTo("123"));
}
}
示例7: TestObject5
public void TestObject5()
{
using (var conn = new DataConnection())
{
var res = conn.Execute<string>(
"SELECT @p",
new
{
p = new DataParameter { DataType = DataType.VarChar, Value = "123" },
p1 = 1
});
Assert.That(res, Is.EqualTo("123"));
}
}
示例8: CacheTest
public void CacheTest(string context)
{
using (var dc= new DataConnection(context))
{
dc.Execute("CREATE TABLE #t1(v1 int not null)");
dc.Execute("INSERT INTO #t1(v1) values (1)");
var v1 = dc.Query<object>("SELECT v1 FROM #t1").ToList();
dc.Execute("ALTER TABLE #t1 ALTER COLUMN v1 INT NULL");
DataConnection.ClearObjectReaderCache();
dc.Execute("INSERT INTO #t1(v1) VALUES (null)");
var v2 = dc.Query<object>("SELECT v1 FROM #t1").ToList();
}
}
示例9: TestBinaryFromDb
public void TestBinaryFromDb(string context)
{
var arr = new byte[] {97, 98, 99, 100, 101, 102, 103, 104};
using (var conn = new DataConnection(context))
{
Assert.That(conn.Execute<byte[]>("SELECT \"binaryDataType\" from \"AllTypes\" WHERE ID=2"), Is.EqualTo(arr));
Assert.That(conn.Execute<byte[]>("SELECT \"varbinaryDataType\" from \"AllTypes\" WHERE ID=2"), Is.EqualTo(arr));
}
}
示例10: TestString
public void TestString(string context)
{
using (var conn = new DataConnection(context))
{
Assert.That(conn.Execute<string>("SELECT Cast('12345' as char(20))"), Is.EqualTo("12345"));
Assert.That(conn.Execute<string>("SELECT Cast(NULL as char(20))"), Is.Null);
Assert.That(conn.Execute<string>("SELECT @p", DataParameter.Char ("p", "123")), Is.EqualTo("123"));
Assert.That(conn.Execute<string>("SELECT @p", DataParameter.VarChar ("p", "123")), Is.EqualTo("123"));
Assert.That(conn.Execute<string>("SELECT @p", DataParameter.Text ("p", "123")), Is.EqualTo("123"));
Assert.That(conn.Execute<string>("SELECT @p", DataParameter.NChar ("p", "123")), Is.EqualTo("123"));
Assert.That(conn.Execute<string>("SELECT @p", DataParameter.NVarChar("p", "123")), Is.EqualTo("123"));
Assert.That(conn.Execute<string>("SELECT @p", DataParameter.NText ("p", "123")), Is.EqualTo("123"));
Assert.That(conn.Execute<string>("SELECT @p", DataParameter.Create ("p", "123")), Is.EqualTo("123"));
Assert.That(conn.Execute<string>("SELECT @p", new DataParameter { Name = "p", Value = "1" }), Is.EqualTo("1"));
}
}
示例11: TestEnum2
public void TestEnum2(string context)
{
using (var conn = new DataConnection(context))
{
Assert.That(conn.Execute<string>("SELECT @p", new { p = TestEnum.AA }), Is.EqualTo("A"));
Assert.That(conn.Execute<string>("SELECT @p", new { p = (TestEnum?)TestEnum.BB }), Is.EqualTo("B"));
Assert.That(conn.Execute<string>("SELECT @p", new { p = ConvertTo<string>.From((TestEnum?)TestEnum.AA) }), Is.EqualTo("A"));
Assert.That(conn.Execute<string>("SELECT @p", new { p = ConvertTo<string>.From(TestEnum.AA) }), Is.EqualTo("A"));
Assert.That(conn.Execute<string>("SELECT @p", new { p = conn.MappingSchema.GetConverter<TestEnum?,string>()(TestEnum.AA) }), Is.EqualTo("A"));
}
}
示例12: TestEnum1
public void TestEnum1(string context)
{
using (var conn = new DataConnection(context))
{
Assert.That(conn.Execute<TestEnum> ("SELECT 'A'"), Is.EqualTo(TestEnum.AA));
Assert.That(conn.Execute<TestEnum?>("SELECT 'A'"), Is.EqualTo(TestEnum.AA));
Assert.That(conn.Execute<TestEnum> ("SELECT 'B'"), Is.EqualTo(TestEnum.BB));
Assert.That(conn.Execute<TestEnum?>("SELECT 'B'"), Is.EqualTo(TestEnum.BB));
}
}
示例13: TestDateTime
public void TestDateTime(string context)
{
using (var conn = new DataConnection(context))
{
var dateTime = new DateTime(2012, 12, 12, 12, 12, 12);
Assert.That(conn.Execute<DateTime> ("SELECT Cast('2012-12-12 12:12:12' as datetime)"), Is.EqualTo(dateTime));
Assert.That(conn.Execute<DateTime?>("SELECT Cast('2012-12-12 12:12:12' as datetime)"), Is.EqualTo(dateTime));
Assert.That(conn.Execute<DateTime> ("SELECT @p", DataParameter.DateTime("p", dateTime)), Is.EqualTo(dateTime));
Assert.That(conn.Execute<DateTime?>("SELECT @p", new DataParameter("p", dateTime)), Is.EqualTo(dateTime));
Assert.That(conn.Execute<DateTime?>("SELECT @p", new DataParameter("p", dateTime, DataType.DateTime)), Is.EqualTo(dateTime));
}
}
示例14: TestChar
public void TestChar(string context)
{
using (var conn = new DataConnection(context))
{
Assert.That(conn.Execute<char> ("SELECT Cast('1' as char)"), Is.EqualTo('1'));
Assert.That(conn.Execute<char?>("SELECT Cast('1' as char)"), Is.EqualTo('1'));
Assert.That(conn.Execute<char> ("SELECT Cast('1' as char(1))"), Is.EqualTo('1'));
Assert.That(conn.Execute<char?>("SELECT Cast('1' as char(1))"), Is.EqualTo('1'));
Assert.That(conn.Execute<char> ("SELECT @p", DataParameter.Char("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char?>("SELECT @p", DataParameter.Char("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char> ("SELECT Cast(@p as char)", DataParameter.Char("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char?>("SELECT Cast(@p as char)", DataParameter.Char("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char> ("SELECT Cast(@p as char(1))", DataParameter.Char("@p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char?>("SELECT Cast(@p as char(1))", DataParameter.Char("@p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char> ("SELECT @p", DataParameter.VarChar ("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char?>("SELECT @p", DataParameter.VarChar ("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char> ("SELECT @p", DataParameter.NChar ("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char?>("SELECT @p", DataParameter.NChar ("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char> ("SELECT @p", DataParameter.NVarChar("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char?>("SELECT @p", DataParameter.NVarChar("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char> ("SELECT @p", DataParameter.Create ("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char?>("SELECT @p", DataParameter.Create ("p", '1')), Is.EqualTo('1'));
Assert.That(conn.Execute<char> ("SELECT @p", new DataParameter { Name = "p", Value = '1' }), Is.EqualTo('1'));
Assert.That(conn.Execute<char?>("SELECT @p", new DataParameter { Name = "p", Value = '1' }), Is.EqualTo('1'));
}
}
示例15: TestBinaryParameterSelect
public void TestBinaryParameterSelect(string context)
{
var arr1 = new byte[] { 46, 127, 0, 5 };
using (var conn = new DataConnection(context))
{
Assert.That(conn.Execute<byte[]>("SELECT :p from dummy", DataParameter.Binary("p", arr1)), Is.EqualTo(arr1));
Assert.That(conn.Execute<byte[]>("SELECT :p from dummy", DataParameter.VarBinary("p", arr1)), Is.EqualTo(arr1));
Assert.That(conn.Execute<byte[]>("SELECT :p from dummy", DataParameter.Create("p", arr1)), Is.EqualTo(arr1));
Assert.That(conn.Execute<byte[]>("SELECT :p from dummy", DataParameter.VarBinary("p", null)), Is.EqualTo(null));
Assert.That(conn.Execute<byte[]>("SELECT :p from dummy", DataParameter.VarBinary("p", new byte[0])), Is.EqualTo(new byte[0]));
Assert.That(conn.Execute<byte[]>("SELECT :p from dummy", DataParameter.Image("p", new byte[0])), Is.EqualTo(new byte[0]));
Assert.That(conn.Execute<byte[]>("SELECT :p from dummy", new DataParameter { Name = "p", Value = arr1 }), Is.EqualTo(arr1));
Assert.That(conn.Execute<byte[]>("SELECT :p from dummy", DataParameter.Create("p", new Binary(arr1))), Is.EqualTo(arr1));
Assert.That(conn.Execute<byte[]>("SELECT :p from dummy", new DataParameter("p", new Binary(arr1))), Is.EqualTo(arr1));
}
}