How to fix the SQL query that come out like this
insert into T (NO_ID, Room, RoomID, Name,)
values (10, 10, 10, 10,)
The button 3 when click will save the data to SQL
private void button3_Click(object sender, EventArgs e)
{
string[] text = new string[DT.Columns.Count];
foreach (DataColumn DC in DT.Columns)
{
for (int k = 0; k < DT.Columns.Count; k++)
{
// to save the datacolumn headertext name to string[]
text[k] = DC.Table.Columns[k].ToString();
}
}
}
The SQL parts t11 is SQL connection string
SqlConnection SC = new SqlConnection(T11);
SC.Open();
// SQL query parts
StringBuilder command = new StringBuilder("insert into ");
//T33 is the table name
command.Append(T33).Append("(");
// I use the forloop to keep add string on `string[]`
for (int i = 0; i < DT.Columns.Count; i++)
{
command.Append(text[i]+",");
}
command.Append(")values(");
for (int l= 0; l < DT.Columns.Count; l++)
{
command.Append("10"+",");
}
command.Append(")");
using (SqlCommand sqlCommand = new SqlCommand(command.ToString(), SC))
{
sqlCommand.ExecuteNonQuery();
}
The error screenshot: