コード例 #1
0
        private void importfromExcelToDB(string filepath)
        {
            CustomerForm.Helper hl = new CustomerForm.Helper();
            if (System.IO.File.Exists(filepath) == false)
            {
                return;
            }
            DataTable       dtimport = hl.dtFromExcelFile(filepath, "Contacts");
            int             reccount = 0;
            string          DQ       = "\"";
            MySqlConnection conn     = new MySqlConnection();

            label3.Text      = "Importing Records to Database Please Wait...";
            label3.ForeColor = Color.Blue;
            label3.BackColor = Color.White;
            label3.Visible   = true;
            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["mysqlconn"].ToString();

                foreach (DataRow dr in dtimport.Rows)
                {
                    string ph = dr["phone"].ToString().Replace(" ", "");

                    string strmycomd = "insert into  customer(firstname,lastname,email,phone,address) " +
                                       "values (" + DQ + dr["firstname"] + DQ + ", " + DQ + dr["lastname"] + DQ + ", " + DQ + dr["email"] + DQ +
                                       ", " + DQ + ph + DQ + ", " + DQ + dr["address"] + DQ + " );";
                    MySqlCommand mycmd = new MySqlCommand(strmycomd, conn);

                    conn.Open();
                    mycmd.ExecuteNonQuery();
                    conn.Close();
                    reccount++;
                }
            }
            catch (MySqlException mex)
            {
                MessageBox.Show("Error " + mex);
            }
            finally
            {
                conn.Close();
            }
            label3.Text      = "Importing Records  Complete..(" + reccount + ")";
            label3.ForeColor = Color.Blue;
            label3.BackColor = Color.White;
            label3.Visible   = true;
        }
コード例 #2
0
        private void importfromExcelToDB(string filepath)
        {
            CustomerForm.Helper hl = new CustomerForm.Helper();
            if (System.IO.File.Exists(filepath) == false)
            {
                return;
            }
            DataTable dtimport = hl.dtFromExcelFile(filepath, "Contacts");
            int reccount = 0;
             string DQ = "\"";
             MySqlConnection conn = new MySqlConnection();
             label3.Text = "Importing Records to Database Please Wait...";
             label3.ForeColor = Color.Blue;
             label3.BackColor = Color.White;
             label3.Visible = true;
             try
             {
                 
                 conn.ConnectionString = ConfigurationManager.ConnectionStrings["mysqlconn"].ToString();

                 foreach (DataRow dr in dtimport.Rows)
                 {
                     string ph = dr["phone"].ToString().Replace(" ", "");
                  
                     string strmycomd = "insert into  customer(firstname,lastname,email,phone,address) " +
                                    "values (" + DQ + dr["firstname"] + DQ + ", " + DQ + dr["lastname"] + DQ + ", " + DQ + dr["email"] + DQ +
                                    ", " + DQ + ph + DQ + ", " + DQ + dr["address"] + DQ + " );";
                     MySqlCommand mycmd = new MySqlCommand(strmycomd, conn);

                     conn.Open();
                     mycmd.ExecuteNonQuery();
                     conn.Close();
                     reccount++;
                 }
             }
             catch (MySqlException mex)
             {
                 MessageBox.Show("Error " + mex);
             }
             finally
             {
                 conn.Close();
             }
             label3.Text = "Importing Records  Complete..("+reccount+")";
             label3.ForeColor = Color.Blue;
             label3.BackColor = Color.White;
             label3.Visible = true;
             
        }