简体   繁体   English

C#将datagridview数据写入SQL表

[英]C# write datagridview data into a SQL table

I have a four coloum table in an SQL database. 我在SQL数据库中有一个四个coloum表。 The info for the first three colums is supplied by another source. 前三个列的信息由另一个源提供。 coloum 4 is set to null by default. coloum 4默认设置为null。

I then have a win form with a datatable that populates with the information from the sql tabase using the following code: 然后,我有一个带有数据表的win表单,该表单使用以下代码填充sql tabase中的信息:

    public DataTable populateFormList()
    {


            SqlConnection con = new SqlConnection(Properties.Settings.Default.sqlConnectionString);
            SqlCommand cmd = new SqlCommand("SELECT * FROM of_formlist_raw", con);
            con.Open();

            SqlDataReader reader = cmd.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(reader);

            return dt;
}

datagridview2.DataSource = populateFormList();
datagridview2.Refresh();

Now that works fine in obtaining my data. 现在,它可以很好地获取我的数据。

The user can then make changes to the null values in column 4. 然后,用户可以更改第4列中的空值。

How can I easily write these changes from the datatable back into the SQL table? 如何轻松地将这些更改从数据表写回到SQL表中?

In other words, once the on screen datatable has aditional values, how can I then store the updated information back in the SQL data from which it was originally obtained from? 换句话说,一旦屏幕数据表具有aditional值,我如何将更新的信息存储回最初从中获取的SQL数据?

Thanks. 谢谢。

Try something like this and just pass (DataTable)datagridview2.DataSource as the data table: 尝试这样的东西,只需传递(DataTable)datagridview2.DataSource作为数据表:

private static void BulkInsertToSQL(DataTable dt, string tableName)
        {

                using (SqlConnection con = new SqlConnection(_DB))
                {
                    SqlBulkCopy sbc = new SqlBulkCopy(con);
                    sbc.DestinationTableName = tableName;

                    //if your DB col names don’t match your data table column names 100%
                    //then relate the source data table column names with the destination DB cols
                    sbc.ColumnMappings.Add("DBAttributeName1", "DTColumnName1");
                    sbc.ColumnMappings.Add("DBAttributeName2", "DTColumnName2");
                    sbc.ColumnMappings.Add("DBAttributeName3", "DTColumnName3");
                    sbc.ColumnMappings.Add("DBAttributeName4", "DTColumnName4");

                    con.Open();

                    sbc.WriteToServer(dt);
                    con.Close();
                }


        }

2 options, with or without TableAdapter. 2个选项,带或不带TableAdapter。

I would recommend to read this in MSDN for TableAdapter 我建议在MSDN中阅读 TableAdapter

They're using BindingSources too, which are excellent components, easy-to-use. 他们也在使用BindingSources,它们是优秀的组件,易于使用。

Without TableAdapter, read this , the "Update Records Using Command Objects" part. 如果没有TableAdapter,请阅读 “使用命令对象更新记录”部分。

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

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