从两张表中加载和读取。 [英] Loading and reading from Two tables.

查看:86
本文介绍了从两张表中加载和读取。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候。我在VS 2010上使用ASP.net并来到这个区块。我在这个项目中有三个数据库。第一个是注册数据库,其他人有数据,但需要与用户侧的注册一起阅读以查看用户是否存在。



这是我的代码:

Greetings. I am using ASP.net on VS 2010 and came to this block. I have three databases in this project. The first is a registration database and the others have data in them but need to be read crossed with the registration one for the user side to see if the user exist.

Here is my code:

using System;
using System.Data;
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;


public partial class Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test1ConnectionString"].ConnectionString);
            SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["CEOConnectionString"].ConnectionString);
            SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["IALOConnectionString"].ConnectionString);
            con.Open();
            con1.Open();
            
            string cmdStr = "Select count(*) from Registration where UserName=''" + TextBoxEA.Text + "''";
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            new SqlCommand(cmdStr, con1);
            new SqlCommand(cmdStr, con2);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            con.Close();
            con1.Close();
            

            if (temp == 1)
            {
                Response.Write("User Name Already Exist!!!<br /> Please Choose Another User Name.");
            }
        }
    }
    protected void Submit_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test1ConnectionString"].ConnectionString);
        SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["CEOConnectionString"].ConnectionString);
        SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["IALOConnectionString"].ConnectionString);
        con.Open();
        con1.Open();
        
        string insCmd = "Insert into Registration (UserName, Prefix, FirstName, LastName, EmailAddress, Password) values (@UserName, @Prefix, @FirstName, @LastName, @EmailAddress, @Password)";
        SqlCommand insertUser = new SqlCommand(insCmd, con);
        insertUser.Parameters.AddWithValue("@UserName", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Prefix", TextBoxPR.Text);
        insertUser.Parameters.AddWithValue("@FirstName", TextBoxFN.Text);
        insertUser.Parameters.AddWithValue("@LastName", TextBoxLN.Text);
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPass.Text);

        //con1.Open();
        //SqlCommand cmd = new SqlCommand("Insert into COCCEO (LastName, FirstName, Prefix, EmailAddress) values (@LastName, @FirstName, @Prefix, @EmailAddress");
        //new SqlCommand(insCmd, con1);
        //insertUser.Parameters.AddWithValue("@LastName", TextBoxLN.Text);
        //insertUser.Parameters.AddWithValue("@FirstName", TextBoxFN.Text);
        //insertUser.Parameters.AddWithValue("@Prefix", TextBoxPR.Text);
        //insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
             
         try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            con1.Close();

            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            Response.Write("Something Really Bad Has Happened....Please Try Again.");
        }
        finally
        {

        }
    }
}

推荐答案

使用System;

使用System.Data;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data.SqlClient;

使用System.Configuration;





公共部分类注册:System.Web.UI.Page

{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [Test1ConnectionString] .ConnectionString);

SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings [CEOConnectionString]。ConnectionString);

SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings [IALOConnectionString]。ConnectionString);



protected void Page_Load(object sender,EventArgs e)

{

if(IsPostBack)

{



}

}

}



//你声明你的sqlconnection
using System;
using System.Data;
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;


public partial class Registration : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test1ConnectionString"].ConnectionString);
SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["CEOConnectionString"].ConnectionString);
SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["IALOConnectionString"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{

}
}
}

// this you declare your sqlconnection


这篇关于从两张表中加载和读取。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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