将数据集保存到数据库 [英] Saving Dataset to database

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

问题描述

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

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.

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

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

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

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?

我已经注释了使代码工作所需的三行.代码现在可以使用了.

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

推荐答案

为了运行SqlDataAdapterUpdate方法,你必须配置InsertCommandDeleteCommandUpdateCommand 属性以及 SqlDataAdapter 的 SelectCommand 或构造 SqlCommandBuilder 对象,隐式配置这些命令.

In order to run Update method of SqlDataAdapter you must have to configure InsertCommand, DeleteCommand and UpdateCommand properties along with SelectCommand of SqlDataAdapter or construct the SqlCommandBuilder object which configure these commands implicitly.

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

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