SQL语句在C#中无法执行?

发布于 2024-07-26 22:24:25 字数 1132 浏览 10 评论 0原文

嘿伙计们,这是我的代码,我从 VS 得到的唯一帮助是 INSERT INTO 语句语法不正确?

我已经检查了所有代码,但看不出哪里出了问题,有人可以帮忙吗?

        public void New(string ApplicationStartupPath, string FileName, string Department, string Month, string Year)
    {
        string sql = "INSERT INTO PodcastsDir (FileName, Department, Month, Year) VALUES (@FileName, @Department, @Month, @Year)";
        using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + ApplicationStartupPath.ToString() + ""))
        using (OleDbCommand cmd = new OleDbCommand(sql, conn))
        {
            cmd.Parameters.Add("@FileName", OleDbType.VarChar);
            cmd.Parameters.Add("@Department", OleDbType.VarChar);
            cmd.Parameters.Add("@Month", OleDbType.VarChar);
            cmd.Parameters.Add("@Year", OleDbType.VarChar);

            conn.Open();

            cmd.Parameters[0].Value = FileName;
            cmd.Parameters[1].Value = Department;
            cmd.Parameters[2].Value = Month;
            cmd.Parameters[3].Value = Year;

            cmd.ExecuteNonQuery();
        }

    }

谢谢阿什

Hey Guys, here is my code for this, the only help i get from VS is that the INSERT INTO statement syntax is incorrect?

I have gone through all of the code and just cannot see where i have gone wrong, can someone gimme a hand please?

        public void New(string ApplicationStartupPath, string FileName, string Department, string Month, string Year)
    {
        string sql = "INSERT INTO PodcastsDir (FileName, Department, Month, Year) VALUES (@FileName, @Department, @Month, @Year)";
        using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + ApplicationStartupPath.ToString() + ""))
        using (OleDbCommand cmd = new OleDbCommand(sql, conn))
        {
            cmd.Parameters.Add("@FileName", OleDbType.VarChar);
            cmd.Parameters.Add("@Department", OleDbType.VarChar);
            cmd.Parameters.Add("@Month", OleDbType.VarChar);
            cmd.Parameters.Add("@Year", OleDbType.VarChar);

            conn.Open();

            cmd.Parameters[0].Value = FileName;
            cmd.Parameters[1].Value = Department;
            cmd.Parameters[2].Value = Month;
            cmd.Parameters[3].Value = Year;

            cmd.ExecuteNonQuery();
        }

    }

Thanks Ash

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

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

发布评论

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

评论(4

懒猫 2024-08-02 22:24:25

您似乎正在使用 Access - 月份和年份不是保留字吗? 尝试将它们括在方括号中(这是 Access 的正确分隔符吗?),然后重试。

You seem to be using Access - aren't Month and Year reserved words? Try enclosing them in square brackets (is this the correct delimiter for Access?) and trying again.

嘿看小鸭子会跑 2024-08-02 22:24:25

如果您使用的是 Access,则参数实际上不是占位符,而是命名参数。

更改您的 SQL 字符串以使用 ? 作为占位符而不是命名参数,并确保按照与 ? 相同的顺序添加参数。 出现在 SQL 字符串中。

If you are using Access aren't the parameters actually place holders rather than named parameters.

Change your SQL string to use ? as a placeholder rather than a named parameter and ensure you add the parameters in the same sequence as the ? appear in the SQL string.

这个俗人 2024-08-02 22:24:25

AFAIK,Access 不支持命名参数。 你应该使用“?” 在查询中指定参数:

    string sql = "INSERT INTO PodcastsDir (FileName, Department, [Month], [Year]) VALUES (?, ?,?,?)";

您还应该将放在方括号中。

其余代码可以保持不变。

AFAIK, Access doesn't support named parameters. You should use "?" in your query to specify parameters:

    string sql = "INSERT INTO PodcastsDir (FileName, Department, [Month], [Year]) VALUES (?, ?,?,?)";

You should also put Year and Month in square brackets.

The rest of your code can be left unchanged.

雨后咖啡店 2024-08-02 22:24:25

您可能想尝试一下,Jet ie Access 中的参数很好

"INSERT INTO PodcastsDir ([FileName], [Department], [Month], [Year]) VALUES (@FileName, @Department, @Month, @Year)"; 

You might want to try this, parameters are fine in Jet ie Access

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