is It correct Sql to import data from csv file into msaccess

我怕爱的太早我们不能终老 提交于 2021-02-08 08:12:00

问题


"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 is just name with extension 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();} }} 

this is what i am trying


回答1:


Your syntax looks pretty close -- try something like this to connect to the csv file:

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



回答2:


Like so, as I already said:

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]


来源:https://stackoverflow.com/questions/14265433/is-it-correct-sql-to-import-data-from-csv-file-into-msaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!