将DataGridView的一行保存到数据库中

本文关键字:保存 一行 数据库 DataGridView | 更新日期: 2023-09-27 18:28:12

伙计们,我正试图将DataGridView的一行保存到数据库中。

到目前为止,我已经得到了一些例子,其中我们需要指定z INSERT语句中的列名和数字。如下所示。

string col1 = datagridview1[0, datagridview1s.CurrentCell.RowIndex].Value.ToString();
        string col2 = datagridview1[1, datagridview1.CurrentCell.RowIndex].Value.ToString();
        string col3 = datagridview1[2, datagridview1.CurrentCell.RowIndex].Value.ToString();
        string insert_sql = "INSERT INTO YourTable(col1,col2,col3)VALUES('"+ col1 +"','"+ col2 +"','"+ col3 +"'");
        this.getcom(insert_sql);
      }
      catch(Exception ex)
      { Message.Box(ex);}
     }

是否有任何方法可以在不必硬编码列详细信息的情况下执行INSERT?我想使用datagridview的列标题作为数据库中的列名来保存数据库中的行。

将DataGridView的一行保存到数据库中

datadapter.update(datatable_name);