|
您好我尝试在sql中使用输出值创建sp,
: b& B# Y% N1 [, G& @这是代码
: j n; {/ U% i* c; P: M5 qALTER PROCEDURE [dbo].[usp_SelectHospital_IfExiste_Department]
# h& p4 u; R% O8 k* Q, }# L@HospitalDepartmentID INT,
' \: K) Y9 m; S: G. o; ]6 g/ W@IfExiste INT OUTPUT
! \# q3 q0 e$ P/ Q0 K9 G) WAS
1 O' X- y+ K. r SET NOCOUNT ON% [/ Y& t1 Z) _: h! U
SET TRANSACTION ISOLATION LEVEL READ COMMITTED* K$ L2 L9 V5 ]2 ^6 O2 @5 e
IF NOT EXISTS (SELECT c.DeptID FROM Clinic c WHERE DeptID=@HospitalDepartmentID )
/ a" j" O9 P7 c9 o! gBEGIN8 q6 h3 R2 Z1 L9 Z/ U% R
8 i: y; A2 r/ p/ [& P- s
SET @IfExiste=06 V b* l, h; a2 L* T
SELECT' [; w- _- N* g/ O
[HospitalDepartmentID],
1 X; g1 [2 {6 K8 x [NAME]
/ A+ F5 G% [; ~$ x: j8 d# uFROM; Y8 _6 t# D$ i7 J: v
[dbo].[Hospital_Department]
! ?3 l3 k8 A0 mWHERE
$ _# N9 p$ n# | x8 u' _; o [HospitalDepartmentID] = @HospitalDepartmentID
' |8 C8 \" t4 {+ x* y1 V* ?$ q3 ^END: M# z1 y* q3 i# U7 x: e
ELSE# N. S% |2 u! o% F& K& p
BEGIN* w( W. p" L& I9 v7 q0 M
SET @IfExiste=1
" n! T4 ]# g' H; s* [SELECT
) u0 G) c2 g5 }+ B [HospitalDepartmentID],7 P* S; g8 G E1 h9 ?- c
[NAME]
3 [) E; N8 I; H) sFROM2 Z' C f, P' j/ O ~
[dbo].[Hospital_Department]* A5 T% K( h; z6 Y
WHERE
: y. p5 F$ X: g! v$ J. g# P [HospitalDepartmentID] = @HospitalDepartmentID
& Q' R: L) a+ E+ X" `2 w: M( }) I END
9 O$ T! |- N M+ v9 a9 U K和C#代码 7 R' x% J. j5 b' D
public static Hospital_Department GetDepartmentInfo(int ID,int OutIfExist), J# _. D& N. U& |1 z! U
{
* t4 v8 p; ~1 S* O2 J# i7 X, G SqlCommand cmd;# y( c5 G+ i, j' J9 K9 j2 ?! ?- L
SqlDataReader dr;+ z, b" D b2 x$ A* B- m: M0 N1 b6 D
Hospital_Department HD = new Hospital_Department();
. a. j+ W: `; g! X: A using (cmd = new SqlCommand("usp_SelectHospital_IfExiste_Department", ProjectCon.GetCon())); h) @6 [ E, h2 [
{5 M, W( P( }! G1 Q1 ]
cmd.CommandType = CommandType.StoredProcedure;* g$ u+ R3 V# H/ q* g3 {& p8 b
cmd.Parameters.AddWithValue("@HospitalDepartmentID", ID);
1 S W! |4 D$ K" K" E //cmd.Parameters.Add("@IfExiste",SqlDbType.Int).Direction = ParameterDirection.Output;
" ? T2 b+ L" S9 D' F7 G, a cmd.Parameters.Add("@IfExiste",SqlDbType.Int);: r8 A. n( S' p- I2 }& j
cmd.Parameters["@IfExiste"].Direction = ParameterDirection.Output;; M' q& K- z9 P# b
dr = cmd.ExecuteReader();
. @; q! }! O8 i) g5 ] while (dr.Read())
& ?, t$ q( k/ p) E! Q {- a1 u5 B8 ]& O: i. `+ e8 {
HD.NAME = dr["NAME"].ToString();/ c! o* e. \" N" N
HD.HospitalDepartmentID = Convert.ToInt32(dr["HospitalDepartmentID"]);
' G1 V9 \$ ^: w. g! V! C8 b1 |5 G }5 u& Y E$ u3 Z1 y, S p# a
OutIfExist = Convert.ToInt32(cmd.Parameters["@IfExiste"].Value);
; \) p( u$ ^2 G9 g9 l1 s return HD;
" i$ [) A8 ~1 r9 f% |, t$ s8 ] }
0 f9 i* K" F# I/ k }
4 f) p& Q8 q$ [6 Y& \& B7 P* `% A当我尝试获取输出值始终为null时,我在sql中运行了存储过程,并返回了该值,因此plz告诉我代码thx出了什么问题
- k1 j1 D+ I( h2 S5 \
1 F5 [: f! u( x7 e5 F' S* J解决方案:1 B& i& A' l% [1 t6 R) U
6 s4 l/ y' N( x, o
6 C+ }( `2 w+ f. v7 n$ B% I- M3 i( R# a1 K r8 M: C
也许这个问题有有用的信息:
( @8 h4 K5 ^, N( L$ |4 a2 T9 r2 f根据 http://msdn.microsoft.com/zh-0 p8 n/ N5 l1 ~
cn/library/ms971497,必须在处理输出参数之前关闭数据读取器。
8 N0 J' Y3 U8 Q2 V- g希望能帮助到你。 |
|