How to execute a stored procedure within C# program

前端 未结 13 1650
别跟我提以往
别跟我提以往 2020-11-22 00:10

I want to execute this stored procedure from a C# program.

I have written the following stored procedure in a SqlServer query window and saved it as stored1:

<
相关标签:
13条回答
  • 2020-11-22 00:38

    What I made, in my case I wanted to show procedure's result in dataGridView:

    using (var command = new SqlCommand("ProcedureNameHere", connection) {
                // Set command type and add Parameters
                CommandType = CommandType.StoredProcedure,
                Parameters = { new SqlParameter("@parameterName",parameterValue) }
            }) 
            {
                // Execute command in Adapter and store to dataset
                var adapter = new SqlDataAdapter(command);
                var dataset = new DataSet();
                adapter.Fill(dataset);
                // Display results in DatagridView
                dataGridView1.DataSource = dataset.Tables[0];
            }
    
    0 讨论(0)
提交回复
热议问题