繁体   English   中英

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

[英]Unable to insert data in C# sql database

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

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();
        }
    }
}

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

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

没有足够的细节来确切知道为什么数值没有显示在您的数据库中,但我建议您更改编码实践(如下所述),重新测试,然后尽可能详细地提供关于事物的确切位置失败了。

编码实践

您可以比您需要的更早地打开连接(这意味着您拥有的宝贵资源超过了必要的时间),并且您不会清除连接或命令。 它们实现了IDisposable,因此您必须在它们上调用Dispose()。 最简单的方法是使用using语句。

建议重写:

// Remove initialization of the SqlConnection in the form load event.

private void button1_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection())
    {
        con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\myworkers.mdf;Integrated Security=True;User Instance=True";
        con.Open();
        // Optional: MessageBox.Show("OPEN!");
        int x;
        using (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 dont see any changes in my table*/
            // Optional: MessageBox.Show(x.ToString());
        }
    }
}

ExecuteNonQuery方法在MSDN上解释如下:

Executes a Transact-SQL statement against the connection and returns
the number of rows affected.

如果返回值为1 ,则必须插入一条记录。

你忘了重新问上桌吗? 新记录不会自动显示在您的应用程序中。

您可以考虑运行SQL事件探查器以查看实际发送到您的数据库的内容。 您可以捕获SQL,然后尝试直接对数据库执行该代码。

“player_id”意味着如果插入失败则这是主键。

我还建议像其他人一样改变你的方法,以“使用”的方式,因为这将为你管理你的资源,并确保你不会留下连接“闲逛”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM