C# 插入日期时间失败

发布于 2024-11-19 12:09:44 字数 829 浏览 9 评论 0原文

我有一个数据库,其中有一个名为 Fio_FinalSched 的表,该表又包含一个名为 FinalDate 的列,其类型为smalldatetime。我通过以下方式将日期字符串转换为 DateTime 类型:

DateTime theDate = Convert.ToDateTime("2010-01-05 23:50:00");

然后,通过以下方式创建命令字符串:

string testCommand = "INSERT INTO Fio_FinalSched (FinalDate) Values ("+theDate+")";

然后,将其转换为 SQL 命令:

SqlCommand myCommand = new SqlCommand(testCommand,conn);

其中 conn 是数据库连接。最后,我执行命令:

myCommand.ExecuteNonQuery();

当我运行这个命令时,它到达执行行,然后给我错误:

Incorrect syntax near '11'.

我尝试了多种方式更改日期字符串的格式,但无济于事。是否因为我的数据库需要 smalldatetime 类型而不是 datetime 类型? C# 似乎没有我可以使用的 smalldatetime 类型。任何见解将不胜感激!

I've got a database with a table called Fio_FinalSched which in turn has a column named FinalDate with type smalldatetime. I convert the date string to a DateTime type by:

DateTime theDate = Convert.ToDateTime("2010-01-05 23:50:00");

I then create the command string by:

string testCommand = "INSERT INTO Fio_FinalSched (FinalDate) Values ("+theDate+")";

Then, turn it into an SQL command:

SqlCommand myCommand = new SqlCommand(testCommand,conn);

where conn is the db connection. Finally, I execute the command:

myCommand.ExecuteNonQuery();

When I run this, it gets to the execution line, and then give me the error:

Incorrect syntax near '11'.

I've tried altering the format of my date string several ways, to no avail. Is it because my database is wanting a smalldatetime type and not a datetime type? C# doesn't seem to have a smalldatetime type that I can use. Any insight would be appreciated!

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

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

发布评论

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

评论(6

沙沙粒小 2024-11-26 12:09:44

您需要使用单引号将日期括在 SQL 字符串文字中...

string testCommand = "INSERT INTO Fio_FinalSched (FinalDate) Values ('"+theDate+"')";

或使用参数..

string testCommand = "INSERT INTO Fio_FinalSched (FinalDate) Values (@myDate)";
cmd.Parameters.Add(new SqlParameter("@myDate", theDate));

You need to wrap the date in a SQL string literal using single quotes...

string testCommand = "INSERT INTO Fio_FinalSched (FinalDate) Values ('"+theDate+"')";

Or use Parameters..

string testCommand = "INSERT INTO Fio_FinalSched (FinalDate) Values (@myDate)";
cmd.Parameters.Add(new SqlParameter("@myDate", theDate));
孤城病女 2024-11-26 12:09:44

尝试在字符串中的日期两边加上单引号...

string testCommand = "INSERT INTO Fio_FinalSched (FinalDate) Values ('"+theDate+"')";

Try putting single quotes around the date in the string...

string testCommand = "INSERT INTO Fio_FinalSched (FinalDate) Values ('"+theDate+"')";
明天过后 2024-11-26 12:09:44

使用 DateTime 类型和数据库中的其他几个类型,强烈建议使用 @Parameters 以及所有转换(如果需要),例如:时区、格式或在客户端执行的任何操作。

还因为,正如您在帖子中提到的,该列是 smalldatetime 类型,而不是字符串。

简而言之,尝试使用@Parameters,你很可能会成功。

问候。

Working with DateTime type and several others in DataBase its strongly recomended using @Parameters, and all convertions, if you need, like: timezone, formatting or whatever do on client side.

Also because, as you mantioned in your post, the column is of smalldatetime type and not string.

In short try to use @Parameters and most likely you will succeed.

Regards.

饭团 2024-11-26 12:09:44

首先在日期两边加上单引号。

例如

('"+theDate+"')";

这通常对于 ISO 来说就足够了。您可能需要研究转换器函数。

请注意 theDate 信息的来源。如果它来自可编辑字段(等),则这是 SQL 注入攻击的候选者。

但是,如果转换为 dateTime,然后粘贴到 SQL 字符串中,这不是一层编码吗?

start by putting single quotes around the date.

e.g.

('"+theDate+"')";

that's usually enough for ISO. You might have to look into converter functions.

Be careful of where the info comes from for theDate. If it comes from an editable field (etc.), then this is a candidate for SQL injection attacks.

However, if you convert to dateTime, and then paste into a SQL string, isn't that a layer of encoding?

一念一轮回 2024-11-26 12:09:44

我建议使用命名参数:

string testCommand = "INSERT INTO Fio_FinalSched (FinalDate) Values (@MYDATE)";
SqlCommand myCommand = new SqlCommand(testCommand,conn);
myCommand.Parameters.Add(new SqlParameter("@MYDATE", SqlDbType.DateTime)).Value = theDate;
myCommand.ExecuteNonQuery();

I'd recommend using named parameters:

string testCommand = "INSERT INTO Fio_FinalSched (FinalDate) Values (@MYDATE)";
SqlCommand myCommand = new SqlCommand(testCommand,conn);
myCommand.Parameters.Add(new SqlParameter("@MYDATE", SqlDbType.DateTime)).Value = theDate;
myCommand.ExecuteNonQuery();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文