//using (SqlConnection mySqlConnection = new SqlConnection(sqlOpen))
//{
// string sqlStr = "usp_pageEach";//存储过程名称
// using (SqlCommand mySqlCommand = new SqlCommand(sqlStr, mySqlConnection))
// {
// mySqlCommand.CommandType = CommandType.StoredProcedure;//设置执行为存储过程,默认是sql语句
// //设置参数
// SqlParameter[] mySqlParameter = new SqlParameter[]{
// new SqlParameter("@pagesize",SqlDbType.Int){Value=pagesize},
// new SqlParameter("@pageIndex",SqlDbType.Int){Value=pageIndex},
// new SqlParameter("@recordcount",SqlDbType.Int){Direction=ParameterDirection.Output},//设置该参数为输出参数
// new SqlParameter("@pagecount",SqlDbType.Int){Direction=ParameterDirection.Output}
// };
// //传入参数
// mySqlCommand.Parameters.AddRange(mySqlParameter);
// //打开连接
// mySqlConnection.Open();
// }
//}
例子
string sqlStr = "usp_pageEach";//存储过程名称
string sqlOpen = "Data Source=AJV91FQQCCEBZD1;Initial Catalog=战国七雄;Integrated Security=True";
using (SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(sqlStr, sqlOpen))
{
mySqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;//作用就是加上exec,如果存储过程名称里面加了exec则这里可以不用设置
SqlParameter[] mySqlParameter = new SqlParameter[]{
new SqlParameter("@pagesize",SqlDbType.Int){Value=3},
new SqlParameter("@pageIndex",SqlDbType.Int){Value=2},
new SqlParameter("@recordcount",SqlDbType.Int){Direction=ParameterDirection.Output},//设置该参数为输出参数
new SqlParameter("@pagecount",SqlDbType.Int){Direction=ParameterDirection.Output}
};
mySqlDataAdapter.SelectCommand.Parameters.AddRange(mySqlParameter);
DataSet myDataSet = new DataSet();
mySqlDataAdapter.Fill(myDataSet,"table1");
recordcount =Convert.ToInt32( mySqlParameter[2].Value);//获取输出参数,这该输出参数必须是执行后才能获取的
pagecount = Convert.ToInt32(mySqlParameter[3].Value);
return myDataSet.Tables["table1"];
}
扩展:
seletc赋值和set赋值的不同,set赋值时候当值是多个时候会报错。select赋值时候当值是多个时候会取最后一个