使用 C# 将记录永久插入到 sql server Express 版本中
我在sql server Express版本中创建了一个数据库,其中创建了一个名为employee的表。现在我可以成功地将行(记录)动态插入表中,并且我也可以成功读取这些记录。但问题是动态插入的值是临时存储的。当我关闭并重新打开应用程序时,以前插入的记录不可用。你能建议我做什么才能将记录永久保存到数据库中吗?
这是我用来将记录插入sql server 数据库的代码。请帮我解决这个问题...
namespace VACS_practice
{
public partial class Form1 : Form
{
string m_sVehicleNo, m_sName, m_sFlatNo, m_sImagpath;
System.Data.SqlClient.SqlConnection Con;
System.Data.SqlClient.SqlCommand Cmd;
string ConString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\VACSDB.mdf;Integrated Security=True;User Instance=True";
public Form1()
{
InitializeComponent();
}
private void btnAddClick(object sender, EventArgs e)
{
Con = new SqlConnection(ConString);
m_sVehicleNo = m_VehicleNo.Text;
m_sName = m_Name.Text;
m_sFlatNo = m_Phno.Text;
//m_sImagpath = m_ImgPath.Text;
Cmd = new SqlCommand("INSERT INTO ResidentDB ([R_VehNo],[R_Name],[R_PhNo]) VALUES ('" + m_sVehicleNo + "','" + m_sName + "','" + m_sFlatNo + "')", Con);
Con.Open();
Cmd.ExecuteNonQuery();
Con.Close();
MessageBox.Show("Inserted successfully");
// this.Close();
}
}
}
I created a database in sql server express edition, in that i create a table called employee. Now i am able to inserting rows(records) dynamically into table successfully, and i can also read those records successfully. But the problem is the values which are inserted dynamically are stored temporarily. When i close and reopen the application the previous inserted records are not available. can u please suggest me what can i do to save records permanently into the database.
This is my code used to inserting the records into sql server database. Please help me out of this problem...
namespace VACS_practice
{
public partial class Form1 : Form
{
string m_sVehicleNo, m_sName, m_sFlatNo, m_sImagpath;
System.Data.SqlClient.SqlConnection Con;
System.Data.SqlClient.SqlCommand Cmd;
string ConString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\VACSDB.mdf;Integrated Security=True;User Instance=True";
public Form1()
{
InitializeComponent();
}
private void btnAddClick(object sender, EventArgs e)
{
Con = new SqlConnection(ConString);
m_sVehicleNo = m_VehicleNo.Text;
m_sName = m_Name.Text;
m_sFlatNo = m_Phno.Text;
//m_sImagpath = m_ImgPath.Text;
Cmd = new SqlCommand("INSERT INTO ResidentDB ([R_VehNo],[R_Name],[R_PhNo]) VALUES ('" + m_sVehicleNo + "','" + m_sName + "','" + m_sFlatNo + "')", Con);
Con.Open();
Cmd.ExecuteNonQuery();
Con.Close();
MessageBox.Show("Inserted successfully");
// this.Close();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您使用的是 AttachDbFilename=|DataDirectory,所以我相信每次运行应用程序时可能会发生的情况是,数据库可能会获取 被解决方案中的“模板”.mdf 覆盖。确保您的数据库未部署为“始终复制”或类似方式。或者,如果您的应用程序不需要本地/每个用户数据库,您可以 将您的 MDF 永久附加到 SQL Express 服务下。
Because you are using AttachDbFilename=|DataDirectory, I believe what might be happening is that everytime you run your app, the database might be getting overwritten by your 'template' .mdf in your solution. Make sure that your database isn't deployed as "Copy Always" or similar. Alternatively, if your application doesn't need a local / per user database, you can permanently attach your MDF under the SQL Express service.