为什么我不能插入使用C#的数据到本地数据库(SQL精简版)? [英] Why can't I insert data into local database (SQL Compact Edition) with C#?

查看:141
本文介绍了为什么我不能插入使用C#的数据到本地数据库(SQL精简版)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的Visual Studio中的一个项目。我使用的是本地数据库(空SQL Server精简版)。我选择了数据集,创造了我的表(图像)。它有一个主要的自动增量ID列,一个nvarchar的ImagePath列。我想插入它的数据,这里是我的代码



 的SqlCeConnection CON =新的SqlCeConnection(); 
con.ConnectionString = yeniApplicationDatabase.Properties.Settings.Default.DatabaseEdaConnectionString;
con.Open();
使用(SqlCeCommand COM =新SqlCeCommand(插入图像(的ImagePath)VALUES('书'),CON))
{
com.ExecuteNonQuery();
}



我不知道为什么,但是这一次不给任何错误,语法(SQL)的罚款。然而,当我检查表中的数据,它仍然是空。这里是东西;



在相同的运行,结果
我执行该代码,然后
我执行另外一个是SELECT * FROM图片...结果
它显示了书。但尽管如此,该表数据是空的,当我重新运行它不插入,从图片只选择,它再次消失了。我真不明白是怎么回事。为什么我不能把任何东西在我的数据库?



我还添加了con.Close(),但它仍然无法正常工作。


解决方案

 的SqlCeConnection CON =新的SqlCeConnection(); 
con.ConnectionString = yeniApplicationDatabase.Properties.Settings.Default.DatabaseEdaConnectionString;
con.Open();
SqlCeCommand COM =新SqlCeCommand(插入图像(的ImagePath)VALUES('书'),CON);

com.ExecuteNonQuery();


com.Dispose();
con.Close();



关闭连接应完成插入。



编辑:该解决方案更新



数据库文件(。自卫队),你看哪个检查数据是否已经被插入。检查测试表在bin\Debug文件夹中。自卫队的内容。我相信,你的数据是存在于bin\Debug文件夹中的数据库文件正确插入



刚刚发现的堆栈溢出类似的问题:的Why我不能插入记录我的SQL Compact 3.5数据库?我坚信,你的问题是完全一样的。要检查错误的数据库文件。


I am doing a project on Visual Studio. I am using a local database (empty sql server compact edition). I chose Dataset and created my table (Images). It has a primary autoincrement id column, and an nvarchar ImagePath column. I want to insert data in it and here is my code.

SqlCeConnection con = new SqlCeConnection();
con.ConnectionString = yeniApplicationDatabase.Properties.Settings.Default.DatabaseEdaConnectionString;
con.Open();
using (SqlCeCommand com = new SqlCeCommand("INSERT INTO Images (ImagePath) VALUES ('book')", con))
{
  com.ExecuteNonQuery();
}

I don't know why but this one doesn't give any error, the syntax(SQL) is fine. However, when I check the table data, it is still null. Here is the thing;

In the same run,
I execute that code, then I execute another one which is select * from images...
It shows 'book'. But still, the table data is empty, and when I rerun it without inserting, only selecting from Images, it is gone again. I really don't understand what is going on. Why can't I put anything in my database?

I also added con.Close() but it still doesn't work.

解决方案

SqlCeConnection con = new SqlCeConnection();
con.ConnectionString =  yeniApplicationDatabase.Properties.Settings.Default.DatabaseEdaConnectionString;
con.Open();
SqlCeCommand com = new SqlCeCommand("INSERT INTO Images (ImagePath) VALUES ('book')", con);

com.ExecuteNonQuery();


com.Dispose();
con.Close();

Closing the connection should complete the insert.

EDIT: update on the solution

Which database file(.sdf) are you viewing to check whether the data has been inserted. check the content of the test table in the .sdf in the bin\Debug folder. I believe that your data is inserted properly in the database file which exist in bin\Debug folder.

Just found a similar question on stack overflow: Why can't I insert a record into my SQL Compact 3.5 database? and I firmly believe that your problem is exactly the same. you are checking the wrong database file.

这篇关于为什么我不能插入使用C#的数据到本地数据库(SQL精简版)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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