【问题标题】:The parameterized query expects the parameter ###### which was not supplied参数化查询需要未提供的参数######
【发布时间】:2011-01-14 01:30:23
【问题描述】:

我正在尝试运行此代码,但出现错误“参数化查询需要未提供的参数 @faid”。至少根据我的知识,这段代码看起来不错。我在以 SQLEXPRESS 作为后端的 Windows 7 上使用 VS 2010。

提前致谢。


       string getDataQuery;
        lcFaid = "70464917-967b-4796-9483-3b0b4b004a3e";

        SqlConnection sqlConnection1 = new SqlConnection(ccsConnectionString);

        DataSet data = new DataSet();
        data.Locale = System.Globalization.CultureInfo.InvariantCulture;

        getDataQuery =
            "SELECT customer,custtrack,ackdate FROM famain WHERE faid = @lcFaid";

        SqlDataAdapter masterDataAdapter = new SqlDataAdapter();
        masterDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        masterDataAdapter.SelectCommand = new SqlCommand();
        masterDataAdapter.SelectCommand.Connection = sqlConnection1;

        masterDataAdapter.SelectCommand.Parameters.Add("@lcFaid",
            SqlDbType.UniqueIdentifier, 36, "faid").SourceVersion = DataRowVersion.Original;


       masterDataAdapter.SelectCommand.CommandText = getDataQuery;
       masterDataAdapter.Fill(data, "famain");

【问题讨论】:

  • 所以您有一个名为@lcFaid 的参数,但错误消息说它正在寻找一个名为@faid 的参数?这很奇怪。

标签: c#


【解决方案1】:

我看到您从不使用变量lcFaid。也许您打算使用它而不是文字字符串 "faid"(根本不是 GUID)?

masterDataAdapter.SelectCommand.Parameters.Add("@lcFaid",
        SqlDbType.UniqueIdentifier, 36, lcFaid).SourceVersion = DataRowVersion.Original;

【讨论】:

    【解决方案2】:

    对此不确定-但我会

    • 首先设置命令的查询文本(定义将有什么参数)
    • 然后将参数添加到集合中

    类似这样的:

    string getDataQuery =
            "SELECT customer,custtrack,ackdate FROM famain WHERE faid = @lcFaid";
    
    SqlDataAdapter masterDataAdapter = new SqlDataAdapter();
    masterDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    
    masterDataAdapter.SelectCommand = new SqlCommand();
    masterDataAdapter.SelectCommand .Connection = sqlConnection1;
    
    // first set the query text
    masterDataAdapter.SelectCommand.CommandText = getDataQuery;
    
    // after that, define the parametesr
    masterDataAdapter.SelectCommand.Parameters
      .Add("@lcFaid", SqlDbType.UniqueIdentifier, 36, "faid").SourceVersion = DataRowVersion.Original;
    

    此外,正如“Gabe”在他的评论中指出的那样 - 错误消息引用了一个参数 @faid,但您正在定义和设置一个名为 @lcFaid 的参数 - 为什么??

    【讨论】:

    • 谢谢!只是为了节省时间,我创建了一个存储过程并调用它来获取相同的数据。不过,我会在业余时间继续使用此代码并在此处发布我的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多