注册 登录
编程论坛 ASP.NET技术论坛

求一个登录代码和连接数据库的代码

hellokity555 发布于 2009-09-08 12:45, 496 次点击
我在做一个项目,用C#语言编写,可我没学过,刚开始先用编写登录界面,点击登录之后验证成功还是失败,需要连接数据库,连接数据库的时候出错,由于本人学识浅薄,所以请各位帮帮忙,给一个代码,或帮忙修改一下,先谢了!
登录界面代码:Default.asp
<%@ Page Language="C#" AutoEventWireup="true"   CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www. >
<head id="Head1" runat="server">
     <title>Login</title>
</head>
<body >
     <form id="form1" runat="server">
     <div>
         &nbsp;&nbsp;<table style="width: 230px; left: 269px; position: absolute; top: 29px;">
             <tr>
                 <td>
                    <asp:DropDownList ID="DropDownLisEmotion" runat="server">
                    <asp:ListItem Value="管理员">管理员</asp:ListItem>
                    <asp:ListItem Value="检测员">检测员</asp:ListItem>
                    <asp:ListItem Value="复检员">复检员</asp:ListItem>
                    <asp:ListItem Value="业务员">业务员</asp:ListItem>
                    </asp:DropDownList>
                    </td>
               </tr>
                    <tr>.
                 <td style="width: 168px; text-align: right" align="right">
                     UserName</td>
                 <td style="width: 253px">
                     <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
             </tr>
             <tr>
                 <td style="width: 168px; text-align: right; height: 24px;" align="right">
                     UserPsw</td>
                 <td style="height: 24px; width: 253px;">
                     <asp:TextBox ID="txtUserPsw" runat="server" Font-Bold="True" Font-Overline="True" Font-Underline="True"></asp:TextBox></td>
             </tr>
             <tr>
                 <td style="width: 228px; text-align: center" colspan="2" rowspan="2">
                     <asp:Button ID="btnSubmit" runat="server" Text="登录" OnClick="btnSubmit_Click" />&nbsp;
                     <asp:Button ID="btnClear" runat="server" Text="清除" OnClick="btnClear_Click" />
                     <input id="Reset1" type="reset" value="重置"   /><br />
         <asp:Label ID="lblMsg" runat="server" Width="192px"></asp:Label></td>
             </tr>
         </table>
     </div>
     </form>
</body>
</html>
连接数据库的代码:Default.asp.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.txtUserName.Text = "";
            this.txtUserPsw.Text = "";
        }
        this.lblMsg.Text = "";
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        this.txtUserName.Text = "";
        this.txtUserPsw.Text = "";
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (this.txtUserName.Text == "")
        {
            this.lblMsg.Text = "用户名不能为空";
            this.txtUserName.Focus();
        }
        else
        {
            if (this.txtUserPsw.Text == "")
            {
                this.lblMsg.Text = "密码不能为空";
                this.txtUserPsw.Focus();
            }
        }
        if (this.txtUserName.Text != "" && this.txtUserPsw.Text != "")
        {
            if (Login(this.txtUserName.Text, this.txtUserPsw.Text))
            {
                this.lblMsg.Text = txtUserName.Text + "登录成功!";
            }
            else
            {
                this.lblMsg.Text = "登录失败请检查用户名和密码!";
            }
        }
    }
    private bool Login(string UserName, string UserPwd)
    {
        SqlConnection CONN = new SqlConnection("server=192.168.0.2;database=login;uid=sa;pwd=;");
        CONN.Open();
        SqlCommand CMD = new SqlCommand("select count(*) from login where UserName='" + UserName + "' and UserPwd='" + UserPwd + "'", CONN);
        int count = Convert.ToInt32(CMD.ExecuteScalar());
        if (count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    private string DateTime()
    {
        throw new Exception("The method or operation is not implemented.");
    }
}
希望多多指教!
0 回复
1