GridView的数据绑定实例
在显视页:
public void gvNewOBind()
{
int P_Int_returnValue = mcObj.IsExistsNI("Proc_GetNOI");
if (P_Int_returnValue == -100)
{
}
else
{
SqlCommand myCmd = mcObj.GetNewICmd("Proc_GetNOI");
mcObj.gvBind(gvOrderList,myCmd,"OrderInfo");
}
}
MangerClass类给出GridView的共用绑定方法:
public void gvBind(GridView gvName, SqlCommand myCmd, string P_Str_srcTable)
{
SqlDataAdapter da = new SqlDataAdapter(myCmd);
DataSet ds = new DataSet();
da.Fill(ds, P_Str_srcTable);
gvName.DataSource = ds.Tables[P_Str_srcTable].DefaultView;
gvName.DataBind();
}
MangerClass类给出SqlCommand:
public SqlCommand GetNewICmd(string P_Str_ProcName)
{
SqlConnection myConn = dbObj.GetConnection();
SqlCommand myCmd = new SqlCommand(P_Str_ProcName, myConn);
myCmd.CommandType = CommandType.StoredProcedure;
//添加参数
SqlParameter returnValue = myCmd.Parameters.Add("returnValue", SqlDbType.Int, 4);
returnValue.Direction = ParameterDirection.ReturnValue;
//执行过程
return myCmd ;
}
MangerClass类中判断是否是当天的订单:
public int IsExistsNI(string P_Str_ProcName)
{
SqlConnection myConn = dbObj.GetConnection();
SqlCommand myCmd = new SqlCommand(P_Str_ProcName, myConn);
myCmd.CommandType = CommandType.StoredProcedure;
//添加参数
SqlParameter returnValue = myCmd.Parameters.Add("returnValue", SqlDbType.Int, 4);
returnValue.Direction = ParameterDirection.ReturnValue;
//执行过程
myConn.Open();
try
{
myCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw (ex);
}
finally
{
myCmd.Dispose();
myConn.Close();
}
int P_Int_returnValue = Convert.ToInt32(returnValue.Value.ToString());
return P_Int_returnValue;
}
本例所使用的存储过程:
存储过程:
ALTER proc Proc_GetNOI
as
if exists(select * from tb_OrderInfo where DATEDIFF(day, OrderDate, getdate()) < 1
)
begin
select * from tb_OrderInfo where DATEDIFF(day, OrderDate, getdate()) < 1
end
else
return -100