C#如何调用SQL存储过程

版权声明: https://blog.csdn.net/Bibabu135766/article/details/79566470

在工作中,经常遇到需要调用存储过程,如何调用呢?

下面实现的功能是判断某参数值,Fail时调用存储过程发送邮件至相关人员:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data.SqlClient;

public partial class SendEmail : System.Web.UI.Page
{
    public string mgs;
    protected void Page_Load(object sender, EventArgs e)
    {
    }   
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text=="Fail")
        {
        try
        {

            SqlConnection conn = new SqlConnection("Data Source=10.99.106.120;Initial Catalog=MES;User ID=fisya;pwd=gogofis");
            conn.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            comm.CommandText = "dbo.PlateCheck";
            comm.CommandType = System.Data.CommandType.StoredProcedure;
            //传值以及赋值

            string bianhao = TextBox2.Text;
            string RC = TextBox3.Text;
            string CA = TextBox4.Text;
            SqlParameter[] sps = new SqlParameter[] { 
                    new SqlParameter("@TextValue",bianhao),
                    new SqlParameter("@RC",RC),
                    new SqlParameter("@CA",CA),
                  };
            comm.Parameters.AddRange(sps);
            object Result1 = comm.ExecuteScalar();
            mgs = "<script>alert('" + Result1 + "')</script>";
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        }
    }
}

1,三个String时需要传递至存储过程的参数

2,Result1时存储存储过程返回来的结果(该出可以作为弹框等提示作用)

猜你喜欢

转载自blog.csdn.net/Bibabu135766/article/details/79566470