数据库的MS Access 2007连接到C#ASP.net,登录页面 [英] Database MS Access 2007 connection to C# ASP.net , login page

查看:383
本文介绍了数据库的MS Access 2007连接到C#ASP.net,登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个名为用户(登录名,密码,名字,姓氏)表的数据库。而我需要做的登录页面。我看过一些教程,但它并没有帮助。我需要检查是否存在于数据库登录名和密码。然后重定向(如果正确的话),以其他网页。这是我已经做了:

  OleDbConnection的CON =新的OleDbConnection();
    公布尔检查()
    {
        con.ConnectionString = @供应商= Microsoft.ACE.OLEDB.12.0;数据源= C:\用户\ Volodia \文件\ WebSiteDatabase.accdb;
        con.Open();
        字符串的CommandString =SELECT登录名,密码与用户;
        // objadapter =新的SqlDataAdapter(CommandString中,sqlconn.ConnectionString);
        OleDbDataAdapter的objadapter =新OleDbDataAdapter的(CommandString中,con.ConnectionString);
        数据集的数据集=新的DataSet();
        objadapter.Fill(数据集,用户); //显示语法错误在FROM子句。这里
        数据表数据表= dataset.Tables [0];
        的for(int i = 0; I< datatable.Rows.Count;我++)
        {
            。字符串墨西哥国立自治大学= datatable.Rows [I] [密码]的ToString();
            字符串upwd = datatable.Rows [I] [密码]的ToString()。
            如果((墨西哥国立自治大学== TextBox1.Text)及及(upwd == TextBox2.Text))
            {
                返回true;
            }
        }

        返回false;
    }
    保护无效的button1_Click(对象发件人,EventArgs的)
    {
        如果(检查()==真)
        {
            的Response.Redirect(WebForm2.aspx);
        }
    }
 

解决方案

字密码是MS-访问喷气SQL保留关键字。如果你想使用它,你需要它括在方括号,同样为用户

 字符串的CommandString =SELECT登录,[密码] FROM [用户];
 

这将解决的语法错误当前的问题,但让我添加一些其他的code表现出了不同的方法

 公共BOOL检查()
 {
    字符串conString = @供应商= Microsoft.ACE.OLEDB.12.0;数据源= C:\用户\ Volodia \文档\ WebSiteDatabase.accdb;
    使用(OleDbConnection的CON =新的OleDbConnection(conString)
    {
        con.Open();
        字符串的CommandString =SELECT COUNT(*)作为cntUser FROM [用户]+
                               ?WHERE登录=和[密码] =;
        使用(OleDbCommand的CMD =新的OleDbCommand(CommandString中,CON))
        {
            cmd.Parameters.AddWithValue(@ P1,TextBox1.Text);
            cmd.Parameters.AddWithValue(@ P2,TextBox2.Text);
            INT结果=(INT)cmd.ExecuteScalar();
            如果(结果大于0)
               返回true;
        }
    }
    返回false;
}
 

  • 第一,不要使用全局连接对象,但创建和使用 连接只在需要时。
  • 其次,封装就像连接一次性对象和 与using语句命令,以确保正确关闭 和处置,
  • 三,通过用户名和密码作为where条件 第(以后会更多)
  • 四,使用参数化查询,以避免语法错误和SQL 注射

通常不是一个很好的做法,存储在数据库里明文密码。你需要存储密码的唯一的哈希值每一次你需要检查用户的真实性时间重新计算该散列

I have a database that contains a table named "User(login,password,firstname,lastname)" . And I need to make login page . I've watched some tutorials , but it didn't help . I need to check if login and password exist in the database . and then redirect(if correct) to other page . This is what I already did:

OleDbConnection con = new OleDbConnection();
    public bool check()
    {
        con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Volodia\Documents\WebSiteDatabase.accdb";
        con.Open();
        string commandstring = "SELECT login,password FROM User";
        //objadapter = new SqlDataAdapter(CommandString, sqlconn.ConnectionString);
        OleDbDataAdapter objadapter = new OleDbDataAdapter(commandstring, con.ConnectionString);
        DataSet dataset = new DataSet();
        objadapter.Fill(dataset, "User");// it shows "Syntax error in FROM clause." here
        DataTable datatable = dataset.Tables[0];
        for (int i = 0; i < datatable.Rows.Count; i++)
        {
            string unam = datatable.Rows[i]["login"].ToString();
            string upwd = datatable.Rows[i]["password"].ToString();
            if ((unam == TextBox1.Text)&&(upwd==TextBox2.Text))
            {
                return true;
            }
        }

        return false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (check() == true)
        {
            Response.Redirect("WebForm2.aspx");
        }
    }

解决方案

The word PASSWORD is a reserved keyword for MS-Access Jet SQL. If you want to use it you need to enclose it in square brackets, the same for USER

 string commandstring = "SELECT login, [password] FROM [User]";

This will resolve the immediate problem of the Syntax Error but let me add some other code to show a different approach

 public bool check()
 {
    string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Volodia\Documents\WebSiteDatabase.accdb";
    using(OleDbConnection con = new OleDbConnection(conString)
    {
        con.Open();
        string commandstring = "SELECT count(*) as cntUser FROM [User] " + 
                               "WHERE login = ? AND [password] = ?";
        using(OleDbCommand cmd = new OleDbCommand(commandstring, con))
        {
            cmd.Parameters.AddWithValue("@p1", TextBox1.Text); 
            cmd.Parameters.AddWithValue("@p2", TextBox2.Text);
            int result = (int)cmd.ExecuteScalar();
            if(result > 0)
               return true;
        }
    }
    return false;
}

  • First, do not use a global connection object but create and use the connection only when needed.
  • Second, encapsulate the disposable objects like the connection and the command with the using statement that will ensure a correct close and dispose,
  • Third, pass the login and the password as conditions for the where clause (more on this later)
  • Fourth, use the parametrized query to avoid syntax errors and sql injection

Usually is not a good practice to store a password in clear text inside the database. You need to store only the hash of the password and recalculate this hash every time you need to check the user authenticity

这篇关于数据库的MS Access 2007连接到C#ASP.net,登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆