应用程序(C#)怎样设置可以让存储过程的变量使用默认值
代码如下:
ALTER PROCEDURE [dbo].[ViewNotes]
(
@grateID INT,
@dateTime DATETIME=0
)
AS
/* SET NOCOUNT ON */
SELECT GrateName,StudentName,Content1,Content2,
CONVERT(VARCHAR(4),YEAR(DateT))+'-'+CONVERT(VARCHAR(2),MONTH(DateT))+'-'+CONVERT(VARCHAR(2),DAY(DateT)) AS DateT
FROM NoteTable
WHERE DateT >= @dateTime AND GrateID=@grateID
ORDER BY DateT DESC
中间层的代码如下:
public static DataTable GetAllNoteByGrateID(string grateID, params string[] dateT)——这里我使用的参数数组
{
DbCommand comm = GenericDataAccess.CreateCommand(); ;
comm.CommandText = "ViewNotes";
DbParameter param = comm.CreateParameter();
DataTable dT = new DataTable();
param.ParameterName="@grateID";
param.DbType = DbType.Int32;
param.Value = grateID;
comm.Parameters.Add(param);
foreach (string st in dateT)————————————这里如果没有dateT参数变量@dateTime里会是什么?
{
param = comm.CreateParameter();
param.ParameterName = "@dateTime";
param.DbType = DbType.DateTime;
param.Value = st;
comm.Parameters.Add(param);
}
try
{
dT = GenericDataAccess.ExecuteSelectCommand(comm);
}
catch { }
return dT;
}
我想实现: GetAllNoteByGrateID(string grateID, params string[] dateT)——当没有dateT参数时只使用grateID,存储过程使用默认值0,当dateT中有参数时使用其中的参数,还请高手指教.
------解决方案--------------------
C# code
if (dateT.lenght==0)
{
param = comm.CreateParameter();
param.ParameterName = "@dateTime";
param.DbType = DbType.DateTime;
param.Value = 0;
}else
{
foreach (string st in dateT)————————————这里如果没有dateT参数变量@dateTime里会是什么?
{
param = comm.CreateParameter();
param.ParameterName = "@dateTime";
param.DbType = DbType.DateTime;
param.Value = st;
comm.Parameters.Add(param);
}
}