标签:自定义类 with server gis stat 字段 string var MIP
static void Main(string[] args)
{
var connection = new SqlConnection("Data Source=.;Initial Catalog=Datamip;Integrated Security=True;MultipleActiveResultSets=True");
var info = connection.Query<Users>("sp_GetUsers", new { id = 5 },
commandType: CommandType.StoredProcedure);
}
需要使用 Sql Server 的自定义类型 : dbo.IDList
CREATE TYPE dbo.IDList
AS TABLE
(
ID INT
);
GO
public static List<WorkLog> QueryWithTVP()
{
int[] idList = new int[] { 1, 2 };
var results = new List<WorkLog>();
try
{
var typeIdsParameter = new List<SqlDataRecord>();
// TypeID 数组参数对应的字段
var myMetaData = new SqlMetaData[] { new SqlMetaData("TypeID", SqlDbType.Int) };
foreach (var num in idList)
{
// Create a new record, i.e. row.
var record = new SqlDataRecord(myMetaData);
// Set the 1st colunm, i.e., position 0 with the correcponding value:
record.SetInt32(0, num);
// Add the new row to the table rows array:
typeIdsParameter.Add(record);
}
using (IDbConnection conn = new SqlConnection(DBConfig.ConnectionString))
{
conn.Open();
//调用存储过程,IDList: 自定义类型
results = conn.Query<WorkLog>("dbo.GetWorkLog_ByTypeIds",
new TableValueParameter("@TypeIds", "IDList", typeIdsParameter)
, commandType: CommandType.StoredProcedure).ToList();
}
}
catch (Exception)
{
throw;
}
return results;
}
Dapper use Table Value Parameter in C# (Sql Server 数组参数)
标签:自定义类 with server gis stat 字段 string var MIP
原文地址:https://www.cnblogs.com/leestone/p/11483216.html