从一个表中选择并保存到同一数据库中的另一个表 [英] Selecting from one Table and saving to another Table in the same database

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

问题描述

我在一个数据库中有两个表。我有一个像这样工作的表单。用户可以输入电子邮件地址和密码,然后确认密码,然后单击提交按钮并重定向回登录屏幕。我的代码不起作用。我从表中选择正确的字段并将它们插入到新表中。为什么这不起作用?表1已包含数据。表2是我想要保存数据的地方。所以,如果Table1有INST_ID,Firstname,LastName,Emailaddress,电话号码。我从Table1中选择INST_ID和Emailaddress并使用密码将其插入Table2。它不会插入表2,为什么?



I have two Tables in one database. I have a form that works like this. The user can put in an email address and password then confirm password then click the submit button and be redirected back to the login screen. The code I have is not working. I am selecting the right fields from the table and inserting them into a new table. Why is this not working? Table1 has the data already in it. Table2 is where I want to save the data to. So, if Table1 has INST_ID, Firstname, LastName, Emailaddress, Phone Number. I am selecting the INST_ID and Emailaddress from Table1 and inserting it into Table2 with the password. It will not insert into Table2, Why?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using System.Security.Cryptography;

public partial class SubmitPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
            con.Open();


            string cmdStr = "Select INST_ID, EmailAddress from TableSecurity where EmailAddress='" + TextBoxEA.Text + "'";
            string cmdStr2 = "Select INST_ID, EmailAddress from TableCEO where EmailAddress ='" + TextBoxEA.Text + "'";
            string cmdStr3 = "Select INST_ID, EmailAddress from TableIALO where EmailAddress ='" + TextBoxEA.Text + "'";
            
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            SqlCommand cmd = new SqlCommand("select INST_ID, EmailAddress from TableSecurity", con);
            SqlCommand cmd2 = new SqlCommand("select INST_ID, EmailAddress from TableCEO", con);
            SqlCommand cmd3 = new SqlCommand("select INST_ID, EmailAddress from TableIALO", con);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            if (temp == 0)

                lblMessage.Text = "User Name Already Exist!!!<br /> Please Choose Another User Name.";
        }



    }

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

        string chkUser = "select INST_ID, EmailAddress from TableCEO where EmailAddress'" + TextBoxEA.Text + "";
        chkUser = "select INST_ID, EmailAddress from TableIALO where EmailAddress'" + TextBoxPW.Text + "";
        SqlCommand chkUsercmd = new SqlCommand(chkUser, con);

        
        string insCmd = "Insert into TableSecurity (INST_ID, EmailAddress, Password) values (@INST_ID, @EmailAddress, @Password)";
        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);
        


        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            Response.Write("An error occurred: " + er.Message);
        }
        finally
        {
        }
    }
    
}

推荐答案

插入table_1 select * from table_2;
Insert into table_1 select * from table_2;


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

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