C# 将数据集保存到数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11608546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:30:22  来源:igfitidea点击:

Saving Dataset to database

c#sql-serverdatagridviewdatasettableadapter

提问by Saad Farooq

I am trying to save a dataset to a database. I got a dataset from another class, Now changes will be made on the form by a user on a datagridview, then the changed Dataset needs to be saved in the database.

我正在尝试将数据集保存到数据库。我从另一个类中获得了一个数据集,现在用户将在 datagridview 上对表单进行更改,然后需要将更改后的数据集保存在数据库中。

I am using the below code; Its not generating any errors, but the data is not being saved in the database.

我正在使用以下代码;它没有产生任何错误,但数据没有保存在数据库中。

public class myForm    
{    
    DataSet myDataSet = new DataSet();
    public void PouplateGridView()
    {
        try
        {
            SqlService sql = new SqlService(connectionString); // Valid Connection String, No Errors


            myDataSet = sql.ExecuteSqlDataSet("SELECT * FROM Qualification"); // Returns a DataSet
            myDataGridView.DataSource = myDataSet.Tables[0];
            myDataGridView.AutoGenerateColumns = true;
            myDataGridView.AutoResizeColumns();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.InnerException + Environment.NewLine + ex.Message, "Error");
            this.Close();
        }

    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        //myDataSet.AcceptChanges();EDIT:Don't know why, but this line wasn't letting the chane in db happen.
        SqlCommand sc = new SqlCommand("SELECT * FROM Qualification", sql.Connection); //ADDED after Replies
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommandBuilder scb = new SqlCommandBuilder(da); //ADDED after replies
        da.Update(myDataSet.Tables[0]);
    }
}
public class mySqlService
{
public DataSet ExecuteSqlDataSet(string sql)
        {
            SqlCommand cmd = new SqlCommand();
            this.Connect();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            cmd.CommandTimeout = this.CommandTimeout;
            cmd.Connection = _connection;
            if (_transaction != null) cmd.Transaction = _transaction;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;

            da.SelectCommand = cmd;

            da.Fill(ds);
            da.Dispose();
            cmd.Dispose();

            if (this.AutoCloseConnection) this.Disconnect();

            return ds;
        }
}

What am I doing wrong here? There are ways on the web to save the dataset, if the datset is created, edited and saved in the same class etc., BUT I would like to have the select dataset method in the mySqlService class. How should I, now can save the dataset to the database?

我在这里做错了什么?网上有保存数据集的方法,如果数据集是在同一个类中创建、编辑和保存的等等,但我希望在 mySqlService 类中有选择数据集的方法。我应该如何,现在可以将数据集保存到数据库中?

EDIT: I have commented the three lines that were required to make the code work. The code works now.

编辑:我已经评论了使代码工作所需的三行。该代码现在有效。

采纳答案by adatapost

In order to run Updatemethod of SqlDataAdapteryou must have to configure InsertCommand, DeleteCommandand UpdateCommandproperties along with SelectCommandof SqlDataAdapter or construct the SqlCommandBuilderobject which configure these commands implicitly.

为了运行更新的方法SqlDataAdapter您必须配置InsertCommandDeleteCommandUpdateCommand沿着属性SelectCommand的SqlDataAdapter或构建SqlCommandBuilder含蓄地配置这些命令对象。

回答by bUKaneer

Hey try following this tutorial here http://support.microsoft.com/kb/308507first and then adapt it to your needs.

嘿,首先尝试在http://support.microsoft.com/kb/308507 上遵循本教程,然后根据您的需要进行调整。