您好我想问有没有办法自动插入列名?
for (int i = 0; i < ADODB.Rows.Count; i++)
{
StrQuery = @ "INSERT INTO tableName VALUES (" + ADODB.Rows[i].Cells["ColumnName"] + ", " + ADODB.Rows[i].Cells["ColumnName"] + ");";
comm.CommandText = StrQuery;
comm.ExecuteNonQuery();
}
答案 0 :(得分:0)
试试这个
dataGridView1.Rows[0].Cells[i].Text // i is column number
或
dataGridView1.Rows.HeaderRow.Cells[i].Text // i is column number
答案 1 :(得分:0)
如果我没有理解错误,那么这个怎么样?
string[] columnNames = dataGridView1.Columns.Cast<DataGridViewColumn>()
.Select(x => x.HeaderText).ToArray();
// get column names to string array
string joinedArray = string.Join(",", columnNames);
// set them into string seperating with ,
string sql = "INSERT INTO tableName VALUES (" + joinedArray + ")";
希望有所帮助,