为什么我的数据库中有双重条目? [英] Why do I get a double entry in my database?

查看:61
本文介绍了为什么我的数据库中有双重条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我注册一个新用户时,我的数据库中有一个双重条目。我输入我编写的用户名和密码,然后单击提交。该部分有效,但当我检查数据库时,我看到用户名两次。为什么?我做错了什么?



<前lang =c#> 受保护 void Submit_Click( object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ HotConnectionString ] .ConnectionString);
con.Open();

string cmdStr = 从Table1中选择INST_ID,accessLevel,EmailAddress,其中EmailAddress =' + TextBoxEA.Text + '< /跨度>;
string cmdStr2 = 选择INST_ID,accessLevel ,表2中的EmailAddress,其中EmailAddress =' + TextBoxEA.Text + ';
string insCmd = 插入表22( EmailAddress,Password,INST_ID,accessLevel)值(@ EmailAddress,@ Password,@ INST_ID,@ accessLevel);
string insCmd2 = 插入表22( EmailAddress,Password,INST_ID,accessLevel)值(@ EmailAddress,@ Password,@ INST_ID,@ accessLevel);

SqlCommand insertUser = new SqlCommand(insCmd,con);
SqlCommand insertUser2 = new SqlCommand(insCmd2,con);

insertUser.Parameters.AddWithValue( @ EmailAddress,TextBoxEA.Text );
insertUser.Parameters.AddWithValue( @ Password,TextBoxPW.Text);
insertUser.Parameters.AddWithValue( @ INST_ID,TextBoxINST_ID.Text);
insertUser.Parameters.AddWithValue( @ accessLevel,TextBoxaccessLevel.Text);

insertUser2.Parameters.AddWithValue( @ EmailAddress,TextBoxEA.Text );
insertUser2.Parameters.AddWithValue( @ Password,TextBoxPW.Text);
insertUser2.Parameters.AddWithValue( @ INST_ID,TextBoxINST_ID.Text);
insertUser2.Parameters.AddWithValue( @ accessLevel,TextBoxaccessLevel.Text);

try
{
insertUser.ExecuteScalar();
insertUser2.ExecuteScalar();
con.Close();
Response.Redirect( Login.aspx);
}
catch (例外)
{

}
}

解决方案

嗯,你为什么这么想? :笑:

您正在设置两个相同的INSERT命令,并在相同的SqlConnection上依次执行它们。那么......你是否希望第二次插入不同的值?


不是很明显吗?



你正在制作两个完全相同的插入命令,然后你们两个都执行它们。你为什么两次在那里? insertUser1和insertUser2?


在查看它并从我之前使用过的其他代码中做了一些更改后,我得到了它的工作。我知道我有两次,但我遗漏了我在其他代码中的另一个连接。这是我更新的代码:



  protected   void  Submit_Click( object  sender,EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ HotConnectionString ] .ConnectionString);
con.Open();

string cmdStr = 从Table1中选择INST_ID,accessLevel,EmailAddress,其中EmailAddress =' + TextBoxEA.Text + '< /跨度>;
string cmdStr2 = 选择INST_ID,accessLevel ,表2中的EmailAddress,其中EmailAddress =' + TextBoxEA.Text + ';
string insCmd = 插入表22( EmailAddress,Password,INST_ID,accessLevel)值(@ EmailAddress,@ Password,@ INST_ID,@ accessLevel);

SqlCommand insertUser = new SqlCommand(insCmd,con);

insertUser.Parameters.AddWithValue( @ EmailAddress,TextBoxEA.Text );
insertUser.Parameters.AddWithValue( @ Password,TextBoxPW.Text);
insertUser.Parameters.AddWithValue( @ INST_ID,TextBoxINST_ID.Text);
insertUser.Parameters.AddWithValue( @ accessLevel,TextBoxaccessLevel.Text);



try
{
insertUser.ExecuteScalar();
con.Close();
Response.Redirect( Login.aspx);
}
catch (例外)
{

}
}


I get a double entry in my database when I signup a new user. I enter the user name and password that I made up and click submit. That part works but when i check the database I see the username twice. Why? What did I do wrong?

protected void Submit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();

        string cmdStr = "Select INST_ID, accessLevel, EmailAddress from Table1 where EmailAddress='" + TextBoxEA.Text + "'";
        string cmdStr2 = "Select INST_ID, accessLevel, EmailAddress from Table2 where EmailAddress='" + TextBoxEA.Text + "'";
        string insCmd = "Insert into Table22 (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";
        string insCmd2 = "Insert into Table22 (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";

        SqlCommand insertUser = new SqlCommand(insCmd, con);
        SqlCommand insertUser2 = new SqlCommand(insCmd2, con);

        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        insertUser.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        insertUser.Parameters.AddWithValue("@accessLevel", TextBoxaccessLevel.Text);

        insertUser2.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser2.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        insertUser2.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        insertUser2.Parameters.AddWithValue("@accessLevel", TextBoxaccessLevel.Text);

        try
        {
            insertUser.ExecuteScalar();
            insertUser2.ExecuteScalar();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            
        }
    }

解决方案

Well, why do you think? :laugh:
You are setting up two identical INSERT commands, and executing them on the same SqlConnection one after the other. So...did you expect to get different values inserted the second time?


Isn't it obvious?

You are making two insert commands that are EXACTLY the same, then you are executing them both. Why do you have it in there twice? insertUser1 and insertUser2?


After looking at it and making some changes from my other code I used before, I got it to work. I knew I had it twice but I left out the other connection I had in my other code. Here is my updated code:

protected void Submit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();
 
        string cmdStr = "Select INST_ID, accessLevel, EmailAddress from Table1 where EmailAddress='" + TextBoxEA.Text + "'";
        string cmdStr2 = "Select INST_ID, accessLevel, EmailAddress from Table2 where EmailAddress='" + TextBoxEA.Text + "'";
        string insCmd = "Insert into Table22 (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";
        
        SqlCommand insertUser = new SqlCommand(insCmd, con);
 
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        insertUser.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        insertUser.Parameters.AddWithValue("@accessLevel", TextBoxaccessLevel.Text);
 
        
 
        try
        {
            insertUser.ExecuteScalar();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            
        }
    }


这篇关于为什么我的数据库中有双重条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆