我想询问有关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/