C# Windows Mobile的SQL Server CE数据库文件上未应用插入、更新和删除
我正在使用Visual Studio 2008用C开发一个Windows Mobile 6.5应用程序,它连接到SQL Server CE数据库 我使用此代码插入行,每列的值与文本框字符串相关:C# Windows Mobile的SQL Server CE数据库文件上未应用插入、更新和删除,c#,windows-mobile,sql-server-ce,C#,Windows Mobile,Sql Server Ce,我正在使用Visual Studio 2008用C开发一个Windows Mobile 6.5应用程序,它连接到SQL Server CE数据库 我使用此代码插入行,每列的值与文本框字符串相关: namespace GesTPL { public partial class Form4 : Form { public Form4() { InitializeComponent(); } pub
namespace GesTPL
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
public Form RefToForm1 { get; set; }
static String pathDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
static String pathDB = System.IO.Path.Combine(pathDir, "releve.sdf");
static String connectionString = string.Format(@"DataSource={0}", pathDB);
SqlCeConnection connection = new SqlCeConnection(connectionString);
private void Form4_Load(object sender, EventArgs e)
{
connection.Open();
}
private void button_Fermer_Click_1(object sender, EventArgs e)
{
this.RefToForm1.Show();
this.Close();
}
private void button_OK_Click_1(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox5.Text == "")
{
MessageBox.Show("Fill in all the information first!");
}
else
{
SqlCeCommand cmd = new SqlCeCommand("INSERT INTO compteur(numeroCompteur, idGeographique, abonne, police) VALUES(@numeroCompteur, @idGeographique, @abonne, @police)", connection);
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@numeroCompteur", textBox1.Text);
cmd.Parameters.AddWithValue("@idGeographique", textBox2.Text);
cmd.Parameters.AddWithValue("@abonne", textBox3.Text);
cmd.Parameters.AddWithValue("@police", textBox5.Text);
try
{
int affectedrows = cmd.ExecuteNonQuery();
if (affectedrows > 0)
{
MessageBox.Show("Data added successfully!");
}
else
{
MessageBox.Show("Failed to add data!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
问题是修改只应用于emulator中的数据库,而不应用于我的项目中的database.sdf文件。在项目中选择relve.sdf数据库文件,转到属性部分,并确保Build Action=Content和Copy to Output Directory=Copy(如果更新)
这应该是您所需要的全部内容,但如果存在其他问题,您可能需要再次检查文件位置:
private void button_OK_Click_1(object sender, EventArgs e)
{
MessageBox.Show(pathDB, "Check Path Location!");
// ... rest of your code
}
你所描述的行为是正常的。通过从VS执行WM项目,将二进制文件和内容文件.sdf(在本例中)复制到设备/仿真器。如果你想保留修改后的.sdf文件的副本,你必须自己从模拟器中手动提取。有没有办法从设备中自动提取数据库文件并替换项目中的原始文件?你可以使用itsutils远程工具:从ActiveSync连接的设备下载文件:他想去另一个设备我想是的。他希望将对设备数据库的更改返回到PC。当我第一次将数据库文件添加到我的项目中时,当我被要求时,我选择不将原始文件复制到项目目录中,因此带黄色图标的文件不会出现。我不知道这是否正确,但至少我可以看到我在模拟器中应用的修改。那么你的程序工作正常。如果您想在PC上查看数据库条目,则需要将该数据库从仿真器复制回PC。