在C 中使用儲存過程

2021-04-14 11:18:50 字數 1437 閱讀 2042

本文以sql server2000為例,示例資料庫為china,表為test,來說明以c#中使用sql儲存過程的方法。

一、test表的建立sql語句:

create table test55

(uid int identity(1,1),

class1 varchar(20),

class2 varchar(20),

uname varchar(20),

birth **alldatetime,

meno varchar(50)

)alter table test55

add constraint primary_id  primary key(uid)

二、建立乙個有輸入、輸出、返回值引數的儲存過程:

create proc proc_out @uid int,@output varchar(200) output

as--select結果集

select * from test where uid>@uid

--對輸出引數進行賦值

set @output='記錄總數:'+convert(varchar(10),(select count(*) from test))

--使用return,給儲存過程乙個返回值。

return 200;

go

三、在c#中,操作儲存過程:

3.1 使用帶有引數的sql語句

private void sql_param()

3.2 儲存過程的使用標準版

private void sql_proc()

四、總結與理解:

sqlcommand類,提供了對t-sql語句和儲存過程的執行能力;它不只能執行某乙個sql語句,亦可執行一組sql語句,如建立表,修改表等,可以這樣理解,對於sql server而言,只要在查詢分析器中可執行的一組語句,通過sqlcommand同樣能得到很好的執行。sqlcommand預設執行方式,是執行t-sql語句,即comm.commandtype=commandtype.text。在執行儲存過程時,可以用"exec 過程名 引數"的形式,將其作為t-sql語句,來執行,也可以將sqlcommand的執行方式改為執行過程方式,即comm.commandtype=commandtype.storedprocedure;兩種執行儲存過程方式的主要區別是,前者在獲得輸出引數和返回值上,很困難;後者是全面的,也是微軟專門為執行儲存過程而定義的全面策略。

(源自:

在C 中使用儲存過程

本文以sql server2000為例,示例資料庫為china,表為test,來說明以c 中使用sql儲存過程的方法。一 test表的建立sql語句 create table test55 uid int identity 1,1 class1 varchar 20 class2 varchar 2...

在C 中使用儲存過程

在c 中使用儲存過程 本文以sql server2000為例,示例資料庫為china,表為test,來說明以c 中使用sql儲存過程的方法。一 test表的建立sql語句 create table test55 uid int identity 1,1 class1 varchar 20 class...

在C 中使用儲存過程

2007年05月23日 23 42 00 本文以sql server2000為例,示例資料庫為china,表為test,來說明以c 中使用sql儲存過程的方法。一 test表的建立sql語句 create table test55 uid int identity 1,1 class1 varcha...