如何调用在数据库查询单值的存储过程
怎么调用在数据库查询单值的存储过程
写了个公共方法
public static object SelectDanZhi(string proname,CommandType type,params SqlParameter [] pp) {
using (SqlConnection con=new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand();
PropareCommand(con, cmd, type, proname, pp);
return cmd.ExecuteScalar();
}
}
数据访问
public static string SelectDan(int id) {
string pro = "sp_tang3";
SqlParameter[] pp = {
new SqlParameter("@id",id),
new SqlParameter("@name",SqlDbType.NVarChar),
};
return Convert.ToString(Dbhelp.SelectDanZhi(pro,CommandType.StoredProcedure,pp));
}
在数据库访问的时候该怎么写才正确呢?
------解决思路----------------------
你现在碰到什么问题?
------解决思路----------------------
new SqlParameter("@name", ParameterDirection.Output)
------解决思路----------------------
试试
SqlParameter[] pp = {
new SqlParameter("@id",id),
new SqlParameter("@name",SqlDbType.NVarChar),
};
pp[1].Direction = ParameterDirection.Output;
------解决思路----------------------
new SqlParameter里面的参数名不要加@
------解决思路----------------------
你给new SqlParameter("@name",SqlDbType.NVarChar)分配个大小
你存储过程分配多少就是多少,比如new SqlParameter("@name",SqlDbType.NVarChar, 100)之类的
写了个公共方法
public static object SelectDanZhi(string proname,CommandType type,params SqlParameter [] pp) {
using (SqlConnection con=new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand();
PropareCommand(con, cmd, type, proname, pp);
return cmd.ExecuteScalar();
}
}
数据访问
public static string SelectDan(int id) {
string pro = "sp_tang3";
SqlParameter[] pp = {
new SqlParameter("@id",id),
new SqlParameter("@name",SqlDbType.NVarChar),
};
return Convert.ToString(Dbhelp.SelectDanZhi(pro,CommandType.StoredProcedure,pp));
}
在数据库访问的时候该怎么写才正确呢?
------解决思路----------------------
你现在碰到什么问题?
------解决思路----------------------
new SqlParameter("@name", ParameterDirection.Output)
------解决思路----------------------
试试
SqlParameter[] pp = {
new SqlParameter("@id",id),
new SqlParameter("@name",SqlDbType.NVarChar),
};
pp[1].Direction = ParameterDirection.Output;
------解决思路----------------------
new SqlParameter里面的参数名不要加@
------解决思路----------------------
你给new SqlParameter("@name",SqlDbType.NVarChar)分配个大小
你存储过程分配多少就是多少,比如new SqlParameter("@name",SqlDbType.NVarChar, 100)之类的