开发者

Inserting records permanetly into sql server express edition using c#

开发者 https://www.devze.com 2023-04-03 14:58 出处:网络
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 re

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开发者_如何学C_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();
        }
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号