-1

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

deliverystatus-待定、已批准、已分配、已确认(任何人)

startdateenddate

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

但是我无法从表单中的控件获取要传递到存储过程中的参数。

4

1 回答 1

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 GetDataFromStoredProcedure(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 stored procedure 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 reportviewer
        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)
        {
        }
    }

您可以将此函数分配给您的单选按钮“CheckedChanged”事件和日期选择器“ValueChanged”事件,因此每次单选按钮或日期选择器值更改时它都会运行。

(希望对你有帮助)

于 2013-09-20T19:43:49.937 回答