无法在C#中将数据集更改保存到数据库 - Visual Studio 2013

时间:2015-01-26 19:28:57

标签: c# database visual-studio-2013 dataset

[解决] 解决方案:当您在visual studio中执行程序时,它不会将更改提交到数据库,而是可以使用副本。将程序部署到可执行文件时,此可执行文件可以永久修改您的数据库。希望这有助于任何人:)

正如我在这个问题HERE中所说,我无法将数据集的更改保存到我的数据库中。我试图遵循本教程HERE并且无法使其工作:程序编译并执行,但新数据未提交给DB。这是我在turorial之后写的代码。

//我注册WINDOWS表格中的方法

    public static SqlDataAdapter GetuserRecord()
    {
        SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\DatabaseJogo.mdf;Integrated Security=True");
        string query = "Select * from dbo.[user]";

        SqlCommand command = new SqlCommand(query, connection);
        connection.Open();
        SqlDataAdapter adp = new SqlDataAdapter(command);
        MessageBox.Show("CONNECTION SUCCESFUL");
        return adp;
    }

//当我的注册按钮被点击时:

            SqlDataAdapter adp = GetuserRecord();
            DataSet ds = new DataSet();

            adp.Fill(ds);

            DataRow newRow = ds.Tables[0].NewRow();
            newRow["login"] = loginText.Text.Trim();
            newRow["name"] = nameText.Text.Trim();
            newRow["age"] = int.Parse(ageText.Text.Trim());
            newRow["graphicsScore"] = trackBar1.Value;
            newRow["storyScore"] = trackBar2.Value;
            newRow["gameplayScore"] = trackBar3.Value;
            newRow["password"] = passwordText.Text.Trim();
            newRow["isAdmin"] = isAdmin.Checked;
            newRow["sex"] = sex.Text.Trim();

            ds.Tables[0].Rows.Add(newRow);

            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adp);

            adp.UpdateCommand = commandBuilder.GetUpdateCommand(true);
            adp.InsertCommand = commandBuilder.GetInsertCommand(true);
            adp.DeleteCommand = commandBuilder.GetDeleteCommand(true);

            adp.Update(ds);

编辑:将代码更改为新代码,但问题仍然是相同的。如果你想看一眼,那就是:

                //TRY 3 SQL
            //https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.commit(v=vs.110).aspx
            SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\DatabaseJogo.mdf;Integrated Security=True");
            connection.Open();

            SqlCommand command = connection.CreateCommand();
            SqlTransaction transaction;

            transaction = connection.BeginTransaction("SIGNUP");

            command.Connection = connection;
            command.Transaction = transaction;
            try
            {
                command.CommandText = "INSERT INTO [user] ([login], [name], [age], [graphicsScore], [storyScore], [gameplayScore], [password], [isAdmin], [sex]) VALUES (@login, @name, @age, @graphicsScore, @storyScore, @gameplayScore, @password, @isAdmin, @sex);";
                command.Parameters.Add("@login", SqlDbType.NChar, 50).Value = loginText.Text.Trim();
                command.Parameters.Add("@name", SqlDbType.NChar, 50).Value = nameText.Text.Trim();
                command.Parameters.Add("@age", SqlDbType.Int).Value = int.Parse(ageText.Text.Trim());
                command.Parameters.Add("@graphicsScore", SqlDbType.Int).Value = trackBar1.Value;
                command.Parameters.Add("@storyScore", SqlDbType.Int).Value = trackBar2.Value;
                command.Parameters.Add("@gameplayScore", SqlDbType.Int).Value = trackBar3.Value;
                command.Parameters.Add("@password", SqlDbType.NChar, 50).Value = passwordText.Text.Trim();
                command.Parameters.Add("@isAdmin", SqlDbType.Bit).Value = isAdmin.Checked;
                command.Parameters.Add("@sex", SqlDbType.NChar).Value = sex.Text.Trim();
                command.ExecuteNonQuery();

                transaction.Commit();
                MessageBox.Show("COMMITTED");
            }
            catch (Exception expt)
            {
                MessageBox.Show(expt.Message);
                try
                {
                    transaction.Rollback();
                }
                catch (Exception ex2)
                {
                    // This catch block will handle any errors that may have occurred 
                    // on the server that would cause the rollback to fail, such as 
                    // a closed connection.
                    MessageBox.Show("Rollback Exception Type: " + ex2.GetType());
                    MessageBox.Show("  Message: " + ex2.Message);
                }
            }
            //connection.UpdateDatabase(ds);

            connection.Close();

1 个答案:

答案 0 :(得分:0)

为了更新数据库上的数据,您的SqlDataAdapter需要设置其InsertCommand,UpdateCommand,DeleteCommand属性。

因此,请尝试以下代码:

 ds.Tables[0].Rows.Add(newRow);
 SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adp);
 adp.DeleteCommand = commandBuilder.GetDeleteCommand(true);
 adp.UpdateCommand = commandBuilder.GetUpdateCommand(true);
 adp.InsertCommand = commandBuilder.GetInsertCommand(true);
 adp.Update(ds.Tables[0]);
 //connection.UpdateDatabase(ds);
 connection.Close();

编辑: Update方法采用DataSet和表的名称。我们的DataSet是ds,它是我们在设置它时传递给UpdateDatabase方法的那个。在点之后,在数据集中键入表的名称。