[英]Asp.net and Sql server
使用帶有SQL Server的asp.net在登錄頁面中驗證用戶名和密碼。 但是問題是,當我在錯誤頁面上輸入正確的數據時,而不是在歡迎頁面上。
using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication21
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from 'user_insert' where username = @username and password = @password,con");
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Response.Redirect("Welcom.aspx");
}
else
{
Response.Redirect("Error.aspx");
}
}
和web.config中的連接字符串
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=Ali-PC;Initial Catalog=LogIn;Integrated Security=True"/>
</connectionStrings>
執行查詢,您將在數據表中使用以下代碼而不是您的代碼獲得結果:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from 'user_insert' where username = @username and password = @password,con");
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
da.Fill(dt);
您缺少要執行的代碼中要執行的sql命令
SqlDataAdapter da = new SqlDataAdapter(cmd);
但沒有在您將任何sql命令分配給cmd的位置
像這樣嘗試並檢查結果
SqlCommand cmd = new SqlCommand
(@"select * from 'your table' where username = @username and password = @password",con);
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.