ADO.NET調用預存程序

來源:互聯網
上載者:User

標籤:

//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調用預存程序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.