从日期时间到字符串格式的转换错误

康纳凯西

这部分代码有问题,我正在尝试在我的预订表中插入一条记录。我试图输入的值是 (6, 3, 3, 20/06/2018 00:00:00, 400, 2800.00, True, 560.00)

        public void insertBooking(int bookingID, int customerID, int entertainmentID, 
                                  DateTime bookingDate, int numberOfGuests, double price, 
                                  bool deposit, decimal depositPrice)
        {
            db.Cmd = db.Conn.CreateCommand();
            db.Cmd.CommandText = "INSERT INTO Booking (bookingID, customerID, entertainmentID, 
                                  [Booking Date], [Number Of Guests], [Price], [Deposit?], 
                                  [Deposit Price]) " + "Values ('" + bookingID + "','" + 
                                  customerID + "','" + entertainmentID + "','" + 
                                  bookingDate + "','" + numberOfGuests + "','" + price + 
                                  "','" + deposit + "','" + depositPrice + "')";
            db.Cmd.ExecuteNonQuery();
        }

我得到的错误如下,

“从字符串转换日期和/或时间时转换失败。”

我试图尽我所能研究这个问题,但我不知道如何解决这个问题。任何帮助表示赞赏。

索纳贡努尔

好的,您的代码有一些问题。我将尝试按顺序解释所有这些。

首先,如果您使用DateTime带有字符串连接的值(在+运算符上),.ToString方法会自动为它们调用,并且您可能会或可能不会为您的数据库列生成“正确”的文本。千万不能选择错误的数据类型的列您需要直接传递您的DateTime如果您不知道您知道哪个SqlDbType用于您的值,您也可以阅读映射 CLR 参数数据

正如评论中所建议的,解决这种情况的最佳方法是使用参数化查询另一方面,这些字符串连接对SQL 注入攻击是开放的

还可以使用usingstatement来自动处理您的连接(我们没有看到但......)和命令,而不是手动调用.Dispose方法(您没有)。

举个例子;

public void insertBooking(int bookingID, int customerID, int entertainmentID,
                          DateTime bookingDate, int numberOfGuests, double price,
                          bool deposit, decimal depositPrice)
{
    using (var con = new SqlConnection(yourConnectionString))
    using (var cmd = con.CreateCommand())
    {
        cmd.CommandText = @"INSERT INTO Booking (bookingID, customerID, entertainmentID, 
                            [Booking Date], [Number Of Guests], [Price], [Deposit?], 
                            [Deposit Price]) Values(@bookId, @cusId, @entId, @bookdate, @guests, @price, @deposit, @depositPrice)";
        cmd.Parameters.Add("@bookId", SqlDbType.Int).Value = bookingID;
        cmd.Parameters.Add("@cusId", SqlDbType.Int).Value = customerID;
        cmd.Parameters.Add("@entId", SqlDbType.Int).Value = entertainmentID;
        cmd.Parameters.Add("@bookdate", SqlDbType.DateTime).Value = bookingDate;
        cmd.Parameters.Add("@guests", SqlDbType.Int).Value = numberOfGuests;
        cmd.Parameters.Add("@price", SqlDbType.Decimal).Value = price;
        cmd.Parameters.Add("@deposit", SqlDbType.Bit).Value = deposit;
        cmd.Parameters.Add("@depositPrice", SqlDbType.Decimal).Value = depositPrice;

        con.Open();
        cmd.ExecuteNonQuery();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将“ YYYYMMDDHHMMSS”格式的字符串转换为日期时间

如何在pyspark中将日期时间从字符串格式转换为日期时间格式?

无效的日期时间格式:1366错误的字符串值

PowerShell:奇怪的日期格式字符串到日期时间

熊猫至今的字符串到日期时间格式

将字符串转换为日期时间格式

Zulu格式的时间戳字符串到日期时间

熊猫时差(从字符串格式到日期时间)

DatePicker的字符串到日期时间的转换问题

日期时间到字符串的转换

将GMT格式的字符串转换为日期时间

字符串中的ValueError到日期时间的转换

Ruby字符串到日期时间格式

字符串到当前日期时间格式

SQL从字符串错误到日期和/或时间的转换

Tableau 字符串到日期时间格式问题

将字符串转换为日期格式:DD-MMM-YYYY 到 Python 中的日期时间

格式为 yyyyMMddhhmmss 的 Java 字符串到日期转换错误

php字符串日期到日期时间格式

转换时间格式,字符串到浮点数

strptime 字符串到日期时间对象的转换

pandas 字符串到日期类型的正确格式转换

将字符串转换为不同的日期时间格式

您如何将格式错误的字符串转换为日期时间

将日期(字符串)转换为日期时间格式

将日期字符串转换为日期时间 Python 格式错误

未知的字符串格式。转换日期时间python

格式不匹配错误-无法将字符串数据转换为日期时间格式

累积时间字符串到熊猫日期时间格式