c# - C# 中的 2 个数据库中的注册用户

标签 c# mysql asp.net database

我想询问有关asp.net中的代码。 我正在尝试显示在两个不同数据库中注册的用户的信息。 如果用户在第一数据库和第二数据库中注册,则会显示他/她的信息。 (转到h2)

如果用户在第一个数据库中注册,但他/她没有在第二个数据库中注册,则会显示注册页面。 (前往赫雷格)

如果他/她没有在第一数据库中注册,则会显示一条警报消息,表明该用户在第一数据库中不存在。

代码正在运行,但是对于用户同时存在于两个数据库中的情况,代码不起作用。显示 Hreg 页面而不是 h2。

这是c#中的代码。 谢谢

 protected void Button1_Click(object sender, EventArgs e)
        {
            Session["BID"] = B_id.Text;

            SqlConnection conne = new SqlConnection(ConfigurationManager.ConnectionStrings["firstDB"].ConnectionString);
            conne.Open();
            String checkuser = "select * from WU where ID=@ppid";
            SqlCommand com = new SqlCommand(checkuser, conne);

            com.Parameters.AddWithValue("@ppid", B_id.Text);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
            da.Fill(dt);

            var pid = Session["BID"];


             if (dt.Rows.Count > 0)
            {

                SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["secondDB"].ConnectionString);
                SqlCommand cmd1 = new SqlCommand("select * from Patients where PID=@pid", conn1);
                conn1.Open();

                if (pid == cmd1 )
            {
                Response.Redirect("H2.aspx");
            }
                else
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('not registered')</script>");
                }
             conn1.Close();

            }
              conne.Close();
            SqlConnection conne1 = new SqlConnection(ConfigurationManager.ConnectionStrings["firstDB"].ConnectionString);
            conne.Open();
            String checkuser2 = "select * from WU where ID=@ppid";
            SqlCommand com1 = new SqlCommand(checkuser2, conne);

            com1.Parameters.AddWithValue("@ppid", B_id.Text);
            SqlDataAdapter da1 = new SqlDataAdapter(com1);
            DataTable dt1 = new DataTable();
            da1.Fill(dt1);

            if (dt1.Rows.Count > 0)
            {

                SqlConnection conn11 = new SqlConnection(ConfigurationManager.ConnectionStrings["secondDB"].ConnectionString);
                SqlCommand cmd2 = new SqlCommand("select * from Patients where PID=@pid", conn11);
                conn11.Open();


                if (pid != cmd2)
                {
                    Response.Redirect("HReg.aspx"); 

                }

                conn11.Close();

            }
      conne1.Close();






        }

最佳答案

此代码与您的代码几乎相同,您没有在第一个 If 中指定参数值。

顺便说一句,在使用 SqlDataAdapter 时,您不需要手动打开和关闭连接,它会为您完成此操作。

在此代码中,您从数据库中获取数据两次,您应该将它们合并在一起以提高性能(减少开销)。

protected void Button1_Click(object sender, EventArgs e)

{

        Session["BID"] = B_id.Text;

        SqlConnection conne = new SqlConnection(ConfigurationManager.ConnectionStrings["D"].ConnectionString);

        String checkuser = "select * from Students where Id=@ppid";
        SqlCommand com = new SqlCommand(checkuser, conne);
        com.Parameters.AddWithValue("@ppid", B_id.Text);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataTable dt = new DataTable();
        da.Fill(dt);
        var pid = Session["BID"];
        if (dt.Rows.Count > 0)
        {

            SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DataConnection"].ConnectionString);
            SqlCommand cmd1 = new SqlCommand("select * from Students where Id=@pid", conn1);
            cmd1.Parameters.AddWithValue("@pid", pid);//Missing in your code
            conn1.Open();
            if (cmd1.ExecuteNonQuery().ToString() != null)
            {
                Response.Redirect("H2.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('not registered')</script>");
            }
            conn1.Close();
        }
        SqlConnection conne1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DataConnection"].ConnectionString);
        String checkuser2 = "select * from Students where ID=@ppid";
        SqlCommand com1 = new SqlCommand(checkuser2, conne);
        com1.Parameters.AddWithValue("@ppid", B_id.Text);
        SqlDataAdapter da1 = new SqlDataAdapter(com1);
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);
        if (dt1.Rows.Count > 0)
        {
            SqlConnection conn11 = new SqlConnection(ConfigurationManager.ConnectionStrings["DataConnection"].ConnectionString);
            SqlCommand cmd2 = new SqlCommand("select * from Students where PID=@pid", conn11);
            conn11.Open();
            if (pid != cmd2)
            {
                Response.Redirect("HReg.aspx");

            }
            conn11.Close();
        }
    }

关于c# - C# 中的 2 个数据库中的注册用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29498051/

相关文章:

PHP: SQLSTATE[42000]: 语法错误或访问冲突: 1064

c# - 从 Azure Blob 存储反序列化对象的最快方法?

c# - 在 Entity Framework 中按自定义顺序对 VARCHAR 字段进行排序

php - sql group by 重复结果

mysql - Nodejs连接数据库或REST服务

asp.net - web.config文件中的staticcontent标记究竟适用于什么?

asp.net - ASPNET5类库抛出当前运行时目标框架与 '<ProjectName>'不兼容

c# - 正则表达式从 url 获取页码

c# - Entity Framework 中存储函数的使用

c# - 当范围很大时,VSTO 4 & C# NamedRange.RefersToRange 抛出 Com 异常