简体   繁体   English

Webmatrix csv导入到SQL Server Compact

[英]Webmatrix csv import to SQL Server Compact

I just switched to Webmatrix because the server I was using stopped supporting MySQL / PHP. 我刚刚切换到Webmatrix,因为我使用的服务器停止支持MySQL / PHP。 Now I'm converting my old php mysql site to Webmatrix with SQL Server Compact. 现在我用SQL Server Compact将旧的php mysql站点转换为Webmatrix。 But I have hit a little snag. 但是我遇到了一些障碍。 I'm using a script to directly import csv files into my database, in php/mysql this was not that hard to do but I can't get it to work in Webmatrix and SQL Server Compact. 我正在使用脚本将csv文件直接导入到我的数据库中,在php / mysql中这并不难,但我无法在Webmatrix和SQL Server Compact中使用它。 Anyone having ideas on why it does not work? 有人为什么不起作用的想法?

mysql_query("TRUNCATE TABLE `tmp_st_age`");
   $age = 'load data local infile "../csv/tmp_st_age.csv"
        into table `tmp_st_age` 
        fields terminated by ";"
        enclosed by "\""
        LINES TERMINATED BY "\r\n"
        IGNORE 1 LINES';

mysql_query($age) or die(myqsl_error());
echo "Done! ".mysql_affected_rows()." rows inserted into tmp_st_age.<br>";

Above code is something I'm trying to implement in Webmatrix but I can't seem to get it correct. 上面的代码是我试图在Webmatrix中实现的东西,但我似乎无法弄清楚它。 Is it so that this is a mysql feature only? 难道这只是一个mysql功能吗? If so, is there any good workaround that means not using a third party program, a web based input is what I'm looking for. 如果是这样,是否有任何好的解决方法,这意味着不使用第三方程序,基于Web的输入是我正在寻找的。

If you were using SQL Server instead of SQL Compact, you could use BULK INSERT which is a very similar approach to the MySQL one you illustrated in your question. 如果您使用的是SQL Server而不是SQL Compact,则可以使用BULK INSERT ,这是一种与您在问题中说明的MySQL非常相似的方法。 Unfortunately SQL Compact doesn't support that so you are left with reading the csv and inserting each row individually. 不幸的是,SQL Compact不支持这一点,所以你只需要阅读csv并单独插入每一行。 Something along the lines of: 有点像:

var db = Database.Open("your_db");
var data = File.ReadAllLines(path_to_csv_file);
foreach(var row in data){
    var columns = row.Split(new []{';'});
    var sql = "INSERT INTO MyTable (f1, f2, f3, etc) VALUES (@0, @1, @2, etc)";
    db.Execute(sql, item[0], item[1], item[2], etc);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM