2014-04-16 98 views
0

我有一個登錄和數據庫的大問題。我搜索了很多,但找不到解決方案。首先,我試圖從我的數據庫中獲得排名。當一切都是真實的時候,我想重定向。但是,當我在寫我的登錄名和密碼,大約30秒後,我收到「而與SQL Server建立連接時出現與網絡相關的或特定於實例的錯誤...」asp.net登錄數據庫

這裏是我的代碼

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> 

試圖獲得由行的結果,但它不工作。 請幫我:)

+0

首先,你應該參數化你的sql語句。 sql注入已經成熟。請看看這樣的文章 - > http://www.codeproject.com/Articles/604268/Hack-Proof-Your-ASP-NET-Applications-From-SQL-Inje關於你的問題,我會檢查你的連接字符串,因爲它很可能不指向有效的sql服務器實例。 –

回答

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得到適當的連接字符串。這非常有用。

乾杯!

0

我認爲連接是好的,因爲當我在Page_Load上使用連接時,我可以從我的數據庫接收信息。在這種情況下,「結果」是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); 

     } 
}