为什么我无法将记录插入 SQL Compact 3.5 数据库?
我刚刚使用 MSDN 文档制作了一个非常简单的测试应用程序。我想要做的就是将一条记录插入到我的表中,该表位于 VS 2010 应用程序的 SQL Server Compact 数据库中。
但是当我运行它时,它的表现就像执行得很好(没有错误),但是当我从服务器资源管理器查看表数据时,记录永远不会插入到我的 SQL Compact 3.5 数据库中。
我的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlCeConnection conn = null;
try
{
conn = new SqlCeConnection("Data Source = test.sdf; Password ='pass'");
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO TEST ([test]) Values('NWIND')";
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
}
}
我有一个名为 test.sdf 的数据库。它包含一个表“test”,其中有 1 列“test”。
我的示例测试应用程序位于: http://dl.dropbox.com/u/3051071 /ConsoleApplication1.zip
我已经测试了与 test.sdf 数据库的数据库连接,并且测试正常。
我无法弄清楚为什么我无法将记录插入到我的表中。每当我在执行后查看数据时,表中的值始终为空。
有人可以告诉我我在这里做错了什么吗?
谢谢。
I just made a very simple test app using documentation from MSDN. All I want to do is insert a record into my table which is in a SQL Server Compact database in my VS 2010 app.
But when I run it, it acts like it executes fine (no errors), but a record is never inserted into my SQL Compact 3.5 database when I go to view the table data from Server Explorer.
My code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlCeConnection conn = null;
try
{
conn = new SqlCeConnection("Data Source = test.sdf; Password ='pass'");
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO TEST ([test]) Values('NWIND')";
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
}
}
I have a database named test.sdf. It contains a table "test" with 1 column "test".
My sample test app is available at: http://dl.dropbox.com/u/3051071/ConsoleApplication1.zip
I have tested the database connection to my test.sdf database and that tests fine.
I can not figure for the life of my why I am unable to insert a record into my table. Whenever I view my data after execution, the value is always null in my table.
Can someone please tell me what I am doing wrong here?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在 SSMS 中查看哪个 .sdf 文件?我刚刚尝试过您的应用程序 - 检查 bin\Debug 文件夹中 .sdf 中的测试表的内容 - 它对我来说看起来很正确。
Which .sdf file are you viewing in SSMS? I've just tried your app - check the content of the test table in the .sdf in the bin\Debug folder - it looks right to me.
我将数据库移出 |DataDirectory|因为我对使用 Bin\Debug 文件夹中的数据库感到不放心...所以现在一切都很好:-)
因为在调试时所有内容都被移动到 Bin\Debug 文件夹:-/不知道:- (但现在很好,感谢大家提供
更多这里有更多信息
I Moved my database out of the |DataDirectory| because I didn't feel confy using the db in Bin\Debug Folder.... so now everything is fine :-)
because at the debugging time everything is moved to Bin\Debug folder :-/ didn't knew that :-( but now great, thankx fellas
for more Here is more info