存储过程参数到DataGridView在C# [英] Stored Procedure with parameters into DataGridView in C#

查看:167
本文介绍了存储过程参数到DataGridView在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我想简单地将数据从SQL数据库拉到Visual Studio中的C#应用​​程序。
我一直在使用各种指南来做到这一点,但找不到什么问题。如果我想在没有指定参数的情况下调用表,则代码可以工作。否则它将失败。

I'm new to C# and I want to simply pull data from SQL database into my C# application in Visual Studios. 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.

这是我现在的代码:

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:

推荐答案

您定义参数两次!

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

删除其中一个

这篇关于存储过程参数到DataGridView在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆