I am facing some problem in creating dynamic statement SQL insertion of a dataTable object in C #. . I it.Here piece of my code, I want to know the best practices for creating've tried so far
string sqlCommandInsert = "dbo.RAW_DATA (insert" string sqlCommandValue = ""; foreach (DataColumn DataColumn in dataTable.Columns) {sqlCommandInsert + = DataColumn + "";} sqlCommandInsert + = sqlCommandInsert.TrimEnd ( ','); sqlCommandInsert + = ") value (for" (I Int = 0 ; I & LT; dataTable.Rows.Count; i ++) {sqlCommandValue + = " '" + dataTable.Rows [i] .ItemArray [i] + ",";} var insertCommand = sqlCommandInsert; sqlCommandValue = sqlCommandValue.TrimEnd (','); Var command = lower command + sqlCommandValue + ")"; DataContext.Database.ExecuteSqlCommand (command);
Any suggestions would be appreciated :) Regards.
Use value
instead of VALUE
. Apart from that you should always use the SQL-parameter
Select the string column = string.Join (",", dataTable.Columns.Cast & lt; DataColumn & gt; () (c = & gt; c .name name)) ; String value = string.Join ( ",", dataTable.Columns.Cast & LT; DataColumn & gt ;. () Select c = & gt (; String.Format ( "@ {0}", c.ColumnName) )); String sqlCommandInsert = string.Format ("INSERT in dbo.RAW_DATA ({0}) value ({1})", column, value); Using (var con = using new SqlConnection ( "ConnectionString")) (var cmd = new SqlCommand (sqlCommandInsert, con)) {con.Open (); Foreign currency (Datatara line in Datatale.rao) {cmd.Parameters.Clear (); Foreign currency (DataTable call data in call column) CMD. Parameter. Advant Value ("@" + Col. Columnname, Line [Color]); Int inserted = cmd.ExecuteNonQuery (); }}
Comments
Post a Comment