【发布时间】:2015-08-19 10:39:22
【问题描述】:
[已解决] 解决方案:当您在 Visual Studio 中执行程序时,它不会将更改提交到数据库,而是使用副本。当您将程序部署到可执行文件时,该可执行文件能够永久修改您的数据库。希望这对任何人都有帮助:)
正如我在这个问题HERE 中所说,我无法将数据集的更改保存到我的数据库中。我尝试按照本教程HERE 进行操作,但也无法正常工作:程序编译并执行,但新数据未提交给数据库。这是我在教程之后编写的代码。
//我的注册窗口表单中的方法
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();
【问题讨论】:
-
您是否提供了与数据库表列名相似的正确列名
newRow["login"]-login- 这与表列名相似吗?列名可能区分大小写,因此请尝试给出确切的列名 -
是的,我很高兴。使用此代码注册表格后,我可以登录我的帐户并查看我在注册屏幕中输入的所有详细信息。唯一的问题是,一旦我关闭程序,新用户就会从数据库中删除。
-
你能在 try/catch 块中包装一下,看看你是否在那个更新上遇到了异常吗?
-
@ragerory 尝试使用 try { adp.Update(ds.Tables[0]); } catch(Exception expt) { MessageBox.Show(expt.Message); } 并且没有抛出异常!
标签: c# database visual-studio-2013 dataset