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

时间:2012-03-25 20:09:56

标签: c#

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

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

4 个答案:

答案 0 :(得分:9)

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

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

答案 1 :(得分:1)

没有足够的细节来确切知道数值未显示在数据库中的原因,但我建议您更改编码实践(如下所述),重新测试,然后提供尽可能详细的信息。确切地说,事情正在失败。

编码实践

您可以比您需要的更早地打开连接(这意味着您拥有的宝贵资源超过了必要的时间),并且您不会清除连接或命令。它们实现了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());
        }
    }
}

答案 2 :(得分:0)

ExecuteNonQuery方法在MSDN上解释如下:

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

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

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

答案 3 :(得分:0)

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

“player_id”表示如果插入失败则这是主键。

我还建议改变你的方法,就像其他人一样,改为“使用”风格,因为这将为你管理你的资源,并确保你不要让连接“闲逛”。