如何将exisitsing数据表值插入数据库 [英] how to insert exisitsing datatable values to database

查看:74
本文介绍了如何将exisitsing数据表值插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.OleDb;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Globalization;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        System.Data.DataTable dtUK = null;
        System.Data.DataTable dt = null;
      
        public Form1()
        {
            InitializeComponent();
        }        
        private void btnbrowse_Click(object sender, EventArgs e)
        {
            /* Used to browse the Excel Sheet and viewd in data grid*/
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.DefaultExt = ".txt";
                dlg.Filter = "EXCEL Files (*.xls)|*.xlsx";
                // dlg.ShowDialog();
                //bool result = (bool)dlg.ShowDialog();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    string filename = dlg.FileName;

                    Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
                    Microsoft.Office.Interop.Excel.Workbook workbook = excelapp.Workbooks.Open(filename);
                    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["UK"];

                    int column = 0;
                    int row = 0;
                    Microsoft.Office.Interop.Excel.Range range = worksheet.UsedRange;

                    System.Data.DataTable dt = new System.Data.DataTable();
                    //Earnings Table

                    dt.Columns.Add("Name of the Employe", typeof(String));
                    dt.Columns.Add("Status", typeof(String));
                    dt.Columns.Add("DOL", typeof(String));
                    dt.Columns.Add("PAN No.", typeof(String));
                    dt.Columns.Add("Designation", typeof(String));
                    dt.Columns.Add("Present Days", typeof(String));
                    dt.Columns.Add("BankBranchName", typeof(String));
                    dt.Columns.Add("A/cNo", typeof(String));
                    dt.Columns.Add("PFA/cNo", typeof(String));
                    dt.Columns.Add("Basic Pay", typeof(String));
                    dt.Columns.Add("HRA", typeof(String));
                    dt.Columns.Add("Conveyance Allowance", typeof(String));
                    dt.Columns.Add("Medical Allowance", typeof(String));
                    dt.Columns.Add("Other Allowance", typeof(String));
                    dt.Columns.Add("Special Allowance", typeof(String));
                    dt.Columns.Add("Incentive", typeof(String));
                    dt.Columns.Add("Bonus", typeof(String));
                    dt.Columns.Add("Arrears", typeof(String));
                    dt.Columns.Add("Joining Bonus", typeof(String));
                    dt.Columns.Add("Leave Encashment", typeof(String));
                    //Deductions
                    dt.Columns.Add("TDS", typeof(String));
                    dt.Columns.Add("Professional Tax", typeof(String));
                    dt.Columns.Add("ProvidentFund", typeof(String));
                    dt.Columns.Add("DeductionagainstLoan/advances", typeof(String));
                    dt.Columns.Add("Cab Deductions", typeof(String));
                    dt.Columns.Add("ESI", typeof(String));
                    dt.Columns.Add("NetPay", typeof(String));
                    //Gross (TDS Details)
                    dt.Columns.Add("BasicPay", typeof(String));
                    dt.Columns.Add("emp1HRA", typeof(String));
                    dt.Columns.Add("ConveyanceAllowance", typeof(String));
                    dt.Columns.Add("MedicalAllowance1", typeof(String));
                    dt.Columns.Add("OtherAllowance1", typeof(String));
                    dt.Columns.Add("SpecialAllowance1", typeof(String));
                    dt.Columns.Add("Incentive1", typeof(String));
                    dt.Columns.Add("Bonus1", typeof(String));
                    dt.Columns.Add("Arrears1", typeof(String));
                    dt.Columns.Add("JoiningBonus1", typeof(String));
                    dt.Columns.Add("LeaveEncashment1", typeof(String));
                    dt.Columns.Add("empHRA1", typeof(String));
                    //Previous Employer Details
                    dt.Columns.Add("SalaryfromPreviousEmployer1", typeof(String));
                    dt.Columns.Add("DeductionFromPreviousEmployer1", typeof(String));
                    dt.Columns.Add("RemunerationFromPreviousEmployer1", typeof(String));
                    //Income tax Deduction
                    dt.Columns.Add("Grosssalary1", typeof(String));
                    dt.Columns.Add("Professionaltax1", typeof(String));
                    dt.Columns.Add("OtherDeductions1", typeof(String));
                    dt.Columns.Add("HouseProperty1", typeof(String));
                    dt.Columns.Add("IncomefromOtherSource1", typeof(String));
                    dt.Columns.Add("GrossIncome1", typeof(String));
                    dt.Columns.Add("TotalVI-ADeductions1", typeof(String));
                    dt.Columns.Add("TaxableIncome1", typeof(String));
                    dt.Columns.Add("TotalTax", typeof(String));
                    dt.Columns.Add("Rebateu/s87A", typeof(String));
                    dt.Columns.Add("Totaltaxafterrebate", typeof(String));
                    dt.Columns.Add("Educationcess", typeof(String));
                    dt.Columns.Add("TaxDeductedtilldate", typeof(String));
                    dt.Columns.Add("Taxtobededucted", typeof(String));
                    // Deductions under Chapter VI-A
                    dt.Columns.Add("Deduction80C", typeof(String));
                    dt.Columns.Add("ActualDeduction80C", typeof(string));
                    dt.Columns.Add("ActualDeduction80D", typeof(string));

                    dt.Columns.Add("Deduction80E", typeof(String));
                    dt.Columns.Add("Deduction80E1", typeof(String));
                    dt.Columns.Add("Deduction80E13", typeof(String));

                    for (row = 1; row <= range.Rows.Count; row++)
                    {
                        DataRow dr = dt.NewRow();
                        for (column = 1; column <= range.Columns.Count; column++)
                        {
                            dr[column - 1] = (range.Cells[row, column] as Range).Value2.ToString();
                        }
                        dt.Rows.Add(dr);
                        dt.AcceptChanges();
                    }

                    workbook.Close();
                    excelapp.Quit();

                    dataGridView1.DataSource = dt;
                }               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

推荐答案

不确定是否会给你一些提示。



http://social.msdn.microsoft.com/Forums/vstudio/en-US/57c2a1ac -d4f1-41c6-850f-264d30a873cb / how-to-insert-content-of-data-table-to-database?forum = csharpgeneral [ ^ ]
Not sure might be it will give you some hint.

http://social.msdn.microsoft.com/Forums/vstudio/en-US/57c2a1ac-d4f1-41c6-850f-264d30a873cb/how-to-insert-content-of-data-table-to-database?forum=csharpgeneral[^]


这篇关于如何将exisitsing数据表值插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆