简体   繁体   English

从ASP.NET WinForm向存储过程传递参数

[英]Passing parameter to stored procedure from ASP.NET WinForm

I'm having trouble with passing parameter to stored procedure. 我在将参数传递给存储过程时遇到麻烦。 No error thrown but desired output cannot be acquired. 没有引发任何错误,但是无法获取所需的输出。 .cs is: .cs是:

private void btnSubmit_Click(object sender, EventArgs e)
{
    try
    {
        if (cbRegions.SelectedIndex > 0)
        {
            con = new SqlConnection(connectionString);
            cmd = new SqlCommand("dbo.SectionIncharge", con);
            cmd.Parameters.Add("@RegionName", SqlDbType.VarChar, 50).Value = cbRegions.SelectedItem.ToString();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            con.Open();
            DataTable table = new DataTable();
            table.Load(cmd.ExecuteReader());
            dgvResults.DataSource = table;
            con.Close();
        }
    }
    catch
    {

    }
}

.sql is: .sql是:

ALTER PROCEDURE [dbo].[SectionIncharge] @RegionName varchar(50)  --name
AS
select UserName
from SystemUser 
where Description LIKE 'RD OFFICE ' + @RegionName  AND
Enable = 'Y'
GO

I've double checked stored procedure working fine in SQL Server Management Studio but when I'm trying to call it from WinForm, no output. 我仔细检查了存储过程在SQL Server Management Studio中的运行情况,但是当我尝试从WinForm调用它时,没有输出。 Can someone please help. 有人可以帮忙吗? Thanks 谢谢

Method ExecuteNonQuery (as it can be guessed from the name) just executes query and doesn't receives any output from server. 方法ExecuteNonQuery (可以从名称中猜出)仅执行查询,而不会从服务器接收任何输出。

So of you need to get results you're selecting - you have to use ExecuteReader method and process result it returns (see example at the bottom of MSDN page I've linked). 因此,您需要获得要选择的结果-您必须使用ExecuteReader方法并处理返回的结果(请参阅我链接的MSDN页面底部的示例)。

Alternatively, if you're using datatables as source for your grid - you can use SqlDataAdapter class and it's Fill method to fill such a datatable. 另外,如果您将数据表用作网格的源,则可以使用SqlDataAdapter类,并且该类的Fill方法可以填充此类数据表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM