C# 2.0 Vista,sql2000带返回值存储过程调用
sql 2000 code:
1IF EXISTS (SELECT name FROM sysobjects
2 WHERE name = 'Proc_Unite_Delete' AND type = 'P')
3 DROP PROCEDURE Proc_Unite_Delete
4GO
5CREATE PROCEDURE Proc_Unite_Delete
6 @UniteID INT,
7 @Return VARCHAR(300) OUTPUT
8AS
9DECLARE @Ccount int, @Pcount int
10 SELECT @Ccount=COUNT(*) FROM C_Physician WHERE UniteID=@UniteID
11 SELECT @Pcount=COUNT(*) FROM Physician WHERE UniteID=@UniteID
12--SELECT @Ccount
13--SELECT @Pcount
14 IF exists(SELECT * FROM Unite WHERE UniteID=@UniteID) AND @Ccount=0 AND @Pcount=0
15 BEGIN
16
17 DELETE FROM Unite
18 WHERE UniteID=@UniteID
19 SET @Return='成功'
20 --PRINT 'OK'
21 RETURN 1
22
23 END
24 SET @Return='不成功'
25 --PRINT 'NO'
26 RETURN 0
27
28GO
29---测试代码
30DECLARE @srturn VARCHAR(300) ,@id int
31set @id=6
32EXEC Proc_Unite_Delete 6,@Return=@srturn OUTPUT
33SELECT @srturn
2 WHERE name = 'Proc_Unite_Delete' AND type = 'P')
3 DROP PROCEDURE Proc_Unite_Delete
4GO
5CREATE PROCEDURE Proc_Unite_Delete
6 @UniteID INT,
7 @Return VARCHAR(300) OUTPUT
8AS
9DECLARE @Ccount int, @Pcount int
10 SELECT @Ccount=COUNT(*) FROM C_Physician WHERE UniteID=@UniteID
11 SELECT @Pcount=COUNT(*) FROM Physician WHERE UniteID=@UniteID
12--SELECT @Ccount
13--SELECT @Pcount
14 IF exists(SELECT * FROM Unite WHERE UniteID=@UniteID) AND @Ccount=0 AND @Pcount=0
15 BEGIN
16
17 DELETE FROM Unite
18 WHERE UniteID=@UniteID
19 SET @Return='成功'
20 --PRINT 'OK'
21 RETURN 1
22
23 END
24 SET @Return='不成功'
25 --PRINT 'NO'
26 RETURN 0
27
28GO
29---测试代码
30DECLARE @srturn VARCHAR(300) ,@id int
31set @id=6
32EXEC Proc_Unite_Delete 6,@Return=@srturn OUTPUT
33SELECT @srturn
c#2.0 code:
1DialogResult dr =MessageBox.Show("是否真的要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
2 if (dr == DialogResult.Yes)
3 {
4 try
5 {
6 DBConnectioin dbc = new DBConnectioin();
7 SqlConnection conn = new SqlConnection(DBConnectioin.conStr);
8 conn.Open();
9 SqlCommand cmd = new SqlCommand("Proc_Unite_Delete", conn);//调用存储过程
10 cmd.CommandType = CommandType.StoredProcedure;
11 SqlParameter pares = new SqlParameter("@UniteID", SqlDbType.Int, 8);//输入参数
12 SqlParameter pares1 = new SqlParameter("@Return", SqlDbType.VarChar, 30);//输出参数
13 cmd.Parameters.Add(pares);
14 cmd.Parameters.Add(pares1);
15 cmd.Parameters["@UniteID"].Value = Int32.Parse(this.txtUniteID.Text.Trim());
16 cmd.Parameters["@Return"].Direction = ParameterDirection.Output;
17 cmd.ExecuteNonQuery();
18 String Retur = (string)cmd.Parameters["@Return"].Value;
19 conn.Close();
20 MessageBox.Show("删除"+Retur, "提示");
21 this.FrmUnite_Load(sender, e);
22 }
23 catch (Exception ex)
24 { }
2 if (dr == DialogResult.Yes)
3 {
4 try
5 {
6 DBConnectioin dbc = new DBConnectioin();
7 SqlConnection conn = new SqlConnection(DBConnectioin.conStr);
8 conn.Open();
9 SqlCommand cmd = new SqlCommand("Proc_Unite_Delete", conn);//调用存储过程
10 cmd.CommandType = CommandType.StoredProcedure;
11 SqlParameter pares = new SqlParameter("@UniteID", SqlDbType.Int, 8);//输入参数
12 SqlParameter pares1 = new SqlParameter("@Return", SqlDbType.VarChar, 30);//输出参数
13 cmd.Parameters.Add(pares);
14 cmd.Parameters.Add(pares1);
15 cmd.Parameters["@UniteID"].Value = Int32.Parse(this.txtUniteID.Text.Trim());
16 cmd.Parameters["@Return"].Direction = ParameterDirection.Output;
17 cmd.ExecuteNonQuery();
18 String Retur = (string)cmd.Parameters["@Return"].Value;
19 conn.Close();
20 MessageBox.Show("删除"+Retur, "提示");
21 this.FrmUnite_Load(sender, e);
22 }
23 catch (Exception ex)
24 { }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)