学习ado.net(4):调用存储过程 1-2
最原始的写法,当然是CommandType = CommandType.StoredProcedure
SqlConnection conn = new SqlConnection("server=.");
SqlDataAdapter ada = new SqlDataAdapter(
"procname",
conn);
ada.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlParameter p = new SqlParameter("@输出参数名");
p.Direction = ParameterDirection.Output;
ada.SelectCommand.Parameters.Add("@输入参数名", "值");
ada.SelectCommand.Parameters.Add(p);
DataTable dt = new DataTable();
ada.Fill(dt);
object outparvalue = p.Value;
开发中,很多使用微软的sqlhelper的:
SqlParameter p = new SqlParameter("@输出参数名");
p.Direction = ParameterDirection.Output;
SqlHelper.ExecuteNonQuery(
"存储过程名",
CommandType.StoredProcedure,
new SqlParameter("@参数名", 值),
p
);
object outparvalue = p.Value;