C#,SQLreader/命令出现问题

发布于 2024-12-04 04:18:12 字数 756 浏览 1 评论 0原文

我在使用 SqlDataReader 时遇到了一些问题:

public string GetVareNavn(string streg)
    {
        string navn = "";
        SqlConnection myCon = DBcon.getInstance().conn();

        string query =
            "SELECT Navn FROM Vare WHERE Stregkode = ) Values('" + streg + "')";

        myCon.Open();
        SqlCommand com = new SqlCommand(query, myCon);
        Console.WriteLine("navn: "+navn);
        SqlDataReader dr = com.ExecuteReader();
        if (dr.Read())
        {
            navn = dr.GetString(1);
        }
        myCon.Close();
        return navn;
    }

它在 com.ExecutiveReader(); 处抛出异常,异常是:

“)”附近的语法不正确。

我不知道为什么这个现在不起作用,因为我已经在另一个项目中使用了它。

I have some trouble with the SqlDataReader:

public string GetVareNavn(string streg)
    {
        string navn = "";
        SqlConnection myCon = DBcon.getInstance().conn();

        string query =
            "SELECT Navn FROM Vare WHERE Stregkode = ) Values('" + streg + "')";

        myCon.Open();
        SqlCommand com = new SqlCommand(query, myCon);
        Console.WriteLine("navn: "+navn);
        SqlDataReader dr = com.ExecuteReader();
        if (dr.Read())
        {
            navn = dr.GetString(1);
        }
        myCon.Close();
        return navn;
    }

It throws an exception at com.ExecutiveReader(); and the exception is:

Incorrect syntax near ')'.

I don't know why this one doesn't work right now, because I've used it in another project.

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

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

发布评论

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

评论(3

苦行僧 2024-12-11 04:18:12

您的查询看起来像是从曾经是 INSERT 语句的内容复制而来;您不需要在语句末尾添加 VALUES... 子句。尝试将您的查询更改为:

string query =
    "SELECT Navn FROM Vare WHERE Stregkode = @streg";

然后修改此代码以使用参数:

SqlCommand com = new SqlCommand(query, myCon);
com.Parameters.AddWithValue("@streg", streg);

Your query looks like it was copied from something that used to be an INSERT statement; you don't need the VALUES... clause at the end of the statement. Try changing your query to:

string query =
    "SELECT Navn FROM Vare WHERE Stregkode = @streg";

Then modify this code to use the parameter:

SqlCommand com = new SqlCommand(query, myCon);
com.Parameters.AddWithValue("@streg", streg);
故事灯 2024-12-11 04:18:12

它不起作用,因为您的 SQL 已损坏:

SELECT Navn FROM Vare WHERE Stregkode = ) Values('" + streg + "')"

您希望 WHERE 子句做什么,以及您尝试使用什么值?看起来您的更新命令的复制/粘贴已损坏。

此外,无论如何,您都不应该像这样将值放入 SQL 中 - 您应该使用参数化查询来避免 SQL 注入攻击(并避免格式问题等)。

It doesn't work because your SQL is broken:

SELECT Navn FROM Vare WHERE Stregkode = ) Values('" + streg + "')"

What did you expect that WHERE clause to do, and what values are you trying to use? It looks like you've got a broken copy/paste from an update command.

Additionally, you shouldn't put values into your SQL like that anyway - you should use parameterized queries to avoid SQL injection attacks (and to avoid formatting issues etc).

╰沐子 2024-12-11 04:18:12

是的,它肯定会给。为什么将 Values 放入 select 查询中?这是错误的语法,请立即尝试。

string query = "SELECT Navn FROM Vare WHERE Stregkode = '" + streg + "'";

Ya, surely it will give. Why you put the Values in your select query? which is wrong syntax, Try Now.

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