简体   繁体   English

多个用户如何同时将数据插入SQL数据库而又不将数据混合到数据库中。

[英]How can more than one user Insert data to the SQL database at the same time without mix the data on the database.

Well I develop an application under the asp and C#. 好吧,我在ASP和C#下开发了一个应用程序。 The idea of this application is to multiple users log in to the app and inserts data to the SQL database server 2008 sp2 at the same time. 此应用程序的想法是让多个用户登录到该应用程序,并将数据同时插入到SQL数据库服务器2008 sp2中。 An user create a subject and then inserting description about this subject , a single user can insert any description data without any problem but when more than one users inserting data then the data does not saved or the data mixed on the databases. 用户创建一个主题,然后插入有关该主题的描述,单个用户可以毫无问题地插入任何描述数据,但是当一个以上的用户插入数据时,该数据将不会保存或在数据库中混合在一起。 For Example if user one inserting for the Subject 1 the data: User1 User1.1 and User two inserting for the Subject 2 the data: User2 User2.1 then the user's one data saved on the subject of users2. 例如,如果用户1为主题1插入数据:User1 User1.1,而用户2为主题2插入数据:User2 User2.1,则用户的一个数据保存在用户2的主题上。 Note that the databases are on Multiuser Mode.... Could you please help me with this problem with examples? 请注意,数据库处于多用户模式下。...能否通过示例为我解决这个问题? Thank you in advanced … 谢谢高级……

        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);
    }
}

Look at where you're getting parameter values for the query: 查看从哪里获取查询的参数值:

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

You're getting them from the Application value collection. 您是从“ Application值集合中获取它们的。 This is not thread safe . 不是线程安全的 All concurrent users of the application share the same Application value collection. 应用程序的所有并发用户共享相同的Application值集合。 So when one user updates a value in that collection, other users will use that value. 因此,当一个用户更新该集合中的值时,其他用户将使用该值。

The values being inserted to the database should be supplied to this function, not referenced in the global Application collection. 插入数据库的值应提供给此功能,而不是在全局Application集合中引用。 So the function signature might look more like this: 因此,函数签名可能看起来像这样:

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

And the query parameters would come from those values: 查询参数将来自这些值:

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

That way whenever a user invokes this functionality, the code invoking it would simply provide those values instead of storing them in a global collection. 这样,每当用户调用此功能时,调用该功能的代码将只提供这些值,而不是将其存储在全局集合中。

In general, avoid global values. 通常,避免使用全局值。 Especially in multi-threaded applications with multiple concurrent users. 特别是在具有多个并发用户的多线程应用程序中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 同时在数据库中插入多行c# - Insert more than one row in database at the same time c# 如何在SQL数据库中显示出现次数超过3次的列数据 - How to display a column data which appear more than 3 times in the sql database without duplication 如何汇总数据库中给定日期的多个时间记录。 错误:子查询返回的值超过1 - How to sum up multiple time records on a given date in database. Error:Subquery returned more than 1 value 如何在一个aspx页面上使用多个用户控件,而又不只在一个控件中进行相同的数据库调用? - How do I use multiple user controls on an aspx page without making the same database calls in more than one control? 将datagridveiw数据插入sql数据库。 “解析查询时出错” - Inserting datagridveiw data into sql database. “there was an error parsing the query” 如何将数据插入数据库? - 用户定义的类 - How to Insert Data to the Database? - User Defined Classes 如何将数据添加到sql但具有一个id和多个数据? - How to add data to sql but for one id and more than one data? 多重访问数据库。 如何防止单独的线程修改相同的数据? - Multiple access to database. How to prevent separate threads from modifying the same data? 如何将数据插入SQL Server数据库 - How to insert Data into sql server database 如何连接到 SQL SERVER 并将数据插入数据库? - How to connect to SQL SERVER and insert data into database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM