本文整理汇总了C#中System.Data.SqlClient.SqlConnection.ExecuteEntities方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.ExecuteEntities方法的具体用法?C# SqlConnection.ExecuteEntities怎么用?C# SqlConnection.ExecuteEntities使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlConnection
的用法示例。
在下文中一共展示了SqlConnection.ExecuteEntities方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteEntities
public void ExecuteEntities()
{
string sql = "SELECT 1 AS IntColumn, 'FizzBuzz' AS StringColumn UNION SELECT 2, 'BuzzBuzz' WHERE @Fizz = 1";
var dict = new Dictionary<string, object> {{"@Fizz", 1}};
// Examples
using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
{
conn.Open();
List<TestObject> list = conn.ExecuteEntities<TestObject>(sql, dict.ToSqlParameters()).ToList();
// Unit Test
Assert.AreEqual(1, list[0].IntColumn);
Assert.AreEqual("FizzBuzz", list[0].StringColumn);
Assert.AreEqual(2, list[1].IntColumn);
Assert.AreEqual("BuzzBuzz", list[1].StringColumn);
Assert.AreEqual(-1, list[0].IntColumnNotExists);
}
}
示例2: ExecuteEntities
public void ExecuteEntities()
{
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
EntityWithAllColumn result = conn.ExecuteEntities<EntityWithAllColumn>("SELECT TOP 1 * FROM EntityWithAllColumns").ToList()[0];
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);
//.........这里部分代码省略.........