C# 如何让多个用户同时向SQL数据库插入数据,而不混合数据库中的数据。

C# 如何让多个用户同时向SQL数据库插入数据,而不混合数据库中的数据。,c#,asp.net,sql-server,database,C#,Asp.net,Sql Server,Database,我在asp和C#下开发了一个应用程序。该应用程序的思想是让多个用户登录到该应用程序,并同时将数据插入SQL数据库服务器2008 sp2。 用户创建一个主题,然后插入关于该主题的描述,单个用户可以毫无问题地插入任何描述数据,但当多个用户插入数据时,数据不会保存或数据在数据库中混合。 例如,如果用户1为主题1插入数据:User1 User1.1,用户2为主题2插入数据:User2 User2.1,则用户1的数据保存在users2的主题上。 请注意,数据库处于多用户模式。。。。 你能举例帮我解决这个问

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

        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);
    }
}
这是我的C代码:
公共无效数据插入()
{
尝试
{
SqlConnection con=new System.Data.SqlClient.SqlConnection();
con.ConnectionString=ConfigurationManager.ConnectionString[“beta”]。ConnectionString;
//storeMulti();
对于(int i=0;i
查看查询参数值的获取位置:

cmd.Parameters.AddWithValue("@p1", Convert.ToInt32(Application["subID"].ToString()));
cmd.Parameters.AddWithValue("@p3", Convert.ToInt32(Application["langID"].ToString()));
您将从
应用程序
值集合中获取它们。这不是线程安全的。应用程序的所有并发用户共享相同的
应用程序
值集合。因此,当一个用户更新该集合中的值时,其他用户将使用该值

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

public void dataInsert(int subjectId, int languageId)
{
    // implementation
}
查询参数将来自这些值:

cmd.Parameters.AddWithValue("@p1", subjectId);
cmd.Parameters.AddWithValue("@p3", languageId);
这样,每当用户调用此功能时,调用该功能的代码都会简单地提供这些值,而不是将它们存储在全局集合中


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

谢谢David的评论,非常感谢。我将尝试将您的建议应用到我的代码中。再次感谢你。