本文整理汇总了C#中System.Data.SqlClient.SqlConnection.ExecuteEntity方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.ExecuteEntity方法的具体用法?C# SqlConnection.ExecuteEntity怎么用?C# SqlConnection.ExecuteEntity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlConnection
的用法示例。
在下文中一共展示了SqlConnection.ExecuteEntity方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteEntity
public void ExecuteEntity()
{
string sql = "SELECT 1 AS IntColumn, 'FizzBuzz' AS StringColumn WHERE @Fizz = 1";
var dict = new Dictionary<string, object> {{"@Fizz", 1}};
// Examples
using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
{
conn.Open();
var entity = conn.ExecuteEntity<TestObject>(sql, dict.ToDbParameters(conn));
// Unit Test
Assert.AreEqual(1, entity.IntColumn);
Assert.AreEqual("FizzBuzz", entity.StringColumn);
Assert.AreEqual(-1, entity.IntColumnNotExists);
}
}
示例2: ExecuteEntity
public void ExecuteEntity()
{
var date = new DateTime(1981, 04, 13);
Guid guid = Guid.NewGuid();
var entity = new EntityWithAllColumn();
entity.BitIntColumn = 1;
entity.BinaryColumn = new byte[] {1};
entity.BitColumn = true;
entity.CharColumn = "z";
entity.DateColumn = date;
entity.DateTimeColumn = date;
entity.DateTime2Column = date;
entity.DateTimeOffsetColumn = date;
entity.DecimalColumn = 1.25m;
entity.FloatColumn = 1.25f;
entity.ImageColumn = new byte[] {1};
entity.IntColumn = 1;
entity.MoneyColumn = 1.25m;
entity.NCharColumn = "z";
entity.NTextColumn = "z";
entity.NumericColumn = 1;
entity.NVarcharColumn = "z";
entity.NVarcharMaxColumn = "z";
entity.RealColumn = 1.25f;
entity.SmallDateTimeColumn = date;
entity.SmallIntColumn = null;
entity.SmallMoneyColumn = 1.25m;
entity.TextColumn = "z";
entity.TimeColumn = date.TimeOfDay;
entity.TimestampColumn = new byte[] {1};
entity.TinyIntColumn = 1;
entity.UniqueIdentifierColumn = guid;
entity.VarBinaryColumn = new byte[] {1};
entity.VarBinaryMaxColumn = new byte[] {1};
entity.VarcharColumn = "z";
entity.VarcharMaxColumn = "z";
entity.XmlColumn = "z";
using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
{
conn.Open();
conn.ExecuteNonQuery("TRUNCATE TABLE EntityWithAllColumns");
var dt = new DataTable();
using (var copy = new SqlBulkCopy(conn))
{
foreach (PropertyInfo property in entity.GetType().GetProperties())
{
if (property.Name == "SmallIntColumn")
{
dt.Columns.Add(property.Name, typeof (short));
}
else
{
dt.Columns.Add(property.Name, property.PropertyType);
}
copy.ColumnMappings.Add(property.Name, property.Name);
}
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
foreach (PropertyInfo property in entity.GetType().GetProperties())
{
object value = property.GetValue(entity, null);
dr[property.Name] = value == null ? DBNull.Value : value;
}
copy.DestinationTableName = "EntityWithAllColumns";
copy.WriteToServer(dt);
}
// Unit Test
var result = conn.ExecuteEntity<EntityWithAllColumn>("SELECT TOP 1 * FROM EntityWithAllColumns");
Assert.AreEqual(1, result.BitIntColumn);
Assert.AreEqual(1, result.BinaryColumn[0]);
Assert.AreEqual(true, result.BitColumn);
Assert.AreEqual("z", result.CharColumn);
Assert.AreEqual(date, result.DateColumn);
Assert.AreEqual(date, result.DateTimeColumn);
Assert.AreEqual(date, result.DateTime2Column);
Assert.AreEqual(date, result.DateTimeOffsetColumn);
Assert.AreEqual(1.25m, result.DecimalColumn);
Assert.AreEqual(1.25f, result.FloatColumn);
Assert.AreEqual(1, result.ImageColumn[0]);
Assert.AreEqual(1, result.IntColumn);
Assert.AreEqual(1.25m, result.MoneyColumn);
Assert.AreEqual("z", result.NCharColumn);
Assert.AreEqual("z", result.NTextColumn);
Assert.AreEqual(1, result.NumericColumn);
Assert.AreEqual("z", result.NVarcharColumn);
Assert.AreEqual("z", result.NVarcharMaxColumn);
Assert.AreEqual(1.25f, result.RealColumn);
Assert.AreEqual(date, result.SmallDateTimeColumn);
Assert.AreEqual(null, result.SmallIntColumn);
Assert.AreEqual(1.25m, result.SmallMoneyColumn);
Assert.AreEqual("z", result.TextColumn);
Assert.AreEqual(date.TimeOfDay, result.TimeColumn);
//.........这里部分代码省略.........