【问题标题】:Database not storing information properly, stores info on different rows数据库未正确存储信息,将信息存储在不同的行上
【发布时间】:2014-04-26 19:26:43
【问题描述】:

数据库未将信息全部存储在同一行上。在第一页上,当我单击按钮时,它会记录它,这很好,它被存储了。然后在下一页上,当我单击按钮时,它会存储信息,但在不同的行上?有什么解决办法吗?这是问题所在,代码如下。


第 1 页

public void addInformationToDatabase()
    {
        string Sex = ddlGender.Text;
        string Name = tbxName.Text;
        string DOB = tbxDOB.Text;

        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection Con = new SqlConnection(connectionString);

        SqlCommand command = new SqlCommand();
        command.Connection = Con;
        command.CommandType = CommandType.Text;
        command.CommandText = "INSERT INTO [User] (GenderID,Name,DOB) VALUES(@Sex,@Name,@DOB)";

        command.Parameters.AddWithValue("@Sex", Sex);
        command.Parameters.AddWithValue("@Name", Name);
        command.Parameters.AddWithValue("@DOB", DOB);

        try
        {
            Con.Open();
            command.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            Con.Close();
        }
    }

第二页

public void save()
    {

        string checkboxSelection = CheckBoxList1.SelectedItem.ToString();

        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection Con = new SqlConnection(connectionString);

        SqlCommand c = new SqlCommand();
        c.Connection = Con;
        c.CommandType = CommandType.Text;
        c.CommandText = "INSERT INTO [User] (Ans1) VALUES(@Ans1)";

        c.Parameters.AddWithValue("@Ans1", checkboxSelection);

        try
        {
            Con.Open();
            c.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            Con.Close(); 
        }
    }

任何帮助表示赞赏

【问题讨论】:

  • 快速检查 - 您想在第一页为用户创建记录,并在第二页更新该用户的行吗?
  • @shree.pat18 是的,没错,但是这个信息会在下面的行中更新并生成一个新的 id,我不知道如何解决这个问题
  • 在这种情况下,您需要在第二页上使用UPDATE 查询。您需要查看哪些列可用于标识要更新的记录,在页面之间传递该值,并在更新查询中添加 WHERE 子句以仅更新该记录。
  • 我明白这一点,但我不明白的是 WHERE 子句,因为它应该知道它的更新位置,因为我在输入它的页面上对其进行编码?
  • SQL 语句不是这样工作的。它们都是相互独立的。它们没有记忆,每个 SQL 语句都应该有自己的“where”语句。它们不会被重复使用。

标签: c# asp.net sql sql-server database


【解决方案1】:

您的第一页需要在插入后取回 ID,然后您的第二页需要根据该 ID 执行 update,而不是后续插入。

有很多关于找回 id 的资源 - 例如 How to get last inserted id?

(我假设 id 字段唯一标识您的行)

第一个查询 -

 c.CommandText = "INSERT INTO [User] (Ans1) VALUES(@Ans1); SELECT SCOPE_IDENTITY()";
 ...
  int userID = (Int32) c.ExecuteScalar();

您需要将该 ID 传递到您的第二页并将插入内容更改为更新:

"UPDATE User] SET Ans1 = @Ans1 WHERE Id = @id";

您还需要将 id 添加为参数

c.Parameters.AddWithValue("@id", userID);

【讨论】:

  • c.CommandText = "INSERT INTO [User] (Ans1) OUTPUT INSERTED.ID VALUES(@Ans1)";这就是我现在拥有的?
  • 仍然无法正常工作,我认为我做得不对。可以吗?
  • 我现在有这个 c.CommandText = "UPDATE [User] SET [Ans1] = @Ans1";并将整个列更新为选定的 ans1
  • 那是因为你需要 where 子句 - 说只更新 ID = xx 的地方
  • 好的,我明白你的意思,但我希望它在用户选择它时自动更新,我不能让 Id = 1,然后下一个 peson 可能是 2,这不会更新?
猜你喜欢
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 2012-08-03
  • 2010-12-05
  • 1970-01-01
  • 2015-10-08
  • 2011-05-29
  • 2021-09-17
相关资源
最近更新 更多