Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

c# - Saving Dataset to database

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

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...