【问题标题】:How to use DataAdapter to call a stored procedure in C# with variable parameters如何使用 DataAdapter 在 C# 中调用带有可变参数的存储过程
【发布时间】:2015-10-27 18:09:26
【问题描述】:

我在 C# 中调用以下代码来用给定的存储过程 "sp1_name" 填充 dataAdapter。问题是我想用不同的参数调用不同的存储过程。 (所有 SP 都执行 SELECT) 假设我的存储过程名称是“SP_SOMESP”,那么一切正常。

假设我的存储过程名称是“SP_SOMESP @Month= 10, @Year = 2010”,那么它不起作用。它抛出一个找不到这个存储过程的异常。

有什么解决办法吗?

谢谢!

//First Connection - SP1
using (SqlConnection con = new SqlConnection(conStr))
{
            using (SqlCommand cmd = new SqlCommand(sp1_name, con)) //sp1_name = NAME + PARAMETERS
            {
                cmd.CommandTimeout = 3600;
                cmd.CommandType = CommandType.StoredProcedure;

                using (SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd))
                {
                    dataAdapter.Fill(results2);
                }

            }
}

【问题讨论】:

    标签: c# sql sql-server stored-procedures connection


    【解决方案1】:

    第一期:
    存储过程中的参数不应与其名称一起包含
    第二个问题:
    在存储过程的名称中包含空格不是一个好习惯。
    对于后面的代码

    using(SqlConnection con = new SqlConnection("Your Connection String Here"))
    { 
        SqlCommand cmd = new SqlCommand("sp_SomeName", con);
        cmd.CommandType = CommandType.StoredProcedure;
    
        //the 2 codes after this comment is where you assign value to the parameters you
        //have on your stored procedure from SQL
        cmd.Parameters.Add("@MONTH", SqlDbType.VarChar).Value = "someValue";
        cmd.Parameters.Add("@YEAR", SqlDbType.VarChar).Value = "SomeYear";
    
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        SqlDataSet ds = new SqlDataSet();
        da.Fill(ds); //this is where you put values you get from the Select command to a 
      //dataset named ds, reason for this is for you to fetch the value from DB to code behind
    
        foreach(DataRow dr in ds.Tables[0].Rows) // this is where you run through the dataset and get values you want from it.
        {
           someTextBox.Text = dr["Month"].ToString(); //you should probably know this code
        }
    }
    

    【讨论】:

      【解决方案2】:

      您必须以编程方式添加参数,请参阅SqlCommand.Parameters

      应该是这样的

      cmd.Parameters.AddWithValue("@Month", 10);
      cmd.Parameters.AddWithValue("@Year", 2010);
      

      这将在命令声明之后执行之前。

      如果你发现需要对数据类型进行delcare,那就这样试试吧

      cmd.Parameters.Add("@Month", SqlDbType.Int).Value = 10;
      

      【讨论】:

      • 您确实应该始终声明数据类型。它消除了 .net 出错的可能性。 blogs.msmvps.com/jcoehoorn/blog/2014/05/12/…
      • 没错,这是一个很好的做法。对该文章的评论提到,在使用存储过程时这没什么大不了的,因为存储过程将具有已声明为特定类型的参数。
      • 同意。我个人更喜欢始终指定数据类型和大小。而且您不必记录为什么这次使用 AddWithValue 和 Add 那次。 :)
      • 你说得对,只有更具体一些才会有帮助。
      【解决方案3】:

      检查一下,

      using (SQLCommand cmd = new SQLCommand())
      {
      cmd.CommandText = "SP_SOMESP";
      cmd.Parameters.Add("@Month", 10);
      cmd.Parameters.Add("@Year", 2010);
      cmd.CommandTimeout = 3600;
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Connection = con;
      }
      using (SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd))
      {
        dataAdapter.SelectCommand = cmd;
        dataAdapter.Fill(results2);
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-01
        • 2011-11-24
        • 1970-01-01
        • 1970-01-01
        • 2011-09-18
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        • 2013-06-24
        相关资源
        最近更新 更多