SQL语句在C#中无法执行?
嘿伙计们,这是我的代码,我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您似乎正在使用 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.
如果您使用的是 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.
AFAIK,Access 不支持命名参数。 你应该使用“?” 在查询中指定参数:
您还应该将年和月放在方括号中。
其余代码可以保持不变。
AFAIK, Access doesn't support named parameters. You should use "?" in your query to specify parameters:
You should also put Year and Month in square brackets.
The rest of your code can be left unchanged.
您可能想尝试一下,Jet ie Access 中的参数很好
You might want to try this, parameters are fine in Jet ie Access