2013-10-14 32 views
1
從SQL訪問數據

我在C#這是在DLL DB是這樣使用DataReader的

static SqlConnection cnn; 
    static SqlDataReader reader; 
    public string StorePass; 
    public string pass; 
    byte[] tmpSource; 
    byte[] tmpHash; 
    public int user; 
    //static ArrayList list; 
    static string connect = @"Server=.;database=Intranet;Integrated Security=true"; 

    public static void open() 
    { 
     cnn = new SqlConnection(); 
     cnn.ConnectionString = connect; 
     try 
     { 
      cnn.Open(); 
     } //open connection 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message); 
      Console.WriteLine(e.Source); 

      Console.WriteLine("unable to open"); 
     } 
    } 
    public bool login(int usr, string pass) 
    { 
     user = usr; 
     this.pass = pass; 
     string temppass; 
     tmpSource = ASCIIEncoding.ASCII.GetBytes(pass); 
     tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource); 
     temppass = ByteArrayToString(tmpHash); 
     DB.open(); 
     StorePass = DB.retrievePass(user); 
     bool bEqual = false; 
     bEqual = String.Equals(temppass, StorePass, StringComparison.Ordinal); 

     if (bEqual) 
     { 

      return true; 
     } 

    } 
    static string ByteArrayToString(byte[] arrInput) 
    { 
     int i; 
     StringBuilder sOutput = new StringBuilder(arrInput.Length); 
     for (i = 0; i < arrInput.Length - 1; i++) 
     { 
      sOutput.Append(arrInput[i].ToString("X2")); 
     } 
     return sOutput.ToString(); 
    } 

    private static string retrievePass(int user) 
    { 
     using (cnn) 
     { 
      string pass = ""; 
      string table = "Login_Table"; 
      string strSQL = string.Format("Select * From {0} where UID = '{1}'", table, user); 
      SqlCommand myCommand = new SqlCommand(strSQL, cnn); 
      cnn.Open(); 
      reader = myCommand.ExecuteReader(); 
      /*while (reader.Read()) 
      { 
       pass = reader["Hashed_Password"].ToString(); 
      }*/ 
      try 
      { 
       reader.Read(); 
       pass = reader["Hashed_Password"].ToString(); 
       reader.Close(); 
       return pass; 

      } 
      catch 
      { 
       reader.Close(); 
       return null; 
      } 
     } 
    } 

我從網站aspx.cs從我所說的以上方法訪問

DB ob; 
protected void Page_Load(object sender, EventArgs e) 
{ 
    ob = new DB(); 
    DB.open(); 
} 
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) 
{ 
    try 
    { 

     int id = int.Parse(Login1.UserName); 

     string pass = Login1.Password; 
     if (ob.login(id, pass)) 
     { 
      Session["user"] = ob; 
      this.Session["UserName"] = Login1.UserName; 

      Response.Redirect("Post_View.aspx"); 
     } 
    } 
    catch(Exception ex) 
    { 
     throw; 
    } 

} 
protected void LoginButton_Click(object sender, EventArgs e) 
{ 

} 

上面的代碼在Visual Studio中很好地模擬。但是,當部署在IIS 8中時,它顯示用戶'IIS APPPOOL \ .NET v2.0'登錄失敗。請在此處幫助我,因爲在IIS中部署時,其他代碼也會以類似的顯示方式寫入錯誤。我是否需要更改我的代碼?由於提前

我的堆棧跟蹤

 Server Error in '/Test' Application. 

    Login failed for user 'IIS APPPOOL\.NET v2.0'. 

    Description: An unhandled exception occurred during the execution of the current web  request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'IIS APPPOOL\.NET v2.0'. 

    Source Error: 


Line 80:     string strSQL = string.Format("Select * From {0} where UID = '{1}'", table, user); 
Line 81:     SqlCommand myCommand = new SqlCommand(strSQL, cnn); 
Line 82:     cnn.Open(); 
Line 83:     reader = myCommand.ExecuteReader(); 
Line 84:     /*while (reader.Read()) 

Source File: e:\Demo\Intranet_DB\Intranet_DB\DB.cs Line: 82 

Stack Trace: 


[SqlException (0x80131904): Login failed for user 'IIS APPPOOL\.NET v2.0'.] 
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +578 
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +88 
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +6322807 
System.Data.SqlClient.SqlConnection.Open() +258 
Intranet_DB.DB.retrievePass(Int32 user) in e:\Demo\Intranet_DB\Intranet_DB\DB.cs:82 
Intranet_DB.DB.login(Int32 usr, String pass) in e:\Demo\Intranet_DB\Intranet_DB\DB.cs:51 
    _Default.Login1_Authenticate(Object sender, AuthenticateEventArgs e) in c:\inetpub\wwwroot\Test\Default.aspx.cs:38 
    System.Web.UI.WebControls.Login.AttemptLogin() +152 
    System.Web.UI.WebControls.Login.OnBubbleEvent(Object source, EventArgs e) +124 
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +70 
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981 
+0

你可以發佈一個屏幕截圖,顯示'IIS APPPOOL \ .NET v2.0'有權訪問sql數據庫嗎? –

+0

@JeremyThompson我不能幫你嗎?我們必須在IIS服務器上更改任何設置以訪問特定的數據庫 –

回答

0

檢查您的連接字符串。您正在使用當前用戶登錄到數據庫,並且在IIS(IIS APPPOOL.NET v2.0)中運行應用程序池的用戶無權訪問數據庫。

您應該:

  1. 更改連接字符串,並指定一個不同的用戶。
  2. 或更改運行應用程序池的用戶。
+0

U是否意味着我應該在連接字符串中指定不同的服務器? –

+0

不,而是添加一個'User Id = myUsername; Password = myPassword;'指定您的用戶名和密碼。 – Szymon

+0

不起作用我再次收到相同的錯誤。 –

0
  1. 使用.Net框架的確切版本創建您自己的應用程序池。
  2. 使用用戶名和密碼修改您的連接字符串,請勿將其用於Windows身份驗證。
+0

請解釋如何創建自己的應用程序池? –

+0

從連接字符串中移除集成安全標籤。它正在爲SQL Server創建衝突。 – ZahidKakar

+0

刪除集成安全性後出現錯誤爲「連接未關閉,連接的當前狀態已打開」 –