ADO.NET之預存程序的調用

來源:互聯網
上載者:User

標籤:style   blog   color   os   資料   io   

要執行預存程序就必須串連到資料庫

代碼如下:

            string source = "server=(local) \\SQLEXPRESS;integrated security=SSPI;database=Student";            SqlConnection con = new SqlConnection(source);//建立一個串連            con.Open();//開啟資料庫            if (con.State == ConnectionState.Open)//ConnectionState為枚舉值,描述資料來源的串連狀態為開啟                Console.WriteLine("資料庫連接成功!");

  1.調用沒有傳回值的預存程序之記錄的更新

            SqlCommand com = new SqlCommand("studentupdate", con);//建立一條命令,調用命令將要執行在sql 2008中定義好的預存程序            //接著開始調用預存程序,因為該預存程序中有兩個輸入參數,所以需要構建兩個參數,設定他們的值,然後添加到Parameters(參數集合)中            com.CommandType = CommandType.StoredProcedure;//com.CommandType:擷取或設定一個值,該值指示如何解釋 CommandText屬性,CommandText屬性:對資料來源執行的 Transact-SQL 陳述式、表名或預存程序   CommandType枚舉指定如何解釋命令字串            com.Parameters.AddWithValue("@id", 1);//表示向 SqlParameterCollection 的末尾添加值,SqlParameterCollection表示參數的集合,該語句還可以用com.Parameters.Add(new SqlParameter("@id","id"))代替            com.Parameters.AddWithValue("@name", "bob");            int x = com.ExecuteNonQuery();//調用該預存程序            Console.WriteLine("受影響的行數:{0}", x);
--寫好的預存程序必須執行或儲存,SqlCommand類才能建立成功use Studentgocreate procedure studentupdate(@id int,@name char(10))asset nocount off--當 SET NOCOUNT 為 ON 時,不返回計數(表示受 Transact-SQL 陳述式影響的行數)。當 SET NOCOUNT 為 OFF 時,返回計數。update customersset name=@namewhere id=@id; 

 

2.調用沒有傳回值的預存程序之記錄的刪除

            SqlCommand com2 = new SqlCommand("studentdelete", con);            com2.CommandType = CommandType.StoredProcedure;            SqlParameter sp = new SqlParameter("@id", SqlDbType.Int, 0, "id");            com2.UpdatedRowSource = UpdateRowSource.None;//UpdateRowSource枚舉:指定如何將查詢命令結果應用到正在更新的行,none表示忽略任何返回的參數或行            com2.Parameters.Add(sp);//將指定的 SqlParameter 對象添加到 SqlParameterCollection 中,參數還沒有賦值,而com.Parameters.AddWithValue()已經賦值            sp.Value = 1;//或者用cmd.Parameters["@id"].value=1            com2.ExecuteNonQuery();            Console.WriteLine("刪除成功!");            con.Close();
use Studentdrop procedure studentdelete;gocreate procedure studentdelete(@id int)asset nocount offdelete from Customerswhere id=@id

 

3.調用沒有傳回值得預存程序之查詢

            SqlCommand com5 = new SqlCommand("selectstudent", con);            com5.CommandType = CommandType.StoredProcedure;            com5.Parameters.Add("@id", 5);            SqlDataReader data=com5.ExecuteReader();            while (data.Read())            {                Console.WriteLine("name:{0} id:{1}", data[0], data[1]);            }
use Studentdrop procedure selectstudentgocreate procedure selectstudent(@id int)asselect *from Customerswhere id<=@id

 

4.調用有傳回值的預存程序之記錄的插入

            SqlCommand com3 = new SqlCommand("studentinsert", con);            com3.CommandType = CommandType.StoredProcedure;            com3.Parameters.Add(new SqlParameter("@name", "李四"));            com3.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,0,ParameterDirection.Output,false,0,0,"id",DataRowVersion.Default,0));            com3.UpdatedRowSource = UpdateRowSource.OutputParameters;//UpdateRowSource枚舉:指定如何將查詢命令結果應用到正在更新的行,OutputParameters表示將輸出參數映射到dataset中已經更改的行            com3.ExecuteNonQuery();            int x = (int)com3.Parameters["@id"].Value;//獲得輸出參數的方式            Console.WriteLine("輸出參數(下一個id號)為:{0}", x);
use Studentdrop procedure studentinsertgocreate procedure studentinsert(@name char(20),@id int output)asset nocount offselect @id=MAX(id)+1from Customersinsert into Customersvalues(@name,@id);

 

5.調用有傳回值得預存程序:返回一組記錄

            SqlCommand com4 = new SqlCommand("returnstudent", con);            com4.CommandType = CommandType.StoredProcedure;            com4.Parameters.Add(new SqlParameter("@id", 3));            com4.Parameters.Add(new SqlParameter("@rid", SqlDbType.Int, 0, ParameterDirection.Output, false, 0, 0, "id", DataRowVersion.Default, 0));            com4.Parameters.Add("@rname", SqlDbType.Char, 20).Direction= ParameterDirection.Output;//這種方式可以省略不必要的參數            com4.ExecuteNonQuery();            int x = (int)com4.Parameters["@id"].Value;            string y=com4.Parameters["@rname"].Value.ToString().Trim();            Console.WriteLine("id:{0} name:{1}",x,y);

 

use Studentdrop procedure returnstudentgocreate procedure returnstudent(@id int,@rid int output,@rname char(20) output)asselect @rid=id,@rname=Namefrom Customerswhere id=@id;

 

   

聯繫我們

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