简体   繁体   English

我可以将此数据从datagridview添加到数据库

[英]Can I add this data from datagridview to database

I'm here to ask you one thing. 我在这里问你一件事。

Can I add this data from datagridview to database with consumidor final id, clicking at Confirmar button? 我可以通过单击Confirmar按钮将数据从datagridview添加到具有consumidor final身份的数据库吗?

在此处输入图片说明

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();
}

For more, See this . 有关更多信息, 请参见此

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM