Question Dynamic table insert issue with dates

Srini

Member
Joined
Nov 15, 2022
Messages
6
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
 
Moving out of VS.NET General discussions since this is actually a database access issue.
 
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.
 
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.
 
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?
 
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.
 
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..
 
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: 11
Back
Top Bottom