不能将数据插入到WPF本地数据库 [英] Cannot insert data into Local Database in WPF

查看:344
本文介绍了不能将数据插入到WPF本地数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个项目中,我有一个本地数据库,(我使用DataGrid查看数据),但问题是,当我插入到数据库中使用插入查询,插入是不持久的,即当我重新运行该应用程序,数据库没有改变,变化不会反映在数据库视图(在Visual Studio中查看表中的数据)。

插入查询是:

 字符串dbCon =新System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly()位置。).DirectoryName +\\ MyDatabase.sdf;
        的SqlCeConnection DBConnection进行=新的SqlCeConnection(数据源=+ dbCon);

        dbconnection.Open();

        字符串插入=插入MainTable(另一LineID,日期,Connection_Type)VALUES('5','2012年5月8日,商业);
SqlCeCommand CMD =新的SqlCeCommand(插入,DBConnection进行);

        ////执行命令
        cmd.ExecuteNonQuery();

        dbconnection.Close();
 

现在这个code运行时,数据网格被更新,但改变不是永久性的。它是什么,我做错了什么? 我使用的Visual Studio 2010旗舰版和Windows 7

解决方案
  1. Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()位置)+\\ MyDatabase.sdf是路径到您的应用程序文件夹。根据安装选项的文件可能不会被安装在那里。使用数据源= | DataDirectory目录| \\ MyDatabase.sdf; ......一个连接字符串所描述的这里

  2. 即使你的数据实际上存放在那里,这取决于用户权限的用户可能无法写入该目录。此外,当用户卸载或更新您的应用程序,其数据将被删除。人们不喜欢这样。你应该将数据复制到一个用户可写文件夹;前面的链接显示了如何做到这一点。

  3. 如果你有一个Visual Studio项目与数据库项目的一部分,恐怕也会有复制到输出目录属性设置为复制。这意味着每次你在调试器中运行时,你得到一个全新的数据库,从源项目复制,覆盖之前无论在那里。任何更改都将在这个临时的输出文件夹,而不是在原来的源数据。当您再次调试应用程序,临时副本将被删除。在这个答案。

In a project, I have a local database, (I am using the datagrid to view the data), but the problem is that when I insert into database using the insertion query, the insertion is not lasting, i.e. when I rerun the application, the database is not changed and the changes are not reflected in the database view (view table data in Visual Studio).

The insertion query is:

string dbCon = new     System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\MyDatabase.sdf";
        SqlCeConnection dbconnection = new SqlCeConnection("datasource=" + dbCon);

        dbconnection.Open();

        String Insert = "insert into MainTable(LineID,Date,Connection_Type)  Values('5','5/8/2012','commercial')";
SqlCeCommand cmd = new SqlCeCommand(Insert, dbconnection);

        //// Execute the command
        cmd.ExecuteNonQuery();

        dbconnection.Close();

Now when this code runs, the datagrid is updated, but the changes are not permanent. What is it that I am doing wrong? I am using Visual Studio 2010 Ultimate and Windows 7

解决方案

  1. Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\MyDatabase.sdf" is the path to your application folder. Depending on installation options your file may not be installed there. Use a connection string of "DataSource=|DataDirectory|\\MyDatabase.sdf;..." as described here.

  2. Even if your data is actually stored there, depending on user permissions the user may not be able to write to that directory. Also, when the user uninstalls or updates your app, his data will be deleted. People don't like that. You should copy the data to a user-writable folder; the earlier link shows how to do that as well.

  3. If you have a Visual Studio project with a database a part of your project, it will probably have the "Copy to Output Directory" property set to "Copy". That means each time you run in the debugger you get a brand-new database copied from your source project, overwriting whatever was there before. Any changes will be in this temporary output folder, not in your original source data. When you debug the app again, the temporary copy is deleted. Further explanation in this answer.

这篇关于不能将数据插入到WPF本地数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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