日期:2014-05-17  浏览次数:20697 次

CSV导入SQL
大家好,

首先声明,本人是经过在网上仔细查找后,才来这里发帖的。

 

private void 导入_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "请选择要导入的文件:";
            dlg.Filter = "Csv文件(*.csv)|*.csv|所有文件|*.*";
            dlg.Multiselect = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
               string[] FileNames = dlg.FileNames;

               foreach (string FileName in FileNames)
               {
                   OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" + FileName);

                   DataSet DS = new DataSet();

                   string str = string.Format("select * from {0}", FileName);

                   OleDbDataAdapter Adapter = new OleDbDataAdapter(str, conn);

                   OleDbCommandBuilder Builder = new OleDbCommandBuilder(Adapter);

                   Adapter.Fill(DS, "FileName");

                   int Total = DS.Tables[0].Rows.Count;

                   Builder.Dispose();
                   Adapter.Dispose();