服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - ASP.NET教程 - asp.net实现调用存储过程并带返回值的方法

asp.net实现调用存储过程并带返回值的方法

2019-12-31 14:09lyflcear ASP.NET教程

这篇文章主要介绍了asp.net实现调用存储过程并带返回值的方法,结合实例形式较为详细的分析了asp.net存储过程调用的相关技巧,需要的朋友可以参考下

本文实例讲述了asp.net实现调用存储过程并带返回值的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/// <summary>
/// DataBase 的摘要说明
/// </summary>
public class DataBase
{
    /// <summary>
    ///DataBase 的摘要说明
    /// </summary>
    protected static SqlConnection BaseSqlConnection = new SqlConnection();//连接对象
    protected SqlCommand BaseSqlCommand = new SqlCommand(); //命令对象
    public DataBase()
    {
      //
      // TODO: 在此处添加构造函数逻辑
      //
    }
    protected void OpenConnection()
    {
      if (BaseSqlConnection.State == ConnectionState.Closed) //连接是否关闭
        try
        {
          BaseSqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["productsunion"].ToString();
          BaseSqlCommand.Connection = BaseSqlConnection;
          BaseSqlConnection.Open();
        }
        catch (Exception ex)
        {
          throw new Exception(ex.Message);
        }
    }
    public void CloseConnection()
    {
      if (BaseSqlConnection.State == ConnectionState.Open)
      {
        BaseSqlConnection.Close();
        BaseSqlConnection.Dispose();
        BaseSqlCommand.Dispose();
      }
    }
    public bool Proc_Return_Int(string proc_name, params SqlParameter[] cmdParms)
    {
      try
      {
        OpenConnection();
        if (cmdParms != null)
        {
          foreach (SqlParameter parameter in cmdParms)
          {
            if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
              (parameter.Value == null))
            {
              parameter.Value = DBNull.Value;
            }
            BaseSqlCommand.Parameters.Add(parameter);
          }
          BaseSqlCommand.CommandType = CommandType.StoredProcedure;
          BaseSqlCommand.CommandText = proc_name;
          BaseSqlCommand.ExecuteNonQuery();
          if (BaseSqlCommand.Parameters["Return"].Value.ToString()== "0")
          {
            return true;
          }
          else
          {
            return false;
          }
        }
        else
        {
          return false;
        }
      }
      catch
      {
        return false;
      }
      finally
      {
        BaseSqlCommand.Parameters.Clear();
        CloseConnection();
      }
    }
}

加入了一个组合类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class SqlModel:ISqlModel
{
  #region ISqlModel 成员
  public bool Proc_Return_Int(string proc_name, string[,] sArray)
  {
    try
    {
      if (sArray.GetLength(0) >= 1)
      {
        DataBase db = new DataBase();
        SqlParameter[] sqlpar = new SqlParameter[sArray.GetLength(0)+1];//加入返回值
        for (int i = 0; i < sArray.GetLength(0); i++)
        {
          sqlpar[i] = new SqlParameter(sArray[i,0], sArray[i,1]);
        }
        sqlpar[sArray.GetLength(0)] = new SqlParameter("Return", SqlDbType.Int);
        sqlpar[sArray.GetLength(0)].Direction = ParameterDirection.ReturnValue;
        if (db.Proc_Return_Int(proc_name, sqlpar))
        {
          return true;
        }
        else
        {
          return false;
        }
      }
      else
      {
        return false;
      }
    }
    catch
    {
      return false;
    }
  }
  #endregion
}

前台调用

?
1
2
3
4
5
6
7
8
9
string[,] sArray = new string[3,2];
sArray[0,0]="@parent_id";
sArray[1,0]="@cn_name";
sArray[2,0]="@en_name";
sArray[0,1]="5";
sArray[1,1]="aaaab";
sArray[2,1]="cccccc";
Factory.SqlModel sm = new Factory.SqlModel();
sm.Proc_Return_Int("Product_Category_Insert", sArray);

存储过程内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
ALTER PROCEDURE [dbo].[Product_Category_Insert]
  @parent_id int,
  @cn_Name nvarchar(50),
  @en_Name nvarchar(50)
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE @ERR int
  SET @ERR=0
  BEGIN TRAN
  IF @parent_id<0 OR ISNULL(@cn_Name,'')=''
    BEGIN
      SET @ERR=1
      GOTO theEnd
    END
  IF(NOT EXISTS(SELECT Id FROM Product_Category WHERE Id=@parent_id))
    BEGIN
      SET @ERR=2
      GOTO theEnd
    END
  DECLARE @Id int,@Depth int,@ordering int
  SELECT @Id=ISNULL(MAX(Id)+1,1) FROM Product_Category--计算@Id
  IF @Parent_Id=0
    BEGIN
      SET @Depth=1--计算@Depth
      SELECT @Ordering=ISNULL(MAX(Ordering)+1,1) FROM Product_Category--计算@OrderId
    END
  ELSE
    BEGIN
      SELECT @Depth=Depth+1 FROM Product_Category WHERE Id=@Parent_Id--计算@Depth,计算@Ordering时需要用到
      SELECT @Ordering=MAX(Ordering)+1 FROM Product_Category--计算@Ordering
        WHERE Id=@Parent_Id
      UPDATE Product_Category SET Ordering=Ordering+1 WHERE Ordering>=@Ordering--向后移动插入位置后面的所有节点
    END
  INSERT INTO Product_Category(Id,Parent_Id,cn_Name,en_name,Depth,Ordering) VALUES (@Id,@Parent_Id,@cn_Name,@en_name,@Depth,@Ordering)
  IF @@ERROR<>0
    SET @ERR=-1
  theEnd:
  IF @ERR=0
    BEGIN
    COMMIT TRAN
    RETURN 0
    END
  ELSE
    BEGIN
    ROLLBACK TRAN
    RETURN @ERR
    END
END

希望本文所述对大家asp.net程序设计有所帮助。

延伸 · 阅读

精彩推荐