【问题标题】:I can't add records to the database我无法将记录添加到数据库
【发布时间】:2019-10-23 19:48:49
【问题描述】:

我正在开发一个应用程序,但遇到了这个问题。我正在尝试向我的 SQL 本地数据库添加一条记录。它给出了成功的消息框,但是当我关闭应用程序并检查表格时,记录不存在。我尝试以两种不同的方式添加它,但都没有奏效。我把代码贴在这里

这是我尝试的第一种方法

using (SqlConnection _conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Restaurant.mdf;Integrated Security=True"))
                {
                    using (SqlCommand _cmd = new SqlCommand("INSERT INTO Chelneri(username, nume, prenume, email, parola) values ('" + textBoxUser.Text.ToString().Trim() +"', '"+ textBoxNume.Text.ToString().Trim() +"', '"+ textBoxPrenume.Text.ToString().Trim() +"', '"+ textBoxEmail.Text.ToString().Trim() +"', '"+ textBoxParola.Text.ToString().Trim() +"')", _conn))
                    {
                        try
                        {
                            _conn.Open();
                            _cmd.ExecuteNonQuery();
                            _conn.Close();
                            textBoxUser.Text = string.Empty;
                            textBoxNume.Text = string.Empty;
                            textBoxPrenume.Text = string.Empty;
                            textBoxParola.Text = string.Empty;
                            textBoxEmail.Text = string.Empty;
                            MessageBox.Show("Utilizatorul a fost creat!", "Restaurant Casa Verde", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.ToString(), "Restaurant Casa Verde", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            _conn.Close();
                        }
                    }
                }

这是第二个。

using (SqlConnection _conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Restaurant.mdf;Integrated Security=True"))
                {
                    using (SqlCommand _cmd = new SqlCommand("INSERT INTO Chelneri(username, nume, prenume, email, parola) values (@user, @nume, @prenume, @email, @parola)", _conn))
                    {
                        try
                        {
                            _conn.Open();

                            _cmd.Parameters.Clear();
                            _cmd.Parameters.Add("@user", SqlDbType.NVarChar).Value = textBoxUser.Text.ToString().Trim();
                            _cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = textBoxEmail.Text.ToString().Trim();
                            _cmd.Parameters.Add("@nume", SqlDbType.NVarChar).Value = textBoxNume.Text.ToString().Trim();
                            _cmd.Parameters.Add("@prenume", SqlDbType.NVarChar).Value = textBoxPrenume.Text.ToString().Trim();
                            _cmd.Parameters.Add("@parola", SqlDbType.NVarChar).Value = textBoxParola.Text.ToString().Trim();

                            _cmd.ExecuteNonQuery();
                            _conn.Close();
                            textBoxUser.Text = string.Empty;
                            textBoxNume.Text = string.Empty;
                            textBoxPrenume.Text = string.Empty;
                            textBoxParola.Text = string.Empty;
                            textBoxEmail.Text = string.Empty;
                            MessageBox.Show("Utilizatorul a fost creat!", "Restaurant Casa Verde", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.ToString(), "Restaurant Casa Verde", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            _conn.Close();
                        }
                    }
                }

我不认为代码是问题所在。我认为我错过了一些东西,我不知道是什么。 这是数据库的打印屏幕。

【问题讨论】:

  • 我想你没有得到任何错误,是吗?我也看到没有交易。因此,如果插入没有抛出,则应该创建记录。这听起来可能很愚蠢,但您是否确认您正在连接到正确的数据库?
  • 此外,当您使用带有using 子句的SqlConnection 时,您不需要手动关闭它。

标签: c# sql database visual-studio tsql


【解决方案1】:

您好,我认为您的连接字符串错误 首先阅读此page 以从您的 sql 服务器获取真正的连接字符串 第二次阅读此page 以获取在 C# 中插入数据库的真实代码

我建议像这样使用 try catch

try
  {

  }
catch
  {

  }
finally { cnn.close}

希望对你有帮助

【讨论】:

  • 是的。连接字符串是问题所在。 Visual Studio 正在创建两组数据库,一组在实际项目文件夹中,另一组在调试文件夹中。我正在将记录添加到调试数据库中,并且正在检查另一个。非常感谢!
【解决方案2】:

我认为您的代码运行良好,但您缺少一个简单的触摸签入 Restaurant.mdf 正在与您的应用程序一起移动,如果没有,请执行以下步骤:

  1. 右键单击 Restaurant.mdf 并单击属性。
  2. 构建选项 -> 选择是否更新。

还要确保您正在检查正确的数据库文件。

如果您在调试模式下运行,请转到 Bin\Debug 文件夹并检查其中的 mdf 文件,如果您在发布模式下运行,则需要检查 Bin\Release 文件夹。

来源:codeproject

【讨论】:

  • 是的,这就是问题所在。我检查了错误的数据库。
【解决方案3】:

你使用的是“using”,你不需要_cmd.Close()

【讨论】:

    猜你喜欢
    • 2014-11-12
    • 2020-07-12
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    相关资源
    最近更新 更多