2015-06-15 206 views
0

我是C#的新手,我想简单地将数据从SQL数据库拖入Visual Studio中的C#应用​​程序。 我一直在使用指南组合来做到这一点,但无法找到问题所在。如果我想调用没有指定参数的表,则代码有效,否则失败。在C#中将参数存储到DataGridView存储过程#

这是我的代码目前:

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; 
    } 
} 

这是错误消息我得到:

enter image description here

回答

1

您定义的参数两次!

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

删除

+0

OMG之一....我觉得我瞎了...... – Justin