I have an array that I created from StringBuilder:
string[] input; input = sb.ToString().Split(',');
Now I want to insert data from this "input" array into an SQL string? The first element in the array must be an integer. How can I implement this in the fastest way?
Thanks in advance for any help or example.
I read from the SQL table, edited, and I have sb, and I have:
{50001,Pingu,C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg,A,Skift 2,Requill,sQ,,,,,S5,,,,,,,Motvikt,,Kvalité_nr_2,,,,,,,,,,,,} col(0) col(1) col(2) col(3) col(4) col(5) col(6) col(7) 50001 Pingu Pathway A Skift 2 Requill SQ ""
Now I want to save back to SQL. The number of columns and names in SQL are changing. That is why I use an array.
Vidor
I tried the following (see code) and now I get an error message:
Must declare @Param scalar variable
The cbItems array contains the column name in the database.
string[] cbItems; cbItems = sbItems.ToString().Split(','); string[] input; input = sb.ToString().Split(','); DataSet dataSet = new DataSet(); dataTable = dataSet.Tables.Add(); SqlConnection cn = new SqlConnection(connString); int firstElement; Int32.TryParse(input[0], out firstElement); string sql = "INSERT INTO UserData1 (Anställningsnummer) VALUES (@ID)"; cn.Open(); SqlCommand cmd = new SqlCommand(sql, cn); cmd.Parameters.AddWithValue("@ID", firstElement); cmd.ExecuteNonQuery(); int i = 1; foreach (string r in input) { string paramName = cbItems[i].ToString(); string anyElement= input[i].ToString(); SqlCommand myInsertCmd = new SqlCommand("INSERT INTO UserData1(" + paramName + ") VALUES(@Param)", cn); cmd.Parameters.AddWithValue("@Param", anyElement); i++; myInsertCmd.ExecuteNonQuery(); } cn.Close();
Did I miss something?
Vidor