C# asp.net:错误 - 没有为一个或多个必需参数给出值

发布于 2024-10-26 12:15:06 字数 1179 浏览 1 评论 0原文

这是数据库层函数的代码:

    public static dsPersonnel GetPersonnel(string Database, string strSearch)
    {
        dsPersonnel DS;
        OleDbConnection sqlConn;
        OleDbDataAdapter sqlDA;

        //create the connection string
        sqlConn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source=" + Database);

        string query;
        if (strSearch == "" || strSearch.Trim().Length == 0)
        {
            query = "SELECT * from tblPersonnel";
        }
        else
        {
            query = "SELECT * FROM tblPersonnel WHERE LastName = " + strSearch + "";
        }




        //create the adapter with query
        sqlDA = new OleDbDataAdapter(query, sqlConn);

        //create the dataset
        DS = new dsPersonnel();

        //fill the data set
        sqlDA.Fill(DS.tblPersonnel);

        //return the dataset
        return DS;
    }
}

如果回发提交空白字符串,我让它返回所有记录。但是当将真实字符串传递给函数时,我收到错误“没有为一个或多个必需参数给出值”。对于“In debug”行

sqlDA.Fill(DS.tblPersonnel);

,我验证了这两种情况下的字符串都正确构建,但后者出现错误。 我知道我应该使用参数化查询,但这是我在课堂上学习过程中的步骤。一次解决一个问题:)。关于这里的问题有什么建议吗?

Here is the code for the database layer function:

    public static dsPersonnel GetPersonnel(string Database, string strSearch)
    {
        dsPersonnel DS;
        OleDbConnection sqlConn;
        OleDbDataAdapter sqlDA;

        //create the connection string
        sqlConn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source=" + Database);

        string query;
        if (strSearch == "" || strSearch.Trim().Length == 0)
        {
            query = "SELECT * from tblPersonnel";
        }
        else
        {
            query = "SELECT * FROM tblPersonnel WHERE LastName = " + strSearch + "";
        }




        //create the adapter with query
        sqlDA = new OleDbDataAdapter(query, sqlConn);

        //create the dataset
        DS = new dsPersonnel();

        //fill the data set
        sqlDA.Fill(DS.tblPersonnel);

        //return the dataset
        return DS;
    }
}

if the postback submits a blank string, i have it return all records. but when a real string is passed to the function, i get the error "no value given for one or more required parameters". for the line

sqlDA.Fill(DS.tblPersonnel);

In debug, I verified that the string is building correctly for both cases, but i get the error with the latter. I understand that I should be using parameterized queries, but this is the step in the learning process in class that I am on. One problem at a time :). Any suggestions on what the issue here is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

↘人皮目录ツ 2024-11-02 12:15:09
query = 
     "SELECT * FROM tblPersonnel WHERE LastName = '" + 
      strSearch + 
      "'";

在搜索条件周围添加引号

query = 
     "SELECT * FROM tblPersonnel WHERE LastName = '" + 
      strSearch + 
      "'";

Add quotes around the search criteria

等风来 2024-11-02 12:15:09

在 SQL 中,您必须将字符串括在单引号中。

query = "SELECT * FROM tblPersonnel WHERE LastName = '" + strSearch + "'"; 

You have to enclose strings in single quotes in SQL.

query = "SELECT * FROM tblPersonnel WHERE LastName = '" + strSearch + "'"; 
肩上的翅膀 2024-11-02 12:15:09

尝试

query = "SELECT * FROM tblPersonnel WHERE LastName = '" + strSearch + "'";

或者更好

query = string.Format("SELECT * FROM tblPersonnel WHERE LastName = '{0}'", strSearch);

Try

query = "SELECT * FROM tblPersonnel WHERE LastName = '" + strSearch + "'";

or better

query = string.Format("SELECT * FROM tblPersonnel WHERE LastName = '{0}'", strSearch);
神经大条 2024-11-02 12:15:09

看起来您没有引用传入查询的参数。

而不是

query = "SELECT * FROM tblPersonnel WHERE LastName = " + strSearch + "";

尝试

query = "SELECT * FROM tblPersonnel WHERE LastName = '" + strSearch.Replace("'", "''") + "'";

注意额外的单引号,以及正确转义过滤器中包含的单引号(例如撇号)的 Replace() 语句

必须重申您的示例代码确实包含多个问题和安全漏洞。

Doesn't look like you're quoting the parameter you're passing in to your query.

Instead of

query = "SELECT * FROM tblPersonnel WHERE LastName = " + strSearch + "";

try

query = "SELECT * FROM tblPersonnel WHERE LastName = '" + strSearch.Replace("'", "''") + "'";

Notice the extra single quotes, and also the Replace() statement which correctly escapes single quotes contained in your filter (e.g. apostrophes)

Must reiterate that your sample code does contain multiple problems and security vulnerabilities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文