本地数据库不保存数据。 [英] Local Database doesn't save data.

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

问题描述

您好,



当我尝试在我的本地数据库(SQL Compact)中插入内容时,它会添加。但它没有保存。我尝试了几件事。这是我的代码:



连接字符串

  public   static  SqlCeConnection Connect()
{
SqlCeConnection connection = new SqlCeConnection();
connection.ConnectionString = @ 数据源= | DataDirectory | \Database \Database.sdf< /跨度>;
connection.Open();

return 连接;
}





添加类。

< pre lang =cs> public static void AddTodo( string title, string description, string content)
{
SqlCeCommand command = new SqlCeCommand( < span class =code-string>插入项目(标题,描述,内容)值(@标题,@描述,@内容),Database.Connect());
command.Parameters.Add( new SqlCeParameter( 标题,标题));
command.Parameters.Add( new SqlCeParameter( 描述,描述));
command.Parameters.Add( new SqlCeParameter( 内容,内容));
command.ExecuteNonQuery();
Database.Disconnect();
}





最后一段代码

 私有  void  addButton_Click( object  sender,RoutedEventArgs e)
{
TodoAdder.AddTodo(titleBox.Text,descriptionBox.Text,contentBox.Text);
}





当我重建数据库时,它会将其删除。或者它永远不会在那里。



提前致谢。



更新的问题:

当我执行插入查询时,它确实显示在ListBox中。但是当我检查我的数据库时,它是空的。另外,当我重新启动程序时。它再次为空。

解决方案

将sdf文件标记为仅作为副本,如果不同或从解决方案中删除它并将其保留在1个位置,例如:应用程序的bin


添加值时尝试匹配参数名称。所以它说的是:

 command.Parameters.Add( new  SqlCeParameter( 标题,title)); 



应为:

 command.Parameters.Add( new  SqlCeParameter(  @ Title,title)); 


我不是积极的,但是我想我以前见过一些数据库配置有一个自动提交的标志或没有...你可能想确保在你的情况下打开自动提交...或者在添加记录后发送提交

Hello,

When I try to insert something into my Local Database (SQL Compact) it does add. But it doesn't save. I have tried serveral things. Here is my code:

Connection String

public static SqlCeConnection Connect()
{
    SqlCeConnection connection = new SqlCeConnection();
    connection.ConnectionString = @"Data Source=|DataDirectory|\Database\Database.sdf";
    connection.Open();

    return connection;
}



The "Add" Class.

public static void AddTodo(string title, string description, string content)
{
    SqlCeCommand command = new SqlCeCommand("insert into items (title, description, content) values (@Title, @Description, @Content)", Database.Connect());
    command.Parameters.Add(new SqlCeParameter("Title", title));
    command.Parameters.Add(new SqlCeParameter("Description", description));
    command.Parameters.Add(new SqlCeParameter("Content", content));
    command.ExecuteNonQuery();
    Database.Disconnect();
}



Last piece of code

private void addButton_Click(object sender, RoutedEventArgs e)
{
    TodoAdder.AddTodo(titleBox.Text, descriptionBox.Text, contentBox.Text);
}



When I rebuild database it deletes it. Or it never is in there.

Thanks in advance.

Updated question:
When I execute the insert query it does show up in the ListBox. But when I check my Database it is empty. Also, when I restart the program. It is empty again.

解决方案

Mark the sdf file as copy only if different or remove it from your solution and keep it only in 1 location e.g.: bin of your application


Try matching your parameter names when you add the values. So where it says this:

command.Parameters.Add(new SqlCeParameter("Title", title));


should be:

command.Parameters.Add(new SqlCeParameter("@Title", title));


I'm not positive, but I think I've seen before that some database configs have a flag for auto-commit or no... you may want to make sure auto-commit is turned on in your case... or send a commit after you add the record.


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

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