简体   繁体   English

2个用户以一种形式注册C#winforms

[英]2 users registration in one form C# winforms

I want to do a game with player1 Vs. 我想用player1 vs做游戏。 player2 and need to register them to DB first. player2,需要先将它们注册到数据库。 I have one form with panel for each user's details and one submit button, so when the two users fill their details they submit them in the same form. 我有一个包含每个用户详细信息的面板的表单和一个提交按钮,因此当两个用户填写其详细信息时,他们将它们提交到同一表单中。 and I have one Users table in DB, so all registered users should be there. 我在DB中有一个Users表,因此所有注册用户都应该在那儿。 but when i add it to the DB table, i get exception that the @username already exist (and the same for all parameters in table). 但是,当我将其添加到数据库表中时,出现了@username已经存在的异常(表中的所有参数都相同)。

is it possible to register two users in one form to one table ? 是否可以将两种用户以一种形式注册到一张桌子?

this is the code for the submit button. 这是提交按钮的代码。 it works for one user.. 它适用于一个用户。

private void submitBtn_Click(object sender, EventArgs e)
{
    if (registerValidation())
    {
        string conStr = ConfigurationManager.ConnectionStrings["BackgammonGame"].ConnectionString;
        SqlConnection con = new SqlConnection(conStr);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        con.Open();

        cmd.CommandText = ("INSERT INTO Users (userName, firstName, lastName, address, addNum, city, phone, email) VALUES (@userName, @firstName, @lastName, @address, @addNum, @city, @phone, @email)");

        cmd.Parameters.Add("@userName", SqlDbType.NVarChar).Value = userNmTxt.Text;
        cmd.Parameters.Add("@firstName", SqlDbType.NVarChar).Value = firstNmTxt.Text;
        cmd.Parameters.Add("@lastName", SqlDbType.NVarChar).Value = lastNmTxt.Text;
        cmd.Parameters.Add("@address", SqlDbType.NVarChar).Value = streetTxt.Text;
        cmd.Parameters.Add("@addNum", SqlDbType.Int).Value = (string.IsNullOrWhiteSpace(stNumTxt.Text)) ? 0 : Int32.Parse(stNumTxt.Text);
        cmd.Parameters.Add("@city", SqlDbType.NVarChar).Value = cityTxt.Text;
        cmd.Parameters.Add("@phone", SqlDbType.Int).Value = (string.IsNullOrWhiteSpace(phoneNumTxt.Text)) ? 0 : Int32.Parse(phoneNumTxt.Text);
        cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = mailTxt.Text;

        cmd.ExecuteNonQuery();
        MessageBox.Show("User successfully created!");
        con.Close();
        Close();
        Form1.ActiveForm.Close();
    }
}

I suspect that the problem you are having is that your button click event is just using the same textboxes which relate to the first player and then reusing these for the second player (which obviously still points to player one). 我怀疑您遇到的问题是您的按钮单击事件只是使用了与第一个玩家相关的相同文本框,然后为第二个玩家重用了它们(显然仍然指向玩家一个)。

Your best idea would be to either make a method that will take in some parameters or create a user control for your panels that you could use to add to db.. You will still need checking for users that exist though. 最好的想法是制作一个可以接受某些参数的方法,或者为面板创建一个用户控件,然后将其添加到数据库中。您仍然需要检查是否存在用户。

Solution One 解决方案一

private void submitBtn_Click(object sender, EventArgs e)
{
    AddPlayerToDb("name","add"....);
    AddPlayerToDb("name2","add2"....);

//Inside method
cmd.Parameters.Add("@userName", SqlDbType.NVarChar).Value = param1;

Solution two 解决方案二

private void submitBtn_Click(object sender, EventArgs e)
{
    userControl1.AddPlayerToDb(connection);
    userControl2.AddPlayerToDb(connection);

Note I'm not familiar with SQL too much so I don't know if its valid to pass a connection through or if you need a new connection 注意:我对SQL不太熟悉,因此我不知道它是否可以通过连接有效,或者您是否需要新的连接

I think you should check if the user exists in the datatable before saving a new one. 我认为您应该在保存新表之前检查用户是否存在于数据表中。 Than you can update the user data, overwrite it or ignor the new data. 比您可以更新用户数据,覆盖它或忽略新数据。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM