当前位置:编程学习 > C#/ASP.NET >>

c#中实现登陆功能代码。。。

好难啊,求帮助。。。我要做网页的。。。

这是连接数据库的代码:
public static OleDbConnection createDB()
        {

            OleDbConnection odbc = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source=/App_Data/db.mdb;");
            return odbc;
        }



这是登陆的代码:
 private void butLogin_Click(object sender, EventArgs e)
        {

            if (textName.Text != "" & textPass.Text != "")
            {
                SqlDataReader temDR = MyClass.getcom("select*from tb_Login where Name='" + textName.Text.Trim() + "'and Pass='" + textPass.Text.Trim() + "'");
                bool ifcom = temDR.Read();
                if (ifcom)
                {
                    App_Code.DB.Login_Name = textName.Text.Trim();
                    App_Code.DB.Login_ID = temDR.GetString(0);
                    App_Code.DB.My_con.Close();
                    App_Code.DB.My_con.Dispose();
                    App_Code.DB.Login_n = (int)(this.Tag);
                    this.Close();
                }

                else
                {
                    MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textName.Text = "";
                    textPass.Text = "";
                }
                MyClass.con_close();
            }
            else
                MessageBox.Show("请将登陆信息填写完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

    } 数据库 c# odbc login object --------------------编程问答-------------------- 没看到哪里调用了 createDB代码来创建OleDbConnection对象,并且OleDbConnection有没有调用Open方法。 --------------------编程问答-------------------- 把遇到什么错误说清楚。 --------------------编程问答-------------------- 你的代码遇到什么问题了 这样的例子有很多 先理清思路 --------------------编程问答-------------------- 就是登陆功能实现不了,反正点了登陆没有反应。。。。 --------------------编程问答-------------------- 你这个是asp.net么?怎么还有MessageBox.Show() --------------------编程问答--------------------
引用 5 楼 caozhy 的回复:
你这个是asp.net么?怎么还有MessageBox.Show()

只要添加了System.Windows.Forms;引用,是可以弹出对话框的。 --------------------编程问答--------------------
引用 5 楼 caozhy 的回复:
你这个是asp.net么?怎么还有MessageBox.Show()
我是用c#写的,代码是从书上抄的,刚刚学不太懂啊,求指点。。。。
         就是想实现登陆功能,验证用户名密码后转到其他页。。。。。 --------------------编程问答--------------------
引用 6 楼 guwei4037 的回复:
只要添加了System.Windows.Forms;引用,是可以弹出对话框的。
不可以呀,没有任何反应。。。。 --------------------编程问答-------------------- 你抄的是winforms版的代码,根本就不对 --------------------编程问答--------------------
引用 9 楼 caozhy 的回复:
你抄的是winforms版的代码,根本就不对
那要抄什么样的代码呢???求指教。。。。 --------------------编程问答--------------------
引用 10 楼 u011992248 的回复:
Quote: 引用 9 楼 caozhy 的回复:

你抄的是winforms版的代码,根本就不对
那要抄什么样的代码呢???求指教。。。。


你不是要asp.net的么? --------------------编程问答-------------------- 找个SQL类操作吧,这样看上去好累,调试下再看看错误 --------------------编程问答-------------------- 你还是分开去学,先学ado.net,然后再学asp.net --------------------编程问答-------------------- 抄书的时候最好是看懂了。

书没有随书光盘么? --------------------编程问答-------------------- 目测你看看建站的实例,从网上下载点模板 --------------------编程问答-------------------- winform代码,用在了asp.net ?

还是怎么个情况?




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

    }
    protected void BtnOK_Click(object sender, EventArgs e)
    {
        if (txtName.Text == "" || txtPsw.Text == "")
        {
            Response.WriteFile("用户名或密码不能为空");
        }
        else if (Islogin() == 0)
        {
            Response.WriteFile("用户名或密码错误");
        }
        else
        {
            Response.WriteFile("登陆成功");
        }
    }

    private int Islogin()
    {
        string connstr = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString();

        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connstr)) 
        {
            string sql = "select count(*) from table1 where name-'"+txtName+"' and psw = '"+txtPsw+"'";
            SqlDataReader dr = new SqlCommand(sql, conn).ExecuteReader();

            if (dr.Read())
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}
--------------------编程问答--------------------
引用 16 楼 qyj2009 的回复:
winform代码,用在了asp.net ?

还是怎么个情况?





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

    }
    protected void BtnOK_Click(object sender, EventArgs e)
    {
        if (txtName.Text == "" || txtPsw.Text == "")
        {
            Response.Write("用户名或密码不能为空");
        }
        else if (Islogin() == 0)
        {
            Response.Write("用户名或密码错误");
        }
        else
        {
            Response.Write("登陆成功");
        }
    }

    private int Islogin()
    {
        string connstr = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString();

        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connstr)) 
        {
            string sql = "select count(*) from table1 where name-'"+txtName+"' and psw = '"+txtPsw+"'";
            SqlDataReader dr = new SqlCommand(sql, conn).ExecuteReader();

            if (dr.Read())
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}




试试这段  上边写错了方法
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,