将datagridView保存到sql数据库 [英] Save datagridView into sql database

查看:197
本文介绍了将datagridView保存到sql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次将datagrid保存到数据库,谢谢您的帮助。

我这样填充dataGrid:

First time saving datagrid to database thanks for any help.
I fill dataGrid this way:

 private void FillGrid1()
    {
        frmEditovat frm2 = new frmEditovat(this);
        DataTable DT = null;
        DataRow newRow;
        int pTypNastaveniaID = 0;
        string pNazov = "", pHodnota = "";

        DataConnector_My dataConnector = new DataConnector_My(mDataRoot.ConnectCentrum.ConnectionString);


        try
        {
            dataConnector.Init(mDataRoot.ConnectCentrum);

            DT = dataConnector.Get_Nastavenia();

            /* Odstranim zdroj pre Grid. Musi byt koli triedeniu gridu. */
            dataGridView1.DataSource = null;
            /* Smazanie Tabulky. */
            mDTable1.Clear();

            for (int row = 0; row < DT.Rows.Count; row++)
            {
                pTypNastaveniaID = 0; pNazov = ""; pHodnota = "";

                if (DT.Rows[row]["TypNastaveniaID"] != DBNull.Value) pTypNastaveniaID = Convert.ToInt32(DT.Rows[row]["TypNastaveniaID"]);
                if (DT.Rows[row]["Nazov"] != DBNull.Value) pNazov = DT.Rows[row]["Nazov"].ToString();
                if (DT.Rows[row]["Hodnota"] != DBNull.Value) pHodnota = DT.Rows[row]["Hodnota"].ToString();

                newRow = mDTable1.NewRow();

                newRow["TypNastaveniaID"] = pTypNastaveniaID;
                newRow["Nazov"] = pNazov;
                newRow["Hodnota"] = pHodnota;


                mDTable1.Rows.Add(newRow);
            }


            /* Datovy zdroj zpet. */
            dataGridView1.DataSource = mDTable1;

            Application.DoEvents();
        }
        catch (SqlException e)
        {
            MessageBox.Show(this.GetType() + ".FillGrid: " + Environment.NewLine + e.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            DT.Dispose();
        }

    }

我想将datagriView1保存到单击按钮后的操作后的数据库:

I want to save datagriView1 to the database after click action in button :

  private void btnSave_Click_1(object sender, EventArgs e)

或者将提供一些类似代码的示例。

Or some example with similar code would be appreciate .

推荐答案

由于您未指定是否使用任何TableAdapter,我猜您只是停留在Datatable上...

Since you didn't specify if you're using any TableAdapters I guess your just stuck with your Datatable ...

最简单的方法是先寻找更改,然后遍历它们。根据它们的性质(NEW,UPDATE,DELETED)对数据库执行您自己的查询。

The simplest would be to first look for the changes and then iterate over them. According to their nature (NEW, UPDATE, DELETED) perform your own queries against the Database.

var dataTable = ((DataTable)dataGridView1.DataSource).GetChanges();
if(dataTable != null && dataTable.Rows.Count > 0)
{
  foreach (DataRow row in dataTable.Rows)
        {
            switch (row.RowState)
            {
                case DataRowState.Added:
                    // DO INSERT QUERY
                    break;
                case DataRowState.Deleted:
                    // DO DELETE QUERY
                    break;
                case DataRowState.Modified:
                    SqlCommand command = new SqlCommand("UPDATE YOURTABLE SET TypNastaveniaID = @typ, Nazov = @title, Hodnota = @amount");
                    command.Parameters.Add(new SqlParameter("@typ", row["TypNastaveniaID"]));
                    command.Parameters.Add(new SqlParameter("@title", row["Nazov"]));
                    command.Parameters.Add(new SqlParameter("@amount", row["Hodnota"]));
                    command.ExecuteNonQuery();
                    break;
            }
        }
    ((DataTable)dataGridView1.DataSource).AcceptChanges();
}

希望您能想到。请注意,AcceptChanges,需要在根表上调用。它将更改设置为 DONE,因此,下次单击时,您将不会再次遍历相同的行。

Hope you get the idea. Note the AcceptChanges, which needs to be called on the root Table. It will set the changes to "DONE", so next time you click you wont iterate over the same rows again.

这篇关于将datagridView保存到sql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆