简体   繁体   English

VB.NET不同格式的日期插入到SQL Server

[英]VB.NET different format date insert into SQL Server

I want to insert data into SQL Server, but I have different format in datagridview data from is "dd/mm/yyyy" and after I see in my SQL Server format is "yyyy/mm/dd" that why I can't use where for update data. 我想将数据插入SQL Server,但是datagridview中的数据格式与“ dd / mm / yyyy”不同,在我的SQL Server中看到的格式为“ yyyy / mm / dd”后,为什么不能使用在哪里更新数据。

In my SQL Server, the datatype is Date , and in my insert query 在我的SQL Server中,数据类型为Date ,在我的插入查询中

 cmd.Parameters.AddWithValue("@Date", Convert.ToDateTime(row.Cells("Date").Value))

vb format , sql format vb格式sql格式

Format is irrelevant. 格式无关紧要。 Format is only an issue when converting a DateTime to a String for display or when converting text input to a DateTime . 一个转换时,格式只能是一个问题DateTimeString进行显示或文本输入转换为当DateTime You should be storing binary dates in your database and binary dates are just numbers and have no format. 您应该将二进制日期存储在数据库中,二进制日期只是数字,没有格式。 You should also be storing binary DateTime values in your grid. 您还应该在网格中存储二进制DateTime值。 How the grid displays them is irrelevant to how they're stored. 网格如何显示它们与它们的存储方式无关。

Here's what you should be doing. 这就是你应该做的。 Create a DataTable with a column for DateTime values, not String values. 创建一个带有DateTime值而不是String值的列的DataTable You might do that manually or let a data adapter do it for you when you call Fill . 您可以手动执行此操作,也可以在调用Fill时让数据适配器为您完成此操作。 Bind that DataTable to your grid and configure the grid column to display the dates however is appropriate for the application. 将该DataTable绑定到您的网格并配置网格列以显示日期,但是它适用于该应用程序。 Save any and all changes made to the grid by calling Update on the same data adapter. 通过在同一数据适配器上调用Update ,保存对网格所做的所有更改。 You can use a command builder to generate the action commands or you can create them yourself. 您可以使用命令构建器来生成操作命令,也可以自己创建它们。 Either way, you don't populate the parameters yourself but the data adapter draws the values from the DataTable . 无论哪种方式,您都不会自己填充参数,而是数据适配器从DataTable绘制值。 If you're creating the commands manually, creating a parameter would look something like this: 如果您是手动创建命令,则创建参数将如下所示:

myDataAdapter.InsertCommand.Parameters.Add("@Date", SqlDbType.Date, 0, "Date")

That tells the data adapter to get the values for the "@Date" parameter from the "Date" column of the DataTable . 这告诉数据适配器从DataTable的“ Date”列中获取“ @Date”参数的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM