从一个表中选择并插入另一个表 [英] Selecting from one Table and insert into another

查看:81
本文介绍了从一个表中选择并插入另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三个表的数据库。表1,表2和表3。当代码运行并且用户输入其用户名和密码时,它将保存到Table3中。 Table3包含用户名,密码,UserID和Level。我无法从Table1和Table2获取UserID和Level3到Table3。代码现在的方式是用户可以设置新用户,并且该用户信息保存在Table3中但没有UserID和Level。有没有办法可以纠正这个?我做错了什么?



I have a database with three tables. Table1, Table2 and Table3. When the code is running and the user puts in their username and password it is then saved into Table3. Table3 holds the username, password, UserID and Level. I am having trouble getting the UserID and Level to Table3 from Table1 and Table2. The way the code is now the user can setup a new user and that user information is saved into Table3 but without the UserID and Level. Is there a way I can correct this? What am I doing wrong?

protected void Submit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con.Open();
        string cmdStr = "Select INST_ID, accessLevel, EmailAddress from TableCEO where EmailAddress='" + TextBoxEA.Text + "'";
        string cmdStr2 = "Select INST_ID, accessLevel, EmailAddress from TableIALO where EmailAddress='" + TextBoxEA.Text + "'";
        string insCmd = "Insert into TableSecurity (EmailAddress, Password, accessLevel) values (@EmailAddress, @Password, @accessLevel)";
        string insCmd2 = "Insert into TableSecurity (EmailAddress, Password, accessLevel) values (@EmailAddress, @Password, @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("@accessLevel", TextBoxEA.Text);
        


        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            lblMessage.Text = "User Already Exist";
        }
        finally
        {
        }
    }
   
}

推荐答案

查看你的代码...

Have a look at your code...
insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
insertUser.Parameters.AddWithValue("@accessLevel", TextBoxEA.Text);





您正在尝试插入accessLevel TextBoxEA.Text - 电子邮件地址。如果将accessLevel定义为整数,则不会通过传递字符串来获得任何保存。我怀疑你并不是故意这样做,并且惊讶于DBMS没有抛出异常



You are trying to insert into accessLevel TextBoxEA.Text - the email address. If accessLevel has been defined as an integer you're not going to get anything saved by passing it a string. I suspect you didn't mean to do that and surprised that no exception was thrown by the DBMS


这篇关于从一个表中选择并插入另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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