【发布时间】:2013-08-07 20:44:47
【问题描述】:
我正在使用 Visual Studio 2008 使用 C# 开发 Windows Mobile 6.5 应用程序,它连接到 SQL Server CE 数据库。
我用这段代码插入行,每列的值与一个文本框字符串相关:
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);
}
}
}
}
}
问题是修改只应用于模拟器中的数据库,而不是我项目中的数据库.sdf文件。
【问题讨论】:
-
您描述的行为是预期的和正常的。通过从 VS 执行 WM 项目,二进制文件和内容文件(在本例中为 .sdf)被复制到设备/模拟器。如果您想保留修改后的 .sdf 文件的副本,您必须自己从模拟器中手动提取它。
-
有没有办法自动从设备中提取数据库文件并替换项目中的原始文件?
-
您可以使用 itsutils(远程工具:forum.xda-developers.com/wiki/XdaUtils)从 ActiveSync 连接的设备下载文件:hjgode.de/wp/2010/02/26/…
标签: c# windows-mobile sql-server-ce