Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear all,
There are 5 columns with smalldatetime datatype and others columns too in my Table named tbltest. And I got the problem very interesting when I am inserting DateTime.
The name of my column fields are Dated, StartTime, EndTime, OnAirStartTime, OnAirEndTime.
And there is not any problem in my insert operation code:
C#
db.Adapter("INSERT INTO tbltest (Dated,StartTime,EndTime,OnAirStartTime,OnAirEndTime,DurationStart,DurationOnAir,DeviceType,UnitName,VendorName,ActiveNetwork,SIM,ProfileUsed,Bitrate,ServerPort,CameraType,EventName,Division,Location,Stutus,Remarks,ReporterName,CameramanName,TechnicianName) VALUES ('" + Convert.ToDateTime(this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["Dated"].ToString()).ToString() + "',"
                        + "'" + Convert.ToDateTime(this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["StartTime"].ToString()).ToString() + "',"
                        + "'" + Convert.ToDateTime(this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["EndTime"].ToString()).ToString() + "',"
                        + "'" + Convert.ToDateTime(this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["OnAirStartTime"].ToString()).ToString() + "',"
                        + "'" + Convert.ToDateTime(this.mysql.dataSet.Tables["liveoperation7"].Rows[i][5].ToString()).ToString() + "',"
                        + "'" + Convert.ToInt32(this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["DurationStart"].ToString()) + "',"
                        //+ "'" + Convert.ToDateTime(this.mysql.dataSet.Tables["liveoperation7"].Rows[i][5].ToString()).ToString() + "',"
                        + "'" + Convert.ToInt32(this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["DurationOnAir"].ToString()) + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["DeviceType"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["UnitName"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["VendorName"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["ActiveNetwork"].ToString() + "',"
                        + "'" + Convert.ToInt32(this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["SIM"].ToString()) + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["ProfileUsed"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["Bitrate"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["ServerPort"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["CameraType"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["EventName"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["Division"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["Location"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["Stutus"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["Remarks"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["ReporterName"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["CameramanName"].ToString() + "',"
                        + "'" + this.mysql.dataSet.Tables["liveoperation7"].Rows[i]["TechnicianName"].ToString() + "')");


Passing 5 datetime values are:

Dated = 2016-10-05 00:00:00
StartTime = 2016-10-05 13:53:00
EndTime = 2016-10-05 13:55:00
OnAirStartTime = 2016-10-05 13:53:00
OnAirEndTime = 10/5/2016 13:54:30

>But, there is inserting in OnAirEndTime filed, the value of EndTime field.
Is there any problem the matching fields name ?
Or is there any limitation in MSSQL 2005 that there should not be more than 4 fields with smalldatetime datatypes ?

What I have tried:

I have just change a OnAirEndTime datatype as varchar than its inserting exact value. But, why should I change datatype ?

Please help me to continuing smalldatatime as a datatype how much I need in a table.

Thank you.
Posted
Updated 5-Oct-16 6:35am
Comments
[no name] 5-Oct-16 12:21pm    
Using a proper parameterized query might help you track your error down.
Dave Kreskowiak 5-Oct-16 12:36pm    
Your code is a MASSIVE security problem as it's susceptible to SQL Injection attacks. Convert this to a parameterized query instead of the string concatenation garbage you have now.

It'll also let you have far more control over how your data is passed to the server and fix your problem at the exact same time.

1 solution

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
In this specific code it's not dangerous - but it's symptomatic that the rest of your code is: so go and check and change it all, before some user does it for you by typing in a text box...
And then pass the DateTime values directly to SQL as parameters and your problem will almost certainly disappear - because at the moment the value you send to SQL are formatted in the locale for the machine running that code, which is unlikely to be ISO format yyyy-MM-dd, and more likely to be MM-dd-yyyy or dd-MM-yyyy, and that causes it's own set of problems.
It's also be a damn sight easier to read as well...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900