الأحد، 2 أكتوبر 2011

how to import data from an Excel 2007 & 2010 sheet to the SQL Server




 Here we are the code below ,My friends :
 ///




          /// PM:2-10-2011
          /// Upload excel sheet
          ///




          ///
          ///
          private void button1_Click(object sender, EventArgs e)
          {
               OpenFileDialog file1 = new OpenFileDialog();
               file1.Filter = "excel2007(*.xlsx)|*.xlsx|EXCEL 2003 (*.xls)|*.xls";
               file1.ShowDialog();
               txtFile.Text = file1.FileName.ToString();

          }
        
          ///



          /// PM: Complete logic is here
          /// and insert into Staff List table ,Or update if exist
          ///




          ///
          ///

          private void Import_Click(object sender, EventArgs e)
          {
               try
               {
                    //PM :for office 2007 and later
                    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtFile.Text + ";Extended Properties=\"Excel 12.0;HDR=Yes;\"");
                    OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", con);
                    con.Open();
                    OleDbDataReader reader1 = cmd.ExecuteReader();

                    int b = 0, i = 0;
                    b = reader1.VisibleFieldCount;   // To get total number of columns

                    // To get first row in the given excel and we consinder them as column names
                    string str = "[" + reader1.GetName(i) + "]" + " nvarchar(200)";
                    i++;
                    while (i < b)
                    {
                         str += "," + "[" + reader1.GetName(i) + "]" + " nvarchar(200)";
                         i++;
                    }

                    // Sql Server Connection and Creation of table based on the user input
                    cnString = GetConnection.GetConnectionString("con");
                    SqlConnection con1 = new SqlConnection(cnString);
                    con1.Open();

                    // append with auto update Primary key ID Field
                    string stringg = "[ID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY," + str + ",[OrgContent] [text] NULL";
                    SqlCommand cmd1 = new SqlCommand("create table StaffList " + "(" + stringg + ")", con1);
                    cmd1.ExecuteNonQuery();

                    // To goto all the rows in the excel file and later for insertion into db
                    while (reader1.Read())
                    {

                         SqlConnection con2 = new SqlConnection(cnString);
                         con2.Open();

                         string str1 = "";
                         int j = 0;

                         // Checking for empty cells in the excel and handling them
                         if (reader1.IsDBNull(j))
                         {
                              str1 = "'" + " " + "'";
                         }
                         else
                         {
                              str1 = reader1[j].ToString();
                         }

                         j++;
                         while (j < b)
                         {
                              if (reader1.IsDBNull(j))
                                   str1 += "," + "'" + " " + "'";
                              else
                              {
                                   str1 += "," + "'" + reader1.GetString(j).ToString() + "'";
                              }

                              j++;
                         }

                         // Inserting data into the given database
                         SqlCommand cmd2 = new SqlCommand("insert into StaffList([Education Number],[URL] ,[Staff List Description],[Type],[PAGE])  values(" + str1 + ")", con2);
                         cmd2.ExecuteNonQuery();
                         con2.Close();

                    }

                    con.Close();
                    MessageBox.Show("Successfully Completed");
               }
               catch (Exception ex)
               {
                    MessageBox.Show(ex.Message);

               }
          }

ليست هناك تعليقات:

إرسال تعليق

كل ما تريد معرفته عن فيتامين "لاكتوفيرين"

ماهو اللاكتوفيرين ؟ يعتبر اللاكتوفيرين بروتين طبيعي موجود في الحليب الحيواني للأبقار وكذلك يتواجد في حليب الأمهات في الإنسان، ويتواجد بكثر...