问题
"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