将 Excel 导入到 SQL Server CE

发布于 2024-09-01 22:46:40 字数 2240 浏览 6 评论 0原文

我有一个 Excel 格式的表(2007 年,但我自然可以保存为低于该格式的任何内容),并且我有一个带有相应列的 SQL Server Compact Edition 3.5 SP1 数据库表。我只想用 Excel 文件中的数据填充 SQLCE 表。数据仅由字符串和整数组成。

我尝试了这个实用程序但没有成功,我也尝试了这个SQL 脚本,但自从 BULK INSERT< SQL Server CE 不支持 /code>。我还找到了这个 Microsoft 教程,但是当涉及到 SQL、提供程序和就像...

谢谢!

编辑:我想出了以下代码(通过这篇文章)

        var fileName = string.Format("{0}\\DataValues.xls", Directory.GetCurrentDirectory());
        var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);

        var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
        var ds = new DataSet();

        adapter.Fill(ds, "anyNameHere");

        DataTable dataTable = ds.Tables["anyNameHere"];

        foreach (DataRow row in dataTable.Rows)
            _entities.MyObjectSet.AddObject(new MyObject()
            {
                Foo= (string)row[1],
                Bar= Convert.ToInt32(row[2]),
            });

        _entities.SaveChanges();

但 SaveChanges() 崩溃并显示“SQL Server Compact 不支持服务器生成的键和服务器生成的值”。我尝试了 将所有 Id 类型更改为 GUID(=uniqueidentifier) 的解决方法,但我仍然收到消息

EDIT2:我将 StoreGeneratePattern 属性更改为“None”,现在它可以工作了。需要注意的一个有趣的事情是,.SDF 文件被复制到运行时目录,并且正是该 副本 被更改,因此您不会看到以下更改项目中链接的 .SDF - 您必须手动复制它

我的问题仍然存在 - 是否有与以下内容等效的 SQL Server CE?

BULK 
INSERT YourDestinationTable
        FROM 'D:\YourFile.csv'
            WITH
    (
                FIELDTERMINATOR = ',',
                ROWTERMINATOR = '\n'
    )
GO

或者甚至更好 - 像 Microsoft 的 SQL Server 导入和导出向导一样可以导出到 SQL Server CE?

I have a table in excel format (2007 but I can save as anything below that, naturally), and I have a SQL Server Compact Edition 3.5 SP1 Database table with corresponding columns. I simply want to populate the SQLCE table with the data from the excel file. The data consists of strings and integers only.

I tried this utility to no avail, I also tried this SQL script but it won't work since BULK INSERT is not supported in SQL Server CE. I also found this Microsoft tutorial but I am basically clueless when it comes to SQL, providers and the like...

Thanks !

Edit: I came up with the following code (via this post)

        var fileName = string.Format("{0}\\DataValues.xls", Directory.GetCurrentDirectory());
        var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);

        var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
        var ds = new DataSet();

        adapter.Fill(ds, "anyNameHere");

        DataTable dataTable = ds.Tables["anyNameHere"];

        foreach (DataRow row in dataTable.Rows)
            _entities.MyObjectSet.AddObject(new MyObject()
            {
                Foo= (string)row[1],
                Bar= Convert.ToInt32(row[2]),
            });

        _entities.SaveChanges();

But SaveChanges() crashes with "Server-generated keys and server-generated values are not supported by SQL Server Compact". I tried the workaround of changing all Id types to GUID(=uniqueidentifier) but I still get the message

EDIT2: I changed the StoreGeneratedPattern property to "None" and now it works. An interesting thing to note is that the .SDF file is copied to the runtime directory, and it is that copy that is altered, so you won't see the changes in the .SDF linked in your project - you'll have to copy it manually

My question still remains - is there a SQL Server CE equivalent to the following ?

BULK 
INSERT YourDestinationTable
        FROM 'D:\YourFile.csv'
            WITH
    (
                FIELDTERMINATOR = ',',
                ROWTERMINATOR = '\n'
    )
GO

Or even better - something like Microsoft's SQL Server Import and Export Wizard that can export to SQL Server CE?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…红帽 2024-09-08 22:46:40

我会避免使用 EF(至少对于此)。

您尚未发布数据库架构,但请查看 这个

尽管 EF 一般支持它,但 CE 不支持,因此您将需要取消将其标记为服务器分配并在创建对象时分配它。

I would avoid using the EF (at least for this).

You haven't posted your database schema, but have a look at this and this.

Although EF supports it in general, CE does not, so you will need to un-mark it as server-assigned and assign it when the object is created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文