从参数面板将参数传递给存储过程

时间:2013-09-20 18:38:52

标签: c# sql-server

我想从我在visual studio中的表单上添加的控件将参数传递给sqlserver中的存储过程,而不是在运行应用程序时自动出现的默认提示。 我想传递三个值作为参数。

deliverystatus - 待审,已批准,已分配,已确认(任何人)

startdateenddate

报告中生成的数据会根据投放状态和日期范围进行过滤。

但我无法将参数传递给我表单中控件的存储过程。

1 个答案:

答案 0 :(得分:2)

这是在sql中创建过程的示例代码:

CREATE PROCEDURE [dbo].[MJTestProc]
@deliverystatus nvarchar(50),
@startdate datetime,
@enddate datetime
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM tbl1 
WHERE deliverystatus=@deliverystatus
    AND DeliverDate>=@startdate
    AND DeliverDate<=@enddate
END

及以下是通过以下过程连接数据库和读取数据的示例代码:

    using System.Data.SqlClient;
    public void GetDataFromSProc(object sender, EventArgs e)
    {
        SqlConnection SConnect = new SqlConnection();
        SqlCommand SCommand = new SqlCommand();
        SqlDataAdapter SAdaptor = new SqlDataAdapter();
        SConnect.Open();
        // in the next line you execute your SProce with form controls content
        SCommand.CommandText = "exec [dbo].[MJTestProc] '" + RadioButton1.Text + "' , '" + DatePicker1.Text + "' , '" + DatePicker2.Text + "'";
        DataTable dt = new DataTable();
        SAdaptor.Fill(dt);
        SConnect.Close();
        //here you can assign dt content to a control. Below code is a sample to assign data to the reportviwer
        Microsoft.Reporting.WinForms.ReportDataSource myreportDataSource = new Microsoft.Reporting.WinForms.ReportDataSource();
        try
        {
            myreportDataSource.Name = "DataSource name in rdlc file";
            myreportDataSource.Value = dt;
            this.reportViewer1.LocalReport.DataSources.Add(myreportDataSource);
            this.reportViewer1.LocalReport.ReportEmbeddedResource = "YourNamespace.YourRDLCfilename.rdlc";

            this.reportViewer1.RefreshReport();
        }
        catch (Exception ex)
        {
        }

    }

您可以将此功能分配给您的radiobutons“CheckedChanged”事件和datepickers“ValueChanged”事件,因此每次radiobuttons或datepickers值更改时它都会运行。 (我希望它可以帮到你)