UserAccount | ||||
UserID | UserName | PassWord | RegisterTime | RegisterIP |
12 | 6 | 6 | 2012-12-31 | 6 |
18 | 5 | 5 | 2013-01-01 | 5 |
19 | 1 | 1 | 2013-01-01 | 1 |
20 | 2 | 2 | 2013-01-01 | 2 |
21 | 3 | 3 | 2013-01-01 | 3 |
22 | 4 | 4 | 2013-01-01 | 4 |
23 | 5 | 5 | 2013-01-01 | 5 |
25 | 7 | 7 | 2013-01-01 | 7 |
26 | 8 | 8 | 2013-01-01 | 8 |
NULL | NULL | NULL | NULL | NULL |
針對上面的表,我使用存儲過程對它做一些操作:
1. 只返回單一記錄集的存儲過程
-------------執行上面的存儲過程----------------
exec GetUserAccount
2.沒有輸入輸出的存儲過程
create Procedure inUserAccount
as
insert into UserAccount (UserName,[PassWord],RegisterTime,RegisterIP) values(9,9,'2013-01-02',9)
go
-------------執行上面的存儲過程----------------
exec inUserAccount
3.有返回值的存儲過程
create Procedure inUserAccountRe
as
insert into UserAccount (UserName,[PassWord],RegisterTime,RegisterIP) values(10,10,'2013-01-02',10)
return @@rowcount
go
-------------執行上面的存儲過程----------------
exec inUserAccountRe
4.有輸入參數和輸出參數的存儲過程
create Procedure GetUserAccountRe
@UserName nchar(20),
@UserID int output
as
if(@UserName>5)
select @UserID=COUNT(*) from UserAccount where UserID>25
else
set @UserID=1000
go
-------------執行上面的存儲過程----------------
exec GetUserAccountRe '7',null
5. 同時具有返回值、輸入參數、輸出參數的存儲過程
create Procedure GetUserAccountRe1
@UserName nchar(20),
@UserID int output
as
if(@UserName>5)
select @UserID=COUNT(*) from UserAccount where UserID>25
else
set @UserID=1000
return @@rowcount
go
-------------執行上面的存儲過程----------------
exec GetUserAccountRe1 '7',null
6.同時返回參數和記錄集的存儲過程
create Procedure GetUserAccountRe2
@UserName nchar(20),
@UserID int output
as
if(@UserName>5)
select @UserID=COUNT(*) from UserAccount where UserID>25
else
set @UserID=1000
select * from UserAccount
return @@rowcount
go
-------------執行上面的存儲過程----------------
exec GetUserAccountRe2 '7',null
7.返回多個記錄集的存儲過程
create Procedure GetUserAccountRe3
as
select * from UserAccount
select * from UserAccount where UserID>5
go
-------------執行上面的存儲過程----------------
exec GetUserAccountRe3
小結:上面我們創建了各式的存儲過程,下面看我們在c#中怎樣調用這些存儲過程。
c#調用存儲過程
這里調用的存儲過程為上面我寫的那些各式各樣的存儲過程。
public partial class ProcedureTest : System.Web.UI.Page
{
public static string conn = ConfigurationManager.ConnectionStrings["StuRelationDBConnectionString"].ConnectionString;
public SqlConnection con = new SqlConnection(conn);
protected void Page_Load(object sender, EventArgs e)
{
runGetUserAccountRe3();
}
//只返回單一記錄集的存儲過程GetUserAccount
public void runGetUserAccount()
{
SqlDataAdapter dp = new SqlDataAdapter(common("GetUserAccount"));
DataSet ds = new DataSet();
// 填充dataset
dp.Fill(ds);
rpt.DataSource = ds;
rpt.DataBind();
}
//沒有輸入輸出的存儲過程inUserAccount
public void runinUserAccount()
{
con.Open();
Label1.Text = common("inUserAccount").ExecuteNonQuery().ToString();
con.Close();
}
//有返回值的存儲過程inUserAccountRe
public void runinUserAccountRe()
{
// 創建參數
SqlCommand cmd = common("inUserAccountRe");
IDataParameter[] parameters = {
new SqlParameter("rval", SqlDbType.Int,4)
};
// 將參數類型設置為 返回值類型
parameters[0].Direction = ParameterDirection.ReturnValue;
// 添加參數
cmd.Parameters.Add(parameters[0]);
con.Open();
// 執行存儲過程并返回影響的行數
Label1.Text = cmd.ExecuteNonQuery().ToString();
con.Close();
// 顯示影響的行數和返回值
Label1.Text += "-" + parameters[0].Value.ToString();
}
//有輸入參數和輸出參數的存儲過程
public void runGetUserAccountRe()
{
SqlCommand cmd = common("GetUserAccountRe");
// 創建參數
IDataParameter[] parameters = {
new SqlParameter("@UserName", SqlDbType.NChar,20) ,
new SqlParameter("@UserID", SqlDbType.Int) ,
};
// 設置參數類型
parameters[0].Value = "7";
parameters[1].Direction = ParameterDirection.Output; // 設置為輸出參數
// 添加參數
cmd.Parameters.Add(parameters[0]);
cmd.Parameters.Add(parameters[1]);
con.Open();
// 執行存儲過程并返回影響的行數
Label1.Text = cmd.ExecuteNonQuery().ToString();
con.Close();
// 顯示影響的行數和輸出參數
Label1.Text += "-" + parameters[1].Value.ToString();
}
//同時具有返回值、輸入參數、輸出參數的存儲過程GetUserAccountRe1
public void runGetUserAccountRe1()
{
SqlCommand cmd = common("GetUserAccountRe1");
// 創建參數
IDataParameter[] parameters = {
new SqlParameter("@UserName", SqlDbType.NChar,20) ,
new SqlParameter("@UserID", SqlDbType.Int) ,
new SqlParameter("rval", SqlDbType.Int,4)
};
// 設置參數類型
parameters[0].Value = "7";
parameters[1].Direction = ParameterDirection.Output; // 設置為輸出參數
parameters[2].Direction = ParameterDirection.ReturnValue; //設置為返回值
// 添加參數
cmd.Parameters.Add(parameters[0]);
cmd.Parameters.Add(parameters[1]);
cmd.Parameters.Add(parameters[2]);
con.Open();
// 執行存儲過程并返回影響的行數
Label1.Text = cmd.ExecuteNonQuery().ToString();
con.Close();
// 顯示影響的行數和輸出參數
Label1.Text += "-輸出參數為:" + parameters[1].Value.ToString();
Label1.Text += "-返回值為:" + parameters[2].Value.ToString();
}
//同時返回參數和記錄集的存儲過程GetUserAccountRe2
public void runGetUserAccountRe2()
{
SqlCommand cmd = common("GetUserAccountRe2");
// 創建參數
IDataParameter[] parameters = {
new SqlParameter("@UserName", SqlDbType.NChar,20) ,
new SqlParameter("@UserID", SqlDbType.Int) ,
new SqlParameter("rval", SqlDbType.Int,4)
};
// 設置參數類型
parameters[0].Value = "7";
parameters[1].Direction = ParameterDirection.Output; // 設置為輸出參數
parameters[2].Direction = ParameterDirection.ReturnValue; //設置為返回值
// 添加參數
cmd.Parameters.Add(parameters[0]);
cmd.Parameters.Add(parameters[1]);
cmd.Parameters.Add(parameters[2]);
con.Open();
// 執行存儲過程并返回影響的行數
Label1.Text = cmd.ExecuteNonQuery().ToString();
DataSet ds = new DataSet();
SqlDataAdapter dt = new SqlDataAdapter(cmd);
dt.Fill(ds);
rpt.DataSource = ds;
rpt.DataBind();
con.Close();
// 顯示影響的行數和輸出參數
Label1.Text += "-輸出參數為:" + parameters[1].Value.ToString();
Label1.Text += "-返回值為:" + parameters[2].Value.ToString();
}
//返回多個記錄集的存儲過程
public void runGetUserAccountRe3()
{
DataSet ds = new DataSet();
SqlDataAdapter dt = new SqlDataAdapter(common("GetUserAccountRe3"));
dt.Fill(ds);
rpt1.DataSource = ds.Tables[0].DefaultView;
rpt1.DataBind();
rpt2.DataSource = ds.Tables[1].DefaultView;
rpt2.DataBind();
}
public SqlCommand common(string proName)
{
SqlCommand cmd = new SqlCommand();
// 設置sql連接
cmd.Connection = con;
// 如果執行語句
cmd.CommandText = proName;
// 指定執行語句為存儲過程
cmd.CommandType = CommandType.StoredProcedure;
return cmd;
}
}
select APP_NAME ( ) as w --當前會話的應用程序
select @@IDENTITY --返回最后插入的標識值
select USER_NAME() --返回用戶數據庫用戶名
SELECT @@CONNECTIONS --返回自上次SQL啟動以來連接或試圖連接的次數。
SELECT GETDATE() --當前時間
SELECT @@CPU_BUSY/100 --返回自上次啟動SQL 以來 CPU 的工作時間,單位為毫秒
USE tempdb SELECT @@DBTS as w --為當前數據庫返回當前 timestamp 數據類型的值。這一 timestamp 值保證在數據庫中是唯一的。
select @@IDENTITY as w --返回最后插入的標識值
SELECT @@IDLE as w --返回SQL自上次啟動后閑置的時間,單位為毫秒
SELECT @@IO_BUSY AS w --返回SQL自上次啟動后用于執行輸入和輸出操作的時間,單位為毫秒
SELECT @@LANGID AS w --返回當前所使用語言的本地語言標識符(ID)。
SELECT @@LANGUAGE AS w --返回當前使用的語言名
SELECT @@LOCK_TIMEOUT as w --當前會話的當前鎖超時設置,單位為毫秒。
SELECT @@MAX_CONNECTIONS as w --返回SQL上允許的同時用戶連接的最大數。返回的數不必為當前配置的數值
EXEC sp_configure --顯示當前服務器的全局配置設置
SELECT @@MAX_PRECISION as w --返回 decimal 和 numeric 數據類型所用的精度級別,即該服務器中當前設置的精度。默認最大精度38。
select @@OPTIONS as w --返回當前 SET 選項的信息。
SELECT @@PACK_RECEIVED as w --返回SQL自啟動后從網絡上讀取的輸入數據包數目。
SELECT @@PACK_SENT as w --返回SQ自上次啟動后寫到網絡上的輸出數據包數目。
SELECT @@PACKET_ERRORS as w --返回自SQL啟動后,在SQL連接上發生的網絡數據包錯誤數。
SELECT @@SERVERNAME as w --返回運行SQL服務器名稱。
SELECT @@SERVICENAME as w --返回SQL正在其下運行的注冊表鍵名
SELECT @@TIMETICKS as w --返回SQL服務器一刻度的微秒數
SELECT @@TOTAL_ERRORS AS w --返回 SQL服務器自啟動后,所遇到的磁盤讀/寫錯誤數。
SELECT @@TOTAL_READ as w --返回 SQL服務器自啟動后讀取磁盤的次數。
SELECT @@TOTAL_WRITE as w --返回SQL服務器自啟動后寫入磁盤的次數。
SELECT @@TRANCOUNT as w --返回當前連接的活動事務數。
SELECT @@VERSION as w --返回SQL服務器安裝的日期、版本和處理器類型。