Am developing an application with c#. I want to know how to insert data set values into the database

Recommended Answers

All 3 Replies

Try using SqlDataAdapter for this job.
Or you can even try using SqlComamndBuilder (of OleDb for Access database instead of Sql):

SqlDataAdapter adapter;
        DataSet dataset;
        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(Properties.Settings.Default.Database1ConnectionString);//Set the connection to the SQL server
            adapter = new SqlDataAdapter("select * from Table1", conn); //Create a DataAdapter instance
            dataset = new DataSet();
            SqlCommandBuilder builder = new SqlCommandBuilder();//User a CommandBuilder to build insert, update automatically.
            builder.DataAdapter = adapter;

            adapter.Fill(dataset);  //Fill the DataSet
            dataGridView1.DataSource = dataset.Tables[0]; //Bind the DataSet to a DataGridView
        } 

        //You can add/remove/modify the Data in the DataGridView
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            adapter.Update(dataset);//Update all the modification to the DataBase
        }

Here you can find some more info about updating databsase from dataSet (or dataTable).

I have tried all of the above of which they function to update data in the database i wanted to insert new data values to the database with DataSet

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.