C#执行带参存储过程
SqlParameter[] sp = new SqlParameter[] {new SqlParameter("@id", SqlDbType.Int),new SqlParameter("@age", SqlDbType.Int) };//实例化一个SqlParameter对像
sp[0].Value = 1;//为SqlParameter对像赋值
sp[1].Direction = ParameterDirection.Output;//将对像中指定参数设置为输出变量
public SqlParameter[] pulicMethod(string procName, SqlParameter[] sp)//返回SqlParameter对像
{
string con = DbHelperSQL.connectionString;
SqlConnection sql = new SqlConnection(con);
SqlCommand com = new SqlCommand(procName, sql);
com.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter spr in sp)
{
com.Parameters.Add(spr);
}
sql.Open();
com.ExecuteScalar();
sql.Close();
return sp;
//string getme = sp[1].Value.ToString() + sp[2].Value.ToString();
//return getme;
}
public DataTable getTable(string procName, SqlParameter[] sp)//返回表
{
string con = DbHelperSQL.connectionString;
SqlConnection sql = new SqlConnection(con);
SqlCommand com = new SqlCommand(procName, sql);
com.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter spr in sp)
{
com.Parameters.Add(spr);
}
sql.Open();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
sda.SelectCommand = com;
sda.Fill(ds);
sql.Close();
return ds.Tables[0];
}
sp[0].Value = 1;//为SqlParameter对像赋值
sp[1].Direction = ParameterDirection.Output;//将对像中指定参数设置为输出变量
public SqlParameter[] pulicMethod(string procName, SqlParameter[] sp)//返回SqlParameter对像
{
string con = DbHelperSQL.connectionString;
SqlConnection sql = new SqlConnection(con);
SqlCommand com = new SqlCommand(procName, sql);
com.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter spr in sp)
{
com.Parameters.Add(spr);
}
sql.Open();
com.ExecuteScalar();
sql.Close();
return sp;
//string getme = sp[1].Value.ToString() + sp[2].Value.ToString();
//return getme;
}
public DataTable getTable(string procName, SqlParameter[] sp)//返回表
{
string con = DbHelperSQL.connectionString;
SqlConnection sql = new SqlConnection(con);
SqlCommand com = new SqlCommand(procName, sql);
com.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter spr in sp)
{
com.Parameters.Add(spr);
}
sql.Open();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
sda.SelectCommand = com;
sda.Fill(ds);
sql.Close();
return ds.Tables[0];
}