无法在C#sql数据库中插入数据

qwr*_*qwr 2 c#

所以当我的表单加载时,它将连接到数据库,当我点击按钮时,它会在我的数据库中插入一个新行,但是在我点击它后我没有看到我的表中的任何更改.

namespace database
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SqlConnection con;

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new SqlConnection();
            con.ConnectionString =
              "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\myworkers.mdf;Integrated Security=True;User Instance=True";
            con.Open();
            MessageBox.Show("OPEN!");
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int x;
            SqlCommand thiscommand = con.CreateCommand();
            thiscommand.CommandText =
              "INSERT INTO player_info (player_id, player_name) values (10, 'Alex') ";

            x =  thiscommand.ExecuteNonQuery();  /* I used variable x to verify
                                                    that how many rows are
                                                    affect, and the return is 1.
                                                    The problem is that I don't
                                                    see any changes in my table*/
            MessageBox.Show(x.ToString());
            con.Close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Hen*_*man 9

您正在使用附加的Db文件.这意味着Project-build在Bin文件夹中创建一个副本,并且您正在处理两个不同版本的Db.你可能正在检查原件.

这实际上非常有用(每次都是新的副本).如果没有,请更改Db文件的Copy-when属性.