標籤: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;