在 Visual Studio 2012 .SDF 文件中不替换(提交)我对原始文件的更改 [英] In visual Studio 2012 .SDF file not replacing (committing) my changes to original file

查看:25
本文介绍了在 Visual Studio 2012 .SDF 文件中不替换(提交)我对原始文件的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在插入 SQL CE 数据库时遇到问题.

I have a problem with inserting to my SQL CE Database.

我写了一些代码,然后当我需要一个数据库时,我右键单击了投影,添加了新项目,本地数据库,然后它让我选择一个数据模型 - 我选择了'数据集'.

I have wrote some code, then when I needed a DB, I have right clicked on projected, added new item, Local Database, after that it offered me to choose a datamodel - I selected 'dataset'.

这为我在左侧的服务器资源管理器下创建了一个数据库,在我的右侧,在解决方案资源管理器中可以看到相同的 .sdf 文件.

This has created a DB for me, under Server Explorer on my left, and same .sdf file is seen on my right, in solution explorer.

我启动了我的应用程序,我运行了一个插入查询,它给了我插入成功的输出,我看到 root/bin/Debug/db.sdf 下的 .sdf 文件刚刚被修改,我关闭了我的应用程序,但是我的原始数据库位于/root/db.sdf.如果我从服务器资源管理器查询数据库,我看不到更改/插入的行.这是我使用的代码:

I start my application, I run an insert query, it gives me output that insert was successful, I see that .sdf file under root/bin/Debug/db.sdf was just modified, I close my application, but my original database located at /root/db.sdf. If I query DB from Server explorer, I see no changes/inserted rows. Here is the code I use:

首先我尝试了 sever Data Source 选项,所有未注释的选项都不适合我.

First I have tried sever Data Source options, all uncommented ones did not work for me.

            //String connectString = @"Data Source=" + '"' + @"C:\Users\Alex\Documents\Visual Studio 2012\Projects\my\my\myDB.sdf;" + '"';
            //String connectString = "Data Source:=" + '"' + @"C:\Users\Alex\Documents\Visual Studio 2012\Projects\my\my\my.sdf" + '"';
            //String connectString = "Data Source:=C:\\Users\\Alex\\Documents\\Visual Studio 2012\\Projects\\my\\my\\my.sdf";
            //String connectString =@"Data Source:=C:\Users\Alex\Documents\Visual Studio 2012\Projects\my\my\myDB.sdf";
            String connectString = "Data Source=myDB.sdf";
            using (SqlCeConnection connection = new SqlCeConnection(connectString))
            {
                connection.Open();
                String query = "Insert into items (id, title)  VALUES ('" + ID + "', '" + title + "');                  
                SqlCeCommand cmd = new SqlCeCommand(string.Format(query), connection);
                int result = cmd.ExecuteNonQuery();  
            }

关闭应用程序后,我在项目表上单击鼠标右键,然后显示表数据" - 没有插入.我做错了什么?

After closing application I do a right click on the items table, and 'show table data' - no inserts. What am I doing wrong?

推荐答案

Visual Studio 可能正在将原始解决方案项的副本创建到 bin/Debug 子文件夹中(在每个构建中).执行代码时从不触及原始文件,下次构建应用程序时,您的更改可能会被丢弃.

Visual studio is probably creating a copy of your original solution item into the bin/Debug sub-folder (on every build). The original file is never touched while executing your code and your changes are possibly discarded the next time you build the application.

因此,根据您的用例,您必须在 bin/Debug 中打开数据库以查看数据,或者更改连接字符串以使用位于项目/解决方案根目录中的字符串.

So depending on your use-case you either have to open the database in bin/Debug for viwewing the data or change your connection string to use the one located in the root of your project/solution.

这篇关于在 Visual Studio 2012 .SDF 文件中不替换(提交)我对原始文件的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆