为什么将更改保存到数据库失败?下面是控制台应用程序中的C#代码。每当我调试应用程序并运行query1(它向数据库中插入一个新值),然后运行query2(它显示数据库中的所有条目),我就可以清楚地看到我插入的新条目。但是,当我关闭应用程序并检查数据库中的表(在VisualStudio中)时,它就消失了。我不知道它为什么不储蓄。using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;
using System.Data.SqlServerCe;using System.Data;namespace ConsoleApplication1{
class Program
{
static void Main(string[] args)
{
try
{
string fileName = "FlowerShop.sdf";
string fileLocation = "|DataDirectory|\\";
DatabaseAccess dbAccess = new DatabaseAccess();
dbAccess.Connect(fileName, fileLocation);
Console.WriteLine("Connected to the following database:\n"+fileLocation + fileName+"\n");
string query = "Insert into Products(Name, UnitPrice, UnitsInStock) values('NewItem', 500, 90)";
string res = dbAccess.ExecuteQuery(query);
Console.WriteLine(res);
string query2 = "Select * from Products";
string res2 = dbAccess.QueryData(query2);
Console.WriteLine(res2);
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadLine();
}
}
}
class DatabaseAccess
{
private SqlCeConnection _connection;
public void Connect(string fileName, string fileLocation)
{
Connect(@"Data Source=" + fileLocation + fileName);
}
public void Connect(string connectionString)
{
_connection = new SqlCeConnection(connectionString);
}编辑:忘记提到我正在使用SQLServerCompactEdition 4和VS 2012 Express。
添加回答
举报
0/150
提交
取消