ASP.NET连接SQL2005的登入页面代码

来源:百度知道 编辑:UC知道 时间:2024/09/25 02:21:20
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button_Click(object sender, EventArgs e)
{
string connectionString = "server=localhost;database=HX1;integrated security=sspi";
SqlConnection mySqlConnection = new SqlConnection(connectionString);
mySqlConnection.Open();
string sql = "select count(*) from User where username=" + this.text1.Text + " and pwd=" + this.pwd.Text + "";

SqlCommand myConmmand = new SqlCommand(sql, mySqlConnection);

int flag = (int)myConmmand.ExecuteScalar();
mySqlConnection.Close();

if (flag > 0)
{

Response.Redirect("default.aspx");
}

第一点 server=localhost;database=HX1;integrated security=sspi 连接字符串的配置,看你在web.config 里面是否配置了信任登录。

第二点 给 string sql = "select count(*) from User where username=" + this.text1.Text + " and pwd=" + this.pwd.Text + ""; 这个sql语句里面给user加上中括号,即改成 [User]

string sql = "select count(*) from User where username='" + this.text1.Text + "' and pwd='" + this.pwd.Text + "'";

看看 我给你加的单引号