多个用户如何在不混合数据库数据的情况下同时将数据插入SQL数据库

本文关键字:数据 数据库 情况下 SQL 插入 混合 用户 | 更新日期: 2023-09-27 18:26:50

我在asp和C#下开发了一个应用程序。该应用程序的想法是让多个用户登录到该应用程序,并同时将数据插入SQL数据库服务器2008sp2。用户创建一个主题,然后插入关于该主题的描述,单个用户可以毫无问题地插入任何描述数据,但当多个用户插入数据时,数据不会保存,或者数据在数据库中混合。例如,如果用户一为主题1插入数据:User1 User1.1,用户二为主题2插入数据:用户2 User2.1,则用户的一个数据保存在用户2的主题上。请注意,数据库处于多用户模式。。。。你能用例子帮我解决这个问题吗?提前感谢您…

        This Is My C# Code : 
  public void dataInsert()
{
    try
    {
        SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["beta"].ConnectionString;
        // storeMulti();
        for (int i = 0; i < howManyFields(); i++)
        {
            TextBox txt = Theat.FindControl("TextBox" + (i + 1)) as TextBox;
            HtmlTableCell cell = Theat.FindControl("Td" + (i + 1)) as HtmlTableCell;
            CheckBox cb = Theat.FindControl("CheckBox" + (i + 1)) as CheckBox;
            TextBox tTitle = Theat.FindControl("tbTitle" + i) as TextBox;
            TextBox tDscr = Theat.FindControl("tbDscr" + i) as TextBox;
            DropDownList dConf = Theat.FindControl("ddConf" + i) as DropDownList;
            DropDownList dType = Theat.FindControl("ddType" + i) as DropDownList;
            HtmlInputFile inFile = Theat.FindControl("tbFile" + i) as HtmlInputFile;
            if (txt.Text.Trim().ToString() != string.Empty)
            {
                //ShowMessageBox("Updated");
                DataSet dsPres = findPresDatasetByLang(Convert.ToInt32(Application["langID"]));
                for (int z = 0; z < dsPres.Tables[0].Rows.Count; z++)
                {
                    System.Data.SqlClient.SqlCommand cmd;
                    cmd = new System.Data.SqlClient.SqlCommand();
                    cmd.Connection = con;                       
                    cmd.CommandText = "INSERT INTO [I_SUBJECT_FIELD] VALUES (@p1,@p2,@p3,@p4,@p5)";
                    cmd.Parameters.AddWithValue("@p1", Convert.ToInt32(Application["subID"].ToString()));
                    cmd.Parameters.AddWithValue("@p2", Convert.ToInt32(dsPres.Tables[0].Rows[z]["ID"].ToString()));
                    cmd.Parameters.AddWithValue("@p3", Convert.ToInt32(Application["langID"].ToString()));
                    cmd.Parameters.AddWithValue("@p4", findFieldTypeID(cell.InnerHtml));
                    cmd.Parameters.AddWithValue("@p5", txt.Text);

                    cmd.Connection.Open();
                    int rowsAffected = cmd.ExecuteNonQuery();
                    cmd.Connection.Close();
                }
            }
               txt.Text = string.Empty;
               cb.Checked = false;
        }
    }
    catch (SqlException sql)
    {
        ShowMessageBox(sql.Message);
    }
    catch (Exception exe)
    {
        ShowMessageBox(exe.Message);
    }
}

多个用户如何在不混合数据库数据的情况下同时将数据插入SQL数据库

看看从哪里获得查询的参数值:

cmd.Parameters.AddWithValue("@p1", Convert.ToInt32(Application["subID"].ToString()));
cmd.Parameters.AddWithValue("@p3", Convert.ToInt32(Application["langID"].ToString()));

您正在从Application值集合中获取它们。这不是线程安全的。应用程序的所有并发用户共享相同的Application值集合。因此,当一个用户更新该集合中的值时,其他用户将使用该值。

插入数据库的值应提供给此函数,而不是在全局Application集合中引用。因此,函数签名可能看起来更像这样:

public void dataInsert(int subjectId, int languageId)
{
    // implementation
}

查询参数将来自这些值:

cmd.Parameters.AddWithValue("@p1", subjectId);
cmd.Parameters.AddWithValue("@p3", languageId);

这样,每当用户调用此功能时,调用它的代码只会提供这些值,而不是将它们存储在全局集合中。

一般来说,避免使用全局值特别是在具有多个并发用户的多线程应用程序中。