| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 496 人关注过本帖
标题:求一个登录代码和连接数据库的代码
只看楼主 加入收藏
hellokity555
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-9-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:0 
求一个登录代码和连接数据库的代码
我在做一个项目,用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.");
    }
}
希望多多指教!
搜索更多相关主题的帖子: 数据库 代码 登录 
2009-09-08 12:45
快速回复:求一个登录代码和连接数据库的代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034507 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved