连接Sql server

后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string connstr = "Data Source=GFWMJ3FJ7JLJJBR;Initial Catalog=ZT_DB.MDF;Persist Security Info=True;User ID=sa;Password=111111";
        using (SqlConnection conn = new SqlConnection(connstr))
        {
            conn.Open();
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = "Select * from person where person_name=@u";
                cmd.Parameters.Add(new SqlParameter("u",txtUserName.Text));
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    if (reader.GetString(reader.GetOrdinal("pwd")) == txtPassword.Text)
                    {
                        Response.Redirect("Page1.aspx");
                    }
                    else
                    {
                        Label1.Text = "密码不正确";
                    }
                }
                else
                {
                    Label1.Text = "用户名不存在!";
                }
            
            }
        }
    }
}

login.aspx:
<%@ Page Language="C#" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <table>
          
           <tr>
            <td></td>
            <td>用户名:</td>
            <td></td>
            <td>
                <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
           </tr>
       

           <tr>
            <td></td>
            <td>密&nbsp;码:</td>
            <td></td>
            <td>
                <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
           </tr>

             <tr>
            <td></td>
            <td> <asp:Button ID="btnLogin" runat="server" Text="登录" onclick="btnLogin_Click" /></td>
            <td></td>
            <td><asp:Button ID="btnClear" runat="server" Text="注册" /></td>
            <td>
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label></td>
           </tr>

       </table>
    
    </div>
    </form>
</body>
</html>

你可能感兴趣的:(sql,server,server,XHTML,button,login,textbox)