使用存储过程

Posted wuhz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用存储过程相关的知识,希望对你有一定的参考价值。

 

//Start C#--------------------------------------------------------------------------------------------------------------------------------

public System.Data.DataTable GetDormHealthOfClass(OurHelper.PageHelper<Dorm_DormitoryHealth> ph)
{
string classID = "";
foreach (var item in ph.QueryKeyValues)
{
if (item.Key == "ClassID")
{
classID = item.Value;
}
}


IDataParameter[] parameter2 = { new SqlParameter("ClassID", SqlDbType.VarChar, 50), new SqlParameter("PageIndex", SqlDbType.Int), new SqlParameter("PageSize", SqlDbType.Int) };
parameter2[0].Value = classID;
parameter2[1].Value = ph.PageIndex;
parameter2[2].Value = ph.PageSize;
int totalCount=0;
DataTable dt= base.ProcForDataTable("Proc_GetDormHealthOfClass", parameter2,totalCount);
ph.TotalCount= totalCount;
return dt;
}

//调用

list = OurHelper.DataTableToListHelper<Dorm_DormitoryHealth>.ConvertToModel(service.GetDormHealthOfClass(page));

 

 

public class ModelConvertHelper<T> where T : new()
{
public static IList<T> ConvertToModel(DataTable dt)
{
// 定义集合
IList<T> ts = new List<T>();

// 获得此模型的类型
Type type = typeof(T);
string tempName = "";

foreach (DataRow dr in dt.Rows)
{
T t = new T();
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name; // 检查DataTable是否包含此列

if (dt.Columns.Contains(tempName))
{
// 判断此属性是否有Setter
if (!pi.CanWrite) continue;

object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
}

 

//End C#--------------------------------------------------------------------------------------------------------------------------------

 

//Start Sql------------------------------------------------------------------------------------------------------------------------------

ALTER PROC [dbo].[Proc_GetDormHealthOfClass] ( @classID NVARCHAR(50),@pageIndex INT,@pageSize INT )
AS
BEGIN
SELECT * FROM ( SELECT DISTINCT
ROW_NUMBER() OVER(Order by DormitoryID ) AS RowId,
t1.DormitoryID ,
t3.Name AS DormitoryName ,
CONVERT(DATETIME, CONVERT(VARCHAR(100), t1.CheckUpDate, 23)) AS CheckUpDate ,
t1.CheckUpResult
FROM dbo.Dorm_DormitoryHealth t1
INNER JOIN ( SELECT tt3.DormID
FROM dbo.Mappding_Bed_Student tt1
INNER JOIN dbo.Basic_StudentInfo tt2 ON tt1.StudentID = tt2.ID
AND tt2.Status = 1
INNER JOIN dbo.Dorm_Bed tt3 ON tt1.BedID = tt3.ID
AND tt3.Status = 1
WHERE tt1.Status = 1
AND tt2.ClassID [email protected]

) t2 ON t1.DormitoryID = t2.DormID
AND t1.Status = 1
LEFT JOIN dbo.Dorm_Dormitory t3 ON t3.ID = t1.DormitoryID
) AS A
WHERE A.RowId BETWEEN ((@pageIndex-1)*@pageSize+1) AND (@pageIndex*@pageSize)
END;

 

//End Sql------------------------------------------------------------------------------------------------------------------------------

以上是关于使用存储过程的主要内容,如果未能解决你的问题,请参考以下文章

MS sql如何使用存储过程?

MySQL 存储过程,获取使用游标查询的结果集

存储过程中如何使用Case嵌套

oracle中的存储过程,有啥作用,以及怎么在代码中使用?

如何在存储过程中直接使用另一个存储过程返回的数据集

如何用存储过程生成唯一UUID