Asp.net登录与SQL Server数据库 [英] Asp.net login with SQL Server database

查看:229
本文介绍了Asp.net登录与SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的登录表单在我的asp.net页面(我有一个HTML页面,现在我试图使其向asp.net)。它应该采取输入的用户名和密码,连接MSSQL数据库。在那里,我会做一个SQL选择说明书,它选择用户名并在用户名等于在文本框中输入的用户名密码(与后比较,如果输入的密码是一样的一个在数据库)。如果用户名和密码是好的,那么就应该打开另一个网页 - main.aspx。我的Index.aspx看起来是这样的:

I am trying to make a simple login form in my asp.net page(I had a html page and now I am trying to make it to asp.net). It should take the entered username and password and connect to the mssql database. There I will make a SQL Select statment which selects the username and the password where the username is equal to the username entered in the textbox(and after that compare if the password entered is the same as the one in the database). If the username and the password is okay, then it should open another page - main.aspx. My index.aspx looks like this:

<form id="loginform" runat="server" method="post" action="/">
<div id="username_box"><p style="padding-top: 10px; margin: 0px">Username:</p></div>
<input type="text" value="" name="usernameTextBox" runat="server" id="username_input" /><br/>
<div id="password_box"><p style="padding-top: 10px; margin: 0px">Password:</p></div>
<input type="password" value="" name="passwordTextBox" runat="server" id="password_input" /><br/>
<a href="facebook.com" style="float: left;padding-left: 13px;padding-top: 8px;">Glemt kodeordet?</a>
<input type="submit" runat="server" id="LoginBtnClick" />
</form>

提交按钮被pressed之后,就是我的C#code:

After the submit button is pressed, there is my c# code:

protected void LoginBtn_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("select user_username, user_password FROM users WHERE user_username =@username and user_password=@password", con);
    cmd.Parameters.AddWithValue("@username", "user");
    cmd.Parameters.AddWithValue("@password", "1234");
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        Response.Redirect("main.aspx");
    }else
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
    }
} 

现在我不使用的用户名和密码,用户类型的文本框,而只是一个硬codeD的用户名用​​户和密码1234,这是我插在我的数据库。我不知道如何从文本框中的文本。不过,即使有硬codeD的用户名和密码,这整个事情是行不通的。

Right now I am not using the username and the password that the user types in the textbox, but just a hardcoded username "user" and password "1234", which I have inserted in my database. I don't know how to get the text from the textbox. But even with the hardcoded username and password, this whole thing doesn't work.

和现在到底,这是我在我的web.config加入使连接字符串:

And now in the end, this is what I added in my Web.Config to make the connection string:

<connectionStrings>
<add name="myConnectionString" providerName="System.Data.SqlClient"  connectionString="Data Source=hereistheipofmydb;Initial Catalog=mydb;User Id =myadmin;password=mypass;" />

还有一个问题:是否有必要在整个项目的服务器(同数据库)上上传?现在,我有我的主机数据库中,但该网站,我通过Visual Studio 2012上运行。

One more question: is it necessary the whole project to be uploaded on the server(the same as database)? Right now I have my database on the hosting, but the website I am running through Visual Studio 2012.

推荐答案

您正在与数据库中的辛勤工作方式。您使用的连接字符串也是一个内存泄漏的潜在来源。请考虑使用任何LINQ到SQL或实体框架。他们将让您的生活更轻松严重。调查正确使用的事件也将是对你非常有帮助。请查看这些参考资料:

You're working with the database the hard way. Your use of the connection string is also a potential source of a memory leak. Please consider using either LINQ-to-SQL or the Entity Framework. They will make your life seriously easier. Investigating proper use of events will also be very helpful for you. Please review these references:


  1. 发布/订阅的事件模式

  2. 入门LINQ在C#

  3. LINQ到SQL教程初学者

  4. 添加模型ASP.NET中使用MVC4和Entity Framework

  1. Publish/Subscribe event pattern
  2. Getting Started with LINQ in C#
  3. LINQ To SQL Tutorial for Beginners
  4. Adding a model to ASP.NET using MVC4 and Entity Framework

这篇关于Asp.net登录与SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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