Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi i am working on a project, i want to pick up an excel file and display record in grid view, than

i have to save that file in sql server 2005 database name 'pension'

code to reterieve file and display in grid view controll is as follows:
private void button3_Click(object sender, EventArgs e)
        {
            DialogResult dr = this.openFileDialog1.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                string connectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data 
                                                        Source={0};Extended Properties=""Excel 
                                           8.0;HDR=YES;IMEX=1;""", openFileDialog1.FileName);
                string query = String.Format("select * from [{0}$]", "Sheet1");
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet);
                dataGridView1.DataSource = dataSet.Tables[0];
            }
        }

this is working fine , now i want to save in database
private void button2_Click(object sender, EventArgs e)
        {
           DataSet ds = new DataSet();
           SqlConnection cs = new SqlConnection("Data Source=admn\\SQLEXPRESS;Initial             

                                Catalog=DB;Integrated Security=True");
               SqlDataAdapter da = new SqlDataAdapter();

            da.InsertCommand = new SqlCommand("INSERT INTO Pension                          

VALUES(@ID,@Experience,@pension)", cs);
           
            cs.Open();
            da.InsertCommand.ExecuteNonQuery();
            MessageBox.Show("Your value has been added");            
            cs.Close();
        }

what to write code before cs.open, can any one help me out
Posted
Updated 18-Jul-11 20:31pm
v2

 
Share this answer
 
specifically ths is a code to enter only one value, what will be the code to enter number of fields and number of records

SQL
da.InsertCommand.Parameters.Add("@Login", SqlDbType.VarChar).Value = textbox1.Text;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900