简体   繁体   English

带参数的存储过程到C#中的DataGridView中

[英]Stored Procedure with parameters into DataGridView in C#

I'm new to C# and I want to simply pull data from SQL database into my C# application in Visual Studios. 我是C#的新手,我想简单地将数据从SQL数据库提取到Visual Studios中的C#应用​​程序中。 I have been using a mix of guides to do this but cannot find what is wrong. 我一直在使用各种指南来执行此操作,但找不到错误所在。 The code works if I want to recall a table without specifying parameters otherwise it fails. 如果我想在不指定参数的情况下调用表,该代码将起作用,否则它将失败。

This is my code currently: 这是我目前的代码:

string CS =  "Server = server-sql1;Database=Accounts;Trusted_connection=true";
using (SqlConnection con = new SqlConnection(CS))
{
    SqlCommand cmd = new SqlCommand("ASP_SLINVOICE", con);

    //specify that it is a stored procedure and not a normal proc
    cmd.CommandType = System.Data.CommandType.StoredProcedure;

    //list the parameters required and what they should be
    cmd.Parameters.AddWithValue("@varCURRENCY", "USD");
    cmd.Parameters.AddWithValue("@invSTART", "0");
    cmd.Parameters.AddWithValue("@invEND", "399999");
    cmd.Parameters.AddWithValue("@varCURRENCY", "0");

    con.Open();
    cmd.ExecuteNonQuery();

    using(SqlDataAdapter adap = new SqlDataAdapter(cmd))
    {
        DataTable dt = new DataTable();
        adap.Fill(dt);
        dataGridView1.DataSource = dt;
    }
}

This is the error message I get: 这是我收到的错误消息:

在此处输入图片说明

you define the parameter twice! 您两次定义参数!

cmd.Parameters.AddWithValue("@varCURRENCY", "USD"); // #1
cmd.Parameters.AddWithValue("@invSTART", "0");
cmd.Parameters.AddWithValue("@invEND", "399999");
cmd.Parameters.AddWithValue("@varCURRENCY", "0"); // #2

remove one of them 删除其中之一

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

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