C#调用类中的方法获取返回Sql Server存储过程的返回值

新建一个类;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace AutoTest
{
    public class SqlQuery
    {
        public static string connectstring = "数据库链接";
        public static string[] Getsheetid(string connect, string procname,SqlParameter[] prams,string Resvalue)
        {
            using (SqlConnection con = new SqlConnection(connect))
            {
                string[] sheetid = { "Y", "" };
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand(procname,con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    if (prams != null)
                    {
                        foreach (SqlParameter parameter in prams)
                        {
                            cmd.Parameters.Add(parameter);
                        }
                    }
                    cmd.ExecuteScalar();
                    if (!string.IsNullOrEmpty(Resvalue))
                    {//Resvalue为返回参数,有返回时添加,无返回就不添加
                        sheetid[1] = cmd.Parameters[Resvalue].Value.ToString();
                    }
                }
                catch (Exception ex)
                {
                    sheetid[0] = "N";
                    sheetid[1] = ex.Message.ToString();

//sheetid[1] = ex.ToString(); //返回异常信息

//sheetid[1] = ex.Message.ToString(); //返回数据库中的错误信息;一般常用这个

//sheetid[1] = ex.StackTrace.ToString();//返回C#调用逻辑的异常信息

//sheetid[1] = ex.TargetSite.ToString();//返回一个异常信息,

                }
                return sheetid;
            }
        }
    }
}

在其他From中调用,新建一个buttom,下面为单击事件

private void button3_Click(object sender, EventArgs e)
        {
            string proname = "P_newsheetid";//存储过程名称
            SqlParameter[] par= {
                new SqlParameter("@sheettype",SqlDbType.Int,10,ParameterDirection.Input,false,0,0,string.Empty,DataRowVersion.Default,"80001"), //输入类型
                new SqlParameter("@sheetid",SqlDbType.VarChar,20,ParameterDirection.Output,false,0,0,string.Empty,DataRowVersion.Default,null), //输出参数,返回过程结果值
            };
            string[] sheetid = SqlQuery.Getsheetid(SqlQuery.connectstring,proname,par,"@sheetid");
           if (sheetid[0]=="Y")
            {
                MessageBox.Show(sheetid[1]); //输入返回值;
                return;
            }
            MessageBox.Show(sheetid[1]);//输出返回的错误信息;
        }

//ex.Message.ToString(); //返回数据库中的错误信息;一般常用这个

//ex.StackTrace.ToString();//返回C#调用逻辑的异常信息

//ex.TargetSite.ToString();//返回一个异常信息,

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值