c#Insert into Database function Advice Please

I am struggling with a public bool insert, which is used to take the input of a dictionary containing the values which are then indexed by the column names as well as a string representing the table name. I then want it to return a boolean to indicate the status of the insert.

So far I have

 public bool Insert(Dictionary<string, string> values, string tableName) { SqlDataAdapter adapter; using (SqlConnection conn = new SqlConnection(ConnectionString)) { string query = "INSERT INTO" + tableName + "VALUES({string.Join("," values)})"; using (SqlCommand command = new SqlCommand(query, conn)) { conn.Open(); adapter = command.ExecuteAdapter(); } } return adapter; } 

I want this function to insert to any table which I select from my current drop down list, hence the use of tableName as a string.

I can add more info if required, so far some of the syntax is causing me problems.

by PHPn via /r/csharp

Leave a Reply