Bulk update column in Data Table based on another column - c#

I have a data table that contains dates in a format of yyyymmdd. I would like to add a datetime column to the table that contains the date value of that column.
Now the problem is that the table contains a lot of records and looping through all the records takes some time. Are there a way to do a bulk update or a use the Expression as from this post?
Something like:
table.Columns.Add("MyDate", typeof(DateTime)).Expression =
DateTime.ParseExact("'strDateField'", "yyyymmdd", new CultureInfo("en-US"));
(Statement above does not compile which is expected, but only posted as to provide an example of what I'm trying to achieve)

Expression use a limit set of SQL functions exposed by .net for operations on data table columns, refer https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.110).aspx
You can try an arrangement like below
dt.Columns.Add("MyDate", GetType(Date)).Expression = "SUBSTRING(strDateField,5,2)+'/'+SUBSTRING(strDateField,7,2)+'/'+SUBSTRING(strDateField,1,4)";

Related

How do I insert into a datetime column minus the seconds?

So I'm working on a c# project that creates row in a table with a datetime column and then extract that auto generated ID of that column afterwards. My way of extracting the ID is through the code below the problem is the length it takes to execute the first query and then the second query is more than 1 second so I end up getting empty rows. So how do I insert or select rows minus the seconds?
INSERT INTO transactionlog(transactionDate) VALUES(NOW())
AND THEN IMMEDIATELY THIS
SELECT transactionID,transactionDate FROM transactionlog WHERE transactionDate=NOW();
NOW() gives you the date/time when the statement it contains started. While it is guaranteed that several invokations in the same query return the same value, this is not true accross queries.
To get the auto-incremented value generated during an insert, you can use LAST_INSERT_ID() instead:
INSERT INTO transactionlog(transactionDate) VALUES(NOW());
SELECT LAST_INSERT_ID();
You can utilize mysql DATE_FORMAT() function to control to format.
SELECT transactionID,transactionDate FROM transactionlog WHERE transactionDate=DATE_FORMAT(NOW(), '%c/%e/%Y %H:%i');
You can have a look here at: https://www.w3resource.com/mysql/date-and-time-functions/mysql-date_format-function.php
if you want to play with the format specifier

How do I automatically get date when I create a new row in my SQL table?

I am doing a project in school, I have to create a website tool for salesmen to fill what they have done during the day, i.e. amount of quotes, quote sum, orders, order sum etc. I am using Visual Studio 2010, ASP.NET with C# with a SQL database.
I have to create a table with different columns, that I know how. But what I need is to have a column called Date and it has the datatype date. I need it to be filled automatically without having to input it manually. The same date that the new information was added. I have searched for solution in google and other places but I think I am searching with the wrong keywords, hopefully you can help me.
The format I wish for the date to be is DD-MM-YYYY
When you look for SQL default date on Google, the second result you get is this one.
In there, you have a default date example:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
OrderDate date DEFAULT GETDATE()
)
using the DEFAULT keyword.
Create a sql datetime column in the database, and specify a default value of GetDate() or GetUtcDate() depending on which you want. Format is irrelevant on the input side; you will have to use a formatter on the select side (or in your c# code).
You can set the default value for the column as current date time..
create table tblname (
fieldname datetime default getdate()
)
Also see this question
Add default value of datetime field in SQL Server to a timestamp
You can use one of this to insert in the table instead.
String s = System.DateTime.Now.ToString("dd.MM.yyyy");
DateTime now = System.DateTime.Now;
The second one would be your choice because the type specified in yur table is Date.
If don't want to be setting it from the app, specify which database you are using to get a specific answer.

How can i only show the new or updated rows in a table in c# using sql?

Is there a way to only load the new or updated (edited) records in a table using Sql in C#?
Suppose i have a table which consists of 1000 student records.I want to show this information in a gridview ( this is just an example ) and upon adding or editing new entries,I want the gridview to get updated and show the latest changes to the table.If i go and have a
select *
statement ,This will definitely kill the application performance,So i thought its a good idea to try to add only those new records to the previously loaded records to the gridview.
But I'm clueless on the How to do such a task.
There is no other solution then simply architect you database.
Just one of many possible solutions: for every inserted or updated record in database you can have a timestamp. Having that information in database you can:
a) get the latest, from timestamp point of view, record in your DataGrid
b) get from darabase all reoirds that have timestamp bigger then it.
I repeat, this us just one of many possible solutions. What fits your requirements has to be decided by yourself.
Create a timestamp column and store the "last updated" date. Within your application, store the last time the data was retrieved from the database. Then, you can select only the records that have changed (or been added) since then.
The simplest way would be add a new field in the table such as ModifyDate of type datetime, and on insertion and updation of record put the current datetime in these field.
In that case you can select a query on the basis of this field as follows
Select Top 10 * from Table1 OrderBy ModifyDate desc

is it required to pass timestamp value in entitymodel to insert a data into sql table

I am working on ado.net entity model and I want to insert data into sql table.
SQL table structure is shown below.
ID int,
Name varchar(20),
EventTime Timestamp
I am trying to insert data into SQL Table using entitymodel but data are not inserted in the table.
My passing arguments are (1,"Test") it's not working.
I figured out that when I pass (1, "test", new Datetime()) then it works.
Can anyone let me know reason for that?
I understand that Timestamps value are automatically inserted by SQL server.
The "timestamp" data type gives you a binary value that automatically gets updated every time your field changes, but it won't give you a nice date/time value.
Update:
as per MSDN: timestamp Is a data type that exposes automatically generated, unique binary numbers within a database. timestamp is generally used as a mechanism for version-stamping table rows. The storage size is 8 bytes. The timestamp data type is just an incrementing number and does not preserve a date or a time.
To record a datetime when the record has been inserted, use a datetime data type. You might add a column of type datetime to your table and set getDAte() to generate the datetime.

SQL Server and c# .net - split large table into two and bulk copying

I will be having a table in SQL Server 2008 that will hold millions of rows and the initial design will be:
Code nvarchar(50) PK
Available bit
validUntil DateTime
ImportID int
The users can import 100,000 odd codes at a time which I will be using sqlbulkcopy to insert the data. They can also request batches of codes of up to 10,000 at a time for a specific ImportID and as long as the request date is less than the ValidUntil date and the code is available.
My question is, will it be better to hold all these codes in the one table and use indexes or to split the one table into two - AvailableCodes and UsedCodes - so whenever codes are requested, they are moved from the AvailableCodes table into the UsedCodes table instead of having the Available flag? That way the AvailableCodes table won't get as massive as a single table as over time there will be more used codes than available codes and I will not be that bothered about them accept for auditing purposes.
Also, if the tables are split will I still be able to use the sqlbulkcopy as the codes will still need to be unique across both tables?
I would create it in one table and create well defined indexes.
Consider a filter index for the flag column. This is done with a where clause in t-sql and the filter page in ssms.
http://msdn.microsoft.com/en-us/library/cc280372.aspx

Categories

Resources