asp.net使用数据库登录

时间:2014-04-16 01:22:37

标签: c# asp.net database login

我的登录和数据库存在很大问题。我经常搜索,但找不到解决方案。首先,我正试图从我的数据库中获取行。当一切都是真的,我想重定向。但是当我写入我的登录名和密码时,大约30秒后我收到“与SQL Server建立连接时出现网络相关或特定于实例的错误......”

这是我的代码

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Configuration;
    using System.Data;
    using System.Data.SqlClient;

    namespace _29
    {
public partial class Default : System.Web.UI.Page
{  
    private string strcon = WebConfigurationManager.ConnectionStrings["UserConnectionString1"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)

 {
        if ((Session["Check"] != null) && (Convert.ToBoolean(Session["Check"]) == true))
        {

// If User is Authenticated then moved to a main page
            if (User.Identity.IsAuthenticated)
                Response.Redirect("Welcome.aspx");
        }     
}

   protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        Boolean wynik;
        wynik = false;       

wynik = Authentication(Login1.UserName, Login1.Password);

        if (wynik == true) {
            e.Authenticated = true;


Session["Check"] = true;
        }
        else
        {
            e.Authenticated = false;



        }
    }
   private bool Authentication(string p1, string p2)
   {



       SqlConnection con = new SqlConnection(strcon);
       SqlCommand cmd = new SqlCommand("Select count(UserName) from Users where UserName='" + p1 + "' and Password ='" + p2 + "'", con);


con.Open();
       int result = (int)cmd.ExecuteScalar();
       if (result == 1)
       {


return true;
       }
       else
       {


return false;
       }

   }}



       }

这是我的Web.Config

<configuration>
    <connectionStrings>
        <add name="UserConnectionString1" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirect`enter code here`ory|\User.mdf;Integrated 

    Security=True"
                    providerName="System.Data.SqlClient" />
            </connectionStrings>
            <system.web>


<compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
  <appSettings>


<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
</configuration>

尝试按行获取结果但不起作用。 请帮帮我:)。

2 个答案:

答案 0 :(得分:0)

您的连接字符串似乎有误。

应该是

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|User.mdf;Database=dbname;
Trusted_Connection=Yes;

不像你在web.config中使用的那样

Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirect`enter codehere`ory|\User.mdf;Integrated Security=True

始终检查this website以获取正确的连接字符串。这非常有用。

干杯!

答案 1 :(得分:0)

我认为连接正常,因为当我在Page_Load上使用连接时,我可以从我的数据库中接收信息。在这种情况下,“result”为1,因为只存在1行。但正如你所看到的,我必须在我的代码上放置已经字符串。我想使用输入(登录控制)。 谢谢你

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace _29
{

    public partial class Default : System.Web.UI.Page
    {


        private string strcon = WebConfigurationManager.ConnectionStrings["UserConnectionString1"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            string p1 ="david";
            string p2 = "1234a";
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand("Select count(UserName) from Users where UserName='" + p1 + "' and Password ='" + p2 + "'", con);
            con.Open();
            int result = (int)cmd.ExecuteScalar();
            Response.Write(result);

        }
}