ADO.NET – 2.調用預存程序

來源:互聯網
上載者:User

目錄:

1.調用預存程序

  1. 1.個參數調用預存程序

預存程序 usp_GetBookCateId

  1. 2.多個參數調用預存程序

預存程序 usp_GetPagesBooks

  1. 3.調用帶輸出參數的預存程序 output

 

 

1.調用預存程序

1.個參數調用預存程序

預存程序 usp_GetBookCateId

代碼

        static void Main(string[] args)        {            //Console.WriteLine("增加資料:");            //string ss = Convert.ToString(Console.ReadLine());//字串使用Console.ReadLine()            //AddRecord(ss);            //QuerySingle();            //QueryList();            //QueryListByAdapter();            QueryListByProcedure();        }        //調用預存程序        public static void QueryListByProcedure()        {            SqlConnection conn = new SqlConnection(sqlCon);            SqlCommand cmd = new SqlCommand("usp_GetBookByCateId", conn);          

            cmd.CommandType = CommandType.StoredProcedure;//指定命令類型為預存程序

            SqlParameter sp = new SqlParameter();//指定預存程序中的參數

            sp.ParameterName = "@cateId";

            sp.SqlDbType = SqlDbType.Int;

            sp.Size = 4;

            sp.Value = 2;//該參數的值

            cmd.Parameters.Add(sp);

//將參數對象加入命令對象的參數集合中            SqlDataAdapter adapter = new SqlDataAdapter(cmd);            DataSet ds = new DataSet();            adapter.Fill(ds);            foreach (DataRow dr in ds.Tables[0].Rows)            {                Console.WriteLine(dr[0].ToString() + " "                     + dr[1].ToString()+ " " + dr[2].ToString()+ " " + dr[3].ToString());            }        }

結果

 

2.多個參數調用預存程序

預存程序 usp_GetPagesBooks

ALTER procedure [dbo].[usp_GetPagesBooks]@PageIndex int = 1,@PageSize int = 5,@RowCount int output,@PageCount int output  --11asbegin    select * from     (    select ROW_NUMBER() over(order by c_id) as nid,* from Category    )     as a     where a.nid > (@PageIndex - 1)* @PageSize and a.nid<= @PageIndex * @PageSize     select @RowCount = COUNT(*) from Category select @PageCount = ceiling(CONVERT(float,@RowCount)/CONVERT(float,@PageSize)) set @PageIndex =10000     set @PageSize =100end

代碼

        static void Main(string[] args)        {            //Console.WriteLine("增加資料:");            //string ss = Convert.ToString(Console.ReadLine());//字串使用Console.ReadLine()            //AddRecord(ss);            //QuerySingle();            //QueryList();            //QueryListByAdapter();            //QueryListByProcedure();            QueryListByProcedure2();        }        //調用多參數預存程序        public static void QueryListByProcedure2()        {            SqlConnection conn = new SqlConnection(sqlCon);            SqlCommand cmd = new SqlCommand("usp_GetPagesBooks", conn);            cmd.CommandType = CommandType.StoredProcedure;//指定命令類型為預存程序            SqlParameter[] sp = {                new SqlParameter("@PageIndex", SqlDbType.Int, 4),                new SqlParameter("@PageSize", SqlDbType.Int, 4),                new SqlParameter("@RowCount", SqlDbType.Int, 4),                new SqlParameter("@PageCount", SqlDbType.Int, 4)                };            sp[0].Value = 2;//變數:儲存頁碼
            sp[1].Value = 4;//變數:儲存頁容量(每頁顯示多少行)
            sp[2].Value = 0;//總行數
           sp[3].Value = 0;//總頁數 
            cmd.Parameters.AddRange(sp);            SqlDataAdapter adapter = new SqlDataAdapter(cmd);            DataSet ds = new DataSet();            adapter.Fill(ds);            foreach (DataRow dr in ds.Tables[0].Rows)            {                Console.WriteLine(dr[0].ToString() + " "                    + dr[1].ToString() + " " + dr[2].ToString() + " " + dr[3].ToString());            }        }

結果

 

3.調用帶輸出參數的預存程序 output

代碼:

 

//3.調用帶輸出參數的預存程序        public static void QueryListByProcedure3()        {            SqlConnection conn = new SqlConnection(sqlCon);            SqlCommand cmd = new SqlCommand("usp_GetPagesBooks", conn);            cmd.CommandType = CommandType.StoredProcedure;//指定命令類型為預存程序            SqlParameter[] sp = {                new SqlParameter("@PageIndex", SqlDbType.Int, 4),                new SqlParameter("@PageSize", SqlDbType.Int, 4),                new SqlParameter("@RowCount", SqlDbType.Int, 4),                new SqlParameter("@PageCount", SqlDbType.Int, 4)                };            sp[0].Value = 2;            sp[1].Value = 4;            sp[2].Direction = ParameterDirection.Output;//設定參數的輸出方向 
       sp[3].Direction = ParameterDirection.Output; 
            cmd.Parameters.AddRange(sp);            SqlDataAdapter adapter = new SqlDataAdapter(cmd);            DataSet ds = new DataSet();            adapter.Fill(ds);            foreach (DataRow dr in ds.Tables[0].Rows)            {                Console.WriteLine(dr[0].ToString() + " "                    + dr[1].ToString() + " " + dr[2].ToString() + " " + dr[3].ToString());            }            Console.WriteLine();             //輸入RowCount和PageCount的值 
        int RowCount =Convert.ToInt32(cmd.Parameters[2].Value); 
        int PageCount = Convert.ToInt32(cmd.Parameters[3].Value); 
        Console.WriteLine("RowCount=" + RowCount + ",PageCount=" + PageCount); 
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.