C#中SQL Server學習
阿新 • • 發佈:2022-03-18
1、資料庫檢視學習
檢視其實就是一個表查詢,一般不存實際的表,每次使用的時候執行一次查詢;
檢視也可以增刪改查,修改對應資料,對應表中資料同樣會被修改;
檢視建立語句:
2、資料庫儲存過程學習
使用儲存過程的優點:
(1)降低網路傳輸資料:執行語句預先定義為了儲存過程,程式碼在呼叫時僅需傳遞儲存過程名和引數,無需傳遞整條語句,降低了網路負載; (2)執行效率高:SQL Server只需要在儲存過程第一次執行的時候編譯成可執行的二進位制程式碼,再次使用不需要重新編譯; (3)封裝性:將實現某種功能的多條SQL語句封裝到一個物件中,可多次重複呼叫,可移植性強; (4)安全:如果所有使用者都使用儲存過程來訪問資料,就可以禁止使用者對錶的直接訪問,並控制所有對資料的訪問; 3、建立儲存過程 、執行儲存過程
[1]建立不帶輸入、輸出的儲存過程
use StudentManagerDB go if(exists (select * from sys.objects where name='VTest')) drop view VTest go --建立檢視 create view VTest as select TOP (100) PERCENT A.StudentId,A.StudentName,A.ClassId,B.ClassName,C.CSharp,C.SQLServerTOP (100) PERCENT:因為SQL規則規定,如果子查詢裡,有order,就必須有TOP,所以就用SELECT TOP (100) percent來限定,top 100 percent是為了保證篩選出所有符合條件的資料條目(百分百)。 sys.objests儲存的是本資料庫中的資訊,不僅儲存表名,還有儲存過程名稱、檢視名、觸發器等;from Students as A inner join StudentClass as B on A.ClassId=B.ClassId left join ScoreList as C on A.StudentId=C.StudentId order by A.StudentId go
(1)降低網路傳輸資料:執行語句預先定義為了儲存過程,程式碼在呼叫時僅需傳遞儲存過程名和引數,無需傳遞整條語句,降低了網路負載; (2)執行效率高:SQL Server只需要在儲存過程第一次執行的時候編譯成可執行的二進位制程式碼,再次使用不需要重新編譯; (3)封裝性:將實現某種功能的多條SQL語句封裝到一個物件中,可多次重複呼叫,可移植性強; (4)安全:如果所有使用者都使用儲存過程來訪問資料,就可以禁止使用者對錶的直接訪問,並控制所有對資料的訪問; 3、建立儲存過程 、執行儲存過程
use StudentManagerDB go if(exists(select * from sys.objects where name='pro_testNo')) drop procedure pro_testNo go create procedure pro_testNo as begin select * from Students end go --執行儲存過程 exec dbo.pro_testNo
[2]建立帶輸入、不帶輸出引數的儲存過程
use StudentManagerDB go if(exists(select * from sys.objects where name='pro_Students')) drop proc pro_Students go create procedure pro_Students @StudentId int as begin select * from Students where StudentId=@StudentId end go --執行儲存過程 exec pro_Students 10002 --或者宣告變數後再執行 declare @StudentId int = 10002 exec pro_Students @StudentIddeclare/dɪˈkler/ v.宣告 [3]建立帶輸出引數、不帶輸入引數的儲存過程
use StudentManagerDB go if(exists(select * from sys.objects where name='pro_testOut')) drop procedure pro_testOut go create procedure dbo.pro_testOut @StudentName varchar(50) output as begin select @StudentName=StudentName from dbo.Students where StudentID=10002 end go --執行儲存過程 declare @StudentName varchar(50)--宣告變數 exec pro_testOut @StudentName output print @StudentName --另一種輸出方法 select @StudentName
[4]建立帶輸入、輸出引數的儲存過程
use StudentManagerDB go if(exists (select * from sys.objects where name='pro_testAll')) drop procedure pro_testAll go create procedure pro_testAll @StudentId int, @StudentName varchar(50) output as begin select @StudentName=StudentName from Students where StudentId=@StudentId end go --呼叫儲存過程 declare @StudentName varchar(50) declare @StudentId int = 10002 exec pro_testAll @StudentId,@StudentName output print @StudentName select @StudentNamestoredprocedure 儲存過程 store/stɔːr/ n.儲存 procedure /prəˈsiːdʒər/ n.過程 parameter /pəˈræmɪtər/ n.引數 direction/dəˈrekʃn/ n.方向 4、ADO.NET呼叫儲存過程 [1]呼叫沒有輸入和輸出引數的儲存過程,而且不返回查詢結果
using System.Data.SqlClient; using System.Data; //呼叫沒有輸入和輸出引數的儲存過程,而且不返回查詢結果 class Store { private string conn = "server=MI-NTCOMPUTER;database=StudentManageDB;Trusted_Connection=sspi"; public void GetProcedure() { SqlConnection con = new SqlConnection(conn); SqlCommand cmd = new SqlCommand("dbo.pro_testNo",con);//("儲存過程名",con) cmd.CommandType =CommandType.StoredProcedure; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } }
[2]呼叫帶有輸入引數的儲存過程
using System.Data.SqlClient; using System.Data; //呼叫有輸入引數的的儲存過程,不返回查詢結果 class Store { private string conn = "server=MI-NTCOMPUTER;database=StudentManageDB;Trusted_Connection=sspi"; public void GetProcedure() { SqlConnection con = new SqlConnection(conn); SqlCommand cmd = new SqlCommand("dbo.pro_testInt",con);//("儲存過程名",con) cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@StudentId",SqlDbType.Int));//("儲存過程引數變數名",資料型別) //把具體的值傳給輸入引數 cmd.Parameters["@StudentID"].Value = 100001; con.Open(); //執行儲存過程 cmd.ExecuteNonQuery(); con.Close(); } }
[3]呼叫帶有輸出引數、沒有輸入引數的儲存過程 5、連線字串--SQL身份驗證的連線字串 和 Winodws身份驗證的連線字串 (1)SQL身份驗證的連線字串 private string con ="server=伺服器名稱;database=資料庫名稱;uid=登入名;pwd=登入密碼"; (2)Windows身份驗證的連線字串 private string con="server=伺服器名稱;database=資料庫名稱;Trusted_Connection=SSPI"; trust /trʌst/ n.信任 (3)Trusted_Connection 當為false時,將在連線中指定使用者的ID和密碼; 當為true時,將使用當前的Windows賬戶憑據進行身份驗證; 可識別的值為true、false、yes、no以及與true等效的sspi(推薦使用);