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.
In my SQL Server, the datatype is Date, and in my insert query
cmd.Parameters.AddWithValue("#Date", Convert.ToDateTime(row.Cells("Date").Value))
vb format , sql format
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. 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. 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. You might do that manually or let a data adapter do it for you when you call Fill. Bind that DataTable to your grid and configure the grid column to display the dates however is appropriate for the application. Save any and all changes made to the grid by calling Update on the same data adapter. 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. 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.
Related
I have a nvarchar(50) column in SQL which is of format mm/dd/yyyy. I am trying to import this data in excel using SSIS. In ssis package, I am creating a column Date_1 with datatype Date in Execute SQL task (Since .xlsx file needs to be created dynamically) and in my data flow task I am converting Date_1 to date(DT_DATE). But after data migration when I check my file column Date_1 is of General data type and not Date. Can someone help me how to convert General column to Date
The drop down list you are showing in the Microsoft Excel interface is not related to the data type it is the Number Format property which is used to change the way the value is shown in Excel.
Available number formats
To change this property you need to use Microsoft.Interop.Excel library within a Script Task and change the Excel.Range.NumberFormat property. As example:
Range rg = (Excel.Range)xlWorksheet.Cells[1,1];
rg.EntireColumn.NumberFormat = "MM/dd/yyyy";
References
How to make correct date format when writing data to Excel
How to: Specify Number or Date Format for Cell Content
Our business would be providing us a .csv file. One of the columns in the file would be in date format. Now as we know there are many date formats in Excel. The problem is that we need to check whether the date provided is a correct date. It could be in any format like ddmmyyyy, yyyymmdd, dd-mon-yyyy etc basically any format that Excel supports.
We are planning to first load the data in a staging area and the date field would be defined as varchar so that it can accept any data.
Now either using SSIS or via T-SQL, I need to check whether the date provided is actually a date and if it is I need to load it into a different table in YYYYMMDD format.
How do I go about doing the above?
Considering you have your excel data already loaded into a SQL Server table as varchar (you can easily do this using SSIS), something like this would work:
SELECT
case when ISDATE(YOUR_DATE) = 1 then CONVERT(int,YOUR_DATE,112) else null end as MyDate
FROM
YOUR_TABLE
I don't have access to a SQL Server instance at the moment and can't test the code above, so you may need to adapt to your needs, but this is the general idea.
You can also do further research on ISDATE and CONVERT functions in SQL Server. You should be able to achieve what you need combining them together.
I want to copy the data from Excel to SQL Server using wizard. However, the data is not same when I import to SQL Server. Can someone help me how to solve it?
So by the comments, the problem is that after you changed the excel data type to date, it still gets exported as datetime?
Sounds like what you changed was how the data was displayed in excel, but the real data is still stored in datetime, which then gets moved as such as you export it. You can check this by clicking on a cell with the data, and looking at the white bar on top of page. That should show you whatever data or formula is actually in that cell. If it's as I suspect, then this is intended working behaviour.
The good news is that:
Storing datetime data as date in SQL Server, gets converted automatically. No manual labor required.
You can for instance add a new field in excel, use SUBSTR function to capture the DATE part only from the datetime field, and use that in place of the actual datetime during export.
If you export the data into what now becomes a datetime column instead of a date column, into some kind of a temporary table in SQL Server, you could simply ALTER the column type to date as part of the process.
Take your pick. :)
I'm trying to use SqlBulkCopy.WriteToServer to insert data in a certain table of an SQL database. To do so, I have a DataTable which is populated with records I need to save on the database. Basically my code is based on the Microsoft's example provided for the method SqlBulkCopy.WriteToServer.
The problem is that I have two DateTime fields in the SQL table, and i don't know how to represent them while defining the columns of the DataTable. I tryed both System.String and System.DateTime, but after the code has been executed, he says he can not convert a String type to DateTime. DataTable columns are defined in the following way (code taken from the example linked above):
Dim productID As DataColumn = New DataColumn()
productID.DataType = System.Type.GetType("System.Int32")
How can i do that? What is the correct type to use for a DataTable's column corresponding to an SQL DateTime field?
Previously, I used an SQL command to map every field, for example:
' Fields initialization
SqlCmd.Parameters.Add("#Field1", SqlDbType.DateTime)
[...]
SqlCmd.Parameters.Add("#FieldN", SqlDbType.NChar, 255)
' After opened the transaction
SqlCmd.Parameters("#Field1").Value = MyDateTimeSavedInAString
[...]
SqlCmd.Parameters("#FieldN").Value = "NTHVALUE"
Thanks in advance.
[UPDATE1] The DateTime column now works, but the same kind of error is given by another column which will be saved on a time field in the SQL Server's table. What kind of VB .NET type i should use to map the DataTable column with the one of SQL Server marked as time?
[UPDATE2] I'm trying to use an SQL table with every field set to nvarchar data type, but it still gives the same error. In fact he says that it is impossible to convert the String type of the origin column in the nvarchar type of the destination column.
Use DateTime - DateTime conversion works like a charm. SqlBulkCopy does not want a lot of modifications in the data - it bypasses most of SQL Server's processing for raw performance.
And you can avoid using a DataTable - it takes about an hour or two to write your own object wrapper ;) DataTables are not exactly efficient.
And try to wrap it up more- SqlBUlkCopy is terrible code in that it puts an exclusive lock on the target table. I have my own wrapper creating a temporary table, bulk copying into this and then using a simple SELECT INTO to move the data to the final table in a short atomic operation.
And be aware - below around 1000 lines it makes no sense to use SqlBulkCopy. High overhead. Rather create a long multi line insert statement.
I'm importing dates from a German SQL Server table into a German Excel file via the built-in Excel connection tool.
However the date format is just like in the SQL Server: 2012-08-08 but I want to display: 08.08.2012. When I double-click inside a cell it will recognize the German date formatting but of course I would like to have that format for the entire column without having to manually change it.
I also need to be able to use these dates for calculations.
Do I need to change something in SQL Server or how do I make this work?
Thank you.
The problem is that Excel does not recognise the SQL Server Date type. Cast the date to a Smalldatetime or Datetime, then import and format.
As long as it's recognized ad a date, you can set the formatting of the entire column to a custom one and set the value of the custom format to dd.mm.yyyy (or mm.dd.yyyy, depending on which you want).
Not a programming answer, but it will get you the display you want just by setting the custom format of the whole column.