Question Dynamic table insert issue with dates

vijaygoturu

New member
Joined
Nov 15, 2022
Messages
4
Programming Experience
Beginner
Hi,

I am trying to insert into sql from Dynamic object ,
But insert failed with date type values,

Can some one suggest how can I handle this situation please.

command.CommandText = "INSERT INTO table VALUES (" + "'" + string.Join("',' ", ((IDictionary<string, Object>)rec).Values) + "')";

1668705652322.png
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,561
Location
Chesapeake, VA
Programming Experience
10+
Moving out of VS.NET General discussions since this is actually a database access issue.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,561
Location
Chesapeake, VA
Programming Experience
10+
Depending on what your backend database is, you'll need to properly format the date time value a particular way, or use a SQL function. Since I'm anti-SQL, I've pretty much exhausted my SQL knowledge with that previous statement. Hopefully someone with more SQL expertise and jump in on this thread.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,561
Location
Chesapeake, VA
Programming Experience
10+
As an aside, the code you have there in post #1 seems to be a SQL injection exploit waiting to happen, but if you trust your inputs, then more power to you.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,880
Location
Sydney, Australia
Programming Experience
10+
It's very frustrating when people ask for help with SQL code and then don't show us the SQLK code. The C# code that builds your SQL is not the SQL code. Have you even looked at the SQL code yourself? You also haven't told us what data types the columns you're inserting to are. Have you done anything to determine what value(s) is the problem?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,880
Location
Sydney, Australia
Programming Experience
10+
Even ignoring the potential for SQL injection here, you would be better off using parameters rather than trying to build the values into a string. That way, as long as the data types of the values and the data types of the columns are compatible, it will just work and you won't have to worry about format, which is only relevant when turning values that aren't strings into strings. You would use the Keys to build your SQL, then loop through the dictionary and add the parameters.
 

cjard

Well-known member
Joined
Jan 25, 2012
Messages
439
Programming Experience
10+
Why is this dynamic? Parsed from json or similar? Feels like there is a whole host of improvements that can be made to this process..
 

vijaygoturu

New member
Joined
Nov 15, 2022
Messages
4
Programming Experience
Beginner
Thanks for all your replies...First accept my apologies, As I am very basic/learner to c#.
We have a requirement to load to sql table from parquet file.
data can be up to 10gb....

With the help of google and this forum created console program.

Please see attached file for my program (please do not frustrate by seeing this junk program) ... this is working and inserting into table... but for to load 100 rows itself taking huge time.


If any have solution to handle these scenarios, please share or guide me correct direction.


Many thanks
 

Attachments

  • Parquet_To_Sql.txt
    4.4 KB · Views: 3
Top Bottom