標籤:
//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賦值時候當值是多個時候會取最後一個
ADO.NET調用預存程序