是否正确Sql将数据从csv文件导入到ms访问

时间:2013-01-10 19:05:58

标签: sql csv ms-access-2007 ms-access-2010

"INSERT INTO CricketMatch
(FName,LastName,Runs,Matches,Location,DateofMatch,PLayerId)
SELECT
    Fname,Lname,Runsby,numberofMatches,MatchLocation,DofMatch,Id
FROM [Text;Data Source=C:\\DB\\;HDR=YES].[filename]";

filename只是扩展名为demo.csv no path的名称

cmd.CDataSet da = new DataSet();string filename = Path.GetFileName(txtCSVFilePath.Text);
try {da = this.ConnectCSV(filename);
     string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\DB\\MatchDetails.accdb"; 
    OleDbConnection conn = new OleDbConnection(connstring);
    OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;conn.Open(); 
    for (int i = 0; i <= da.Tables["Ss"].Rows.Count - 1; i++){ 
        for (int j = 1; j <= da.Tables["Ss"].Columns.Count - 1;j++){ 
           cmd.CommandText="INSERTINTOCricketMatch(FName,LastName,Runs,Matches,Location,DateofMatch,PLayerId)SELECT(Fname,Lname,Runsby,numberofMatches,MatchLocation,DofMatch,Id)  FROM [Text;FMT=Delimited(;);HDR=NO;DATABASE=C:\\DB]." + filename;cmd.Connection = conn; cmd.ExecuteNonQuery();} }} 

这就是我正在尝试的

2 个答案:

答案 0 :(得分:4)

您的语法看起来非常接近 - 尝试使用类似的方法连接到csv文件:

SELECT Fname,
  Lname,
  Runsby,
  numberofMatches,
  MatchLocation,
  DofMatch,
  Id 
FROM [Text;FMT=Delimited;HDR=YES;DATABASE=C:\DB].filename.csv;

答案 1 :(得分:1)

像这样,正如我已经说过的那样:

INSERT INTO CricketMatch
(FName,LastName,Runs,Matches,Location,DateofMatch,PLayerId)
SELECT
    Fname,Lname,Runsby,numberofMatches,MatchLocation,DofMatch,Id
FROM [Text;Database=z:\docs\;HDR=yes].[importfilename.csv]