[英]Can I add this data from datagridview to database
Provided that the schema of the DataTable is the same as the schema of the database table you can just use a DataAdapter to insert the data. 只要DataTable的架构与数据库表的架构相同,您就可以使用DataAdapter插入数据。 Have a look here: How can I add data from DataTable to database table directly? 在这里看看: 如何将数据表中的数据直接添加到数据库表中? Stuart 斯图尔特
As you can try this: 您可以尝试以下方法:
SqlConnection sqlConnection = new SqlConnection(ConnString))
SqlCommand sqlCommand = new SqlCommand()
sqlCommand.Connection = sqlConnection;
sqlConnection.Open();
for(int i=0; i< myDataGridView.Rows.Count;i++)
{
query= @"INSERT INTO tableName VALUES ("
+ ddlConsumidorFinalId.SelectedValue + ", "
+ myDataGridView.Rows[i].Cells["ColumnName"].Value +", "
+ myDataGridView.Rows[i].Cells["ColumnName"].Value +");";
//Add as pre table fields
sqlCommand.CommandText = query;
sqlCommand.ExecuteNonQuery();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.