将.csv文件导入sql表 [英] Importing .csv file into sql table

查看:107
本文介绍了将.csv文件导入sql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

请查看以下代码,我正在使用这些代码将csv文件导入到sql表中:

Dear All,

Please have a look at the following code which I am using to import csv file into a sql table:

string Filepath = Server.MapPath("Documents") + "\\" + System.DateTime.Now.ToString("dd_MM_yyyy_mm_ss_") + fupFile.FileName;
                        fupFile.SaveAs(Filepath);
                        if (FileExt == ".xls")
                        {
                            sExcelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Filepath + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;\"";
                        }
                        else if (FileExt == ".xlsx")
                        {
                            sExcelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Filepath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
                        }
                        else
                        {
                            sExcelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Filepath + ";Extended Properties=\"text;HDR=YES;FMT=Delimited\"";
                        }



在这里,我能够导入.xls和.xlsx.但是,当我尝试导入.csv文件时,出现错误.我尝试在google中搜索解决方案,但没有找到合适的解决方案.

任何帮助或建议,将不胜感激.

问候,



Here,I am able to import .xls and .xlsx. But, when I try to import .csv file, its giving an error. I tried searching in google for the solution, but not getting the suitable solution.

Any help or suggestion would be greatly appreciated.

Regards,

推荐答案

为此,请使用c#中的BulkCopy命令:
http://msdn.microsoft.com/en-us/library/system. data.sqlclient.sqlbulkcopy.aspx [ ^ ]
use the BulkCopy command in c# for this:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx[^]


尝试使用文本"连接字符串,例如

Provider = Microsoft.Jet.OLEDB.4.0; Data Source = c:\ csvfolder \; Extended Properties ="text; HDR = Yes; FMT = Delimited";


http://www.connectionstrings.com/textfile [
Try using a "text" connection string such as

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\csvfolder\;Extended Properties="text;HDR=Yes;FMT=Delimited";


http://www.connectionstrings.com/textfile[^]


这篇关于将.csv文件导入sql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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