Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,647 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,128 people online right now. Registration is fast and FREE... Join Now!




Reading Excel sheet data and Sheet name and transfer to SQL data base

 
Reply to this topicStart new topic

Reading Excel sheet data and Sheet name and transfer to SQL data base

siddharthbjoshi
post 13 Aug, 2008 - 11:40 PM
Post #1


New D.I.C Head

*
Joined: 13 Aug, 2008
Posts: 1

i trying to get excel sheet name and binding sheet name into combobox
but using below code, only last sheet name is showing in combobox.
i dont know why



CODE


//For Reading excel sheet name and mount into combobox
private void btnbrows_Click(object sender, EventArgs e)
        {
            if (Opendialog.ShowDialog() == DialogResult.OK)
            {
                currentfilename = Opendialog.FileName;
                tbfilepath.Text = currentfilename;

                //Excel importing function
                string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + tbfilepath.Text.ToString() + ";Extended Properties=Excel 8.0;";
                con = new OleDbConnection(conStr);


                con.Open();
                dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

               if (dt == null)
               {
                   MessageBox.Show("no shhet found");;
               }
               string[] excelSheetNames = new string[dt.Rows.Count];
               int i = 0;
               int c = 0;

               foreach (DataRow row in dt.Rows)
               {
                   excelSheetNames[i] = row[c].ToString();
                   i++;
                   c++;
               }
               for (int s = 0; s < excelSheetNames.Length; s++)
               {
                   comboBox1.Items.Add(excelSheetNames[s].ToString());
               }
                      
            }



one more task is
i want to transfer excel data into SQL data base in existing table
for that i using following code

CODE
string constr= ConfigurationManager.AppSettings["dbpath"];
            string query="SELECT * INTO temp FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database='"+tbfilepath.Text+"','SELECT * FROM [Sheet2$]')";
            SqlConnection conn=new SqlConnection(constr);
            conn.Open();
            SqlCommand cmd = new SqlCommand(query, conn);
            SqlDataReader dr = cmd.ExecuteReader();//findng error in this line
            dr.Read();
            MessageBox.Show("done");
            conn.Close();
User is offlineProfile CardPM

Go to the top of the page

shelby poston
post 3 Sep, 2008 - 12:40 PM
Post #2


New D.I.C Head

*
Joined: 15 Apr, 2008
Posts: 5

I just uploaded a solution for the excel to sql take a look.Attached File  Excel2Sql.zip ( 23.85k ) Number of downloads: 88


QUOTE(siddharthbjoshi @ 14 Aug, 2008 - 12:40 AM) *

i trying to get excel sheet name and binding sheet name into combobox
but using below code, only last sheet name is showing in combobox.
i dont know why



CODE


//For Reading excel sheet name and mount into combobox
private void btnbrows_Click(object sender, EventArgs e)
        {
            if (Opendialog.ShowDialog() == DialogResult.OK)
            {
                currentfilename = Opendialog.FileName;
                tbfilepath.Text = currentfilename;

                //Excel importing function
                string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + tbfilepath.Text.ToString() + ";Extended Properties=Excel 8.0;";
                con = new OleDbConnection(conStr);


                con.Open();
                dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

               if (dt == null)
               {
                   MessageBox.Show("no shhet found");;
               }
               string[] excelSheetNames = new string[dt.Rows.Count];
               int i = 0;
               int c = 0;

               foreach (DataRow row in dt.Rows)
               {
                   excelSheetNames[i] = row[c].ToString();
                   i++;
                   c++;
               }
               for (int s = 0; s < excelSheetNames.Length; s++)
               {
                   comboBox1.Items.Add(excelSheetNames[s].ToString());
               }
                      
            }



one more task is
i want to transfer excel data into SQL data base in existing table
for that i using following code

CODE
string constr= ConfigurationManager.AppSettings["dbpath"];
            string query="SELECT * INTO temp FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database='"+tbfilepath.Text+"','SELECT * FROM [Sheet2$]')";
            SqlConnection conn=new SqlConnection(constr);
            conn.Open();
            SqlCommand cmd = new SqlCommand(query, conn);
            SqlDataReader dr = cmd.ExecuteReader();//findng error in this line
            dr.Read();
            MessageBox.Show("done");
            conn.Close();


User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:55AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month