因转换而无法插入日期

Mag*_*iam -2 c# sql sql-server

这是我的代码:

SqlCommand cmda = new SqlCommand(
                        "insert into EnteryItem (ItemName, ImageIndex, DateEntery, Index0, ItemBarcode)"
                        + "Values (@ItemName, GETDATE(), @ImageIndex, @Index0, @ItemBarcode)"
                        , cn);
cn.Open();
cmda.Parameters.AddWithValue("@ItemName", "");
//   cmda.Parameters.AddWithValue("@DateEntery", now);
cmda.Parameters.AddWithValue("@ImageIndex", "0");
cmda.Parameters.AddWithValue("@Index0", "");
cmda.Parameters.AddWithValue("@ItemBarcode", label12.Text);

cmda.ExecuteNonQuery();
cn.Close();
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我尝试了几种方法,但我得到了相同的结果

将nvarchar值'28/01/2014'转换为数据类型int时转换失败.

错误,有时

不允许从数据类型datetime到int的隐式转换.使用CONVERT函数运行此查询.

我需要以特定格式插入日期和时间

And*_*rei 6

看起来第二列和第三列的valus应该被交换.应该:

insert into EnteryItem (ItemName, ImageIndex, DateEntery, Index0, ItemBarcode)
Values (@ItemName, @ImageIndex, GETDATE(), @Index0, @ItemBarcode)
Run Code Online (Sandbox Code Playgroud)

另请注意@DateEntery,虽然您要将参数添加到参数列表中,但您并未使用参数.