个人比较喜欢使用第二种传递参数的方法
1. 调用的方法
public DataTable ExceStoredProcedure (string strCom, SqlParameter[] commandParameters)
{
using (SqlConnection _sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
_sqlCon.Open(); /
SqlCommand cmd = _sqlCon.CreateCommand();
//调用的类型是存储过程,即strCom为存储过程的名称
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = strCom;
cmd.Parameters.AddRange(commandParameters);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
_sqlCon.Close(); //关闭连接
return dt;
}
}
//参数一
SqlParameter[] param = new SqlParameter[] {
new SqlParameter("@TableName", "T_Web_List_Pch"),
new SqlParameter("@pageindex", 1),
new SqlParameter("@PageSize", 10), //页码数量
new SqlParameter("@SearchString",searchString), //搜索框的输入值
new SqlParameter("@OrderField","Id ") ,//排序字段
new SqlParameter("@SearchField","Id ") //搜索框的输入值
};
//参数二
1. 可在sqlhelp中定义方法
public static SqlParameter MakeInParam(string paramName, object value)
{
return new SqlParameter(paramName, value);
}
写入参数
SqlParameter[] param =
{
MakeInParam("@TableName", "T_Web_List_Pch"), //表名
MakeInParam("@pageindex", 1)
MakeInParam("@PageSize", 10), //页码数量
MakeInParam("@SearchString",searchString), //搜索框的输入值
MakeInParam("@OrderField","Id ") ,//排序字段
MakeInParam("@SearchField","Id ") //搜索框的输入值
};
DataTable dt= ExceStoredProcedure("NewPage", param)