[英]Insert, Update, Delete are not applied on SQL Server CE database file for Windows Mobile
我正在使用Visual Studio 2008用C#开发Windows Mobile 6.5应用程序,它已连接到SQL Server CE数据库。
我使用此代码插入行,每一列的值都与textBox字符串有关:
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
文件。
在项目中选择releve.sdf
数据库文件,转到“ 属性”部分,并确保“ Build Action = Content
和“ Copy to Output Directory = Copy if newer
。
那应该是您所需要的,但是如果还有其他问题,您可能需要仔细检查文件位置:
private void button_OK_Click_1(object sender, EventArgs e)
{
MessageBox.Show(pathDB, "Check Path Location!");
// ... rest of your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.