【ASP.NET開發】ADO.NET呼叫帶輸出引數的儲存過程

zhiqiang21發表於2012-09-10
public static void QueryList()
        {
            SqlConnection con = new SqlConnection("資料庫連線字串");
            SqlCommand cmd = new SqlCommand("儲存過程名稱", con);
            //指定命令的型別
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter[] para ={
                                     new SqlParameter("引數1",SqlDbType.Int),
                                     new SqlParameter("引數2",SqlDbType.Int),
                                     new SqlParameter("引數3",SqlDbType.Int),
                                     new SqlParameter("引數4",SqlDbType.Int)

            };

            para[0].Value = 3;
            para[1].Value = 4;
            para[2].Direction = ParameterDirection.Output;  //設定引數的輸出方向
            para[3].Direction = ParameterDirection.Output;

            cmd.Parameters.AddRange(para);// 將引數加入命令物件
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                Console.WriteLine(dr[0].ToString());
            }

            int pageCount = Convert.ToInt32(cmd.Parameters[2].Value);
            int rowCount = Convert.ToInt32(cmd.Parameters[3].Value);

            Console.WriteLine("PageCount=" + pageCount + ",rowCount=" + rowCount);
        }

相關文章