2011-11-17 65 views
-2

我有這樣的代碼在這裏:安排代碼正確

public class clsDataLayer 
{ 
    // This function saves the personnel data 
    public static bool SavePersonnel(string Database, string FirstName, string LastName, 
            string PayRate, string StartDate, string EndDate) 
    { 

     bool recordSaved; 

     try 
     { 
      // Retrieving information 
      OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" + 
                 "Data Source=" + Database); 
      conn.Open(); 
      OleDbCommand command = conn.CreateCommand(); 
      string strSQL; 
      // Inserting information into the table 
      strSQL = "Insert into tblPersonnel " + 
        "(FirstName, LastName, PayRate, StartDate, EndDate) values ('" + 
        FirstName + "', '" + LastName + "', " + PayRate + ", '" + StartDate + 
        "', '" + EndDate + "')"; 
      // Gets the statement to execute at the data source 
      command.CommandType = CommandType.Text; 
      command.CommandText = strSQL; 
      // Executes the SQL statement and returns the number of rows 
      command.ExecuteNonQuery(); 
      // Closes the connection to the data source 
      conn.Close(); 
      recordSaved = true; 
     } 
     catch (Exception) 
     { 
      recordSaved = false; 

     } 

     return recordSaved; 
    } 


    // This function gets the user activity from the tblUserActivity 
    public static dsUserActivity GetUserActivity(string Database) 
    { 
     // States the classes used 
     dsUserActivity DS; 
     OleDbConnection sqlConn; 
     OleDbDataAdapter sqlDA; 

     // Defines sqlConnclass and what each will consist of 
     sqlConn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" + 
      "Data Source=" + Database); 

     // Defines sqlDA and what each will consist of 
     sqlDA = new OleDbDataAdapter("select * from tblUserActivity", sqlConn); 

     // Defines DS and what each will consist of 
     DS = new dsUserActivity(); 

     // Outputs the results from the information gathered 
     sqlDA.Fill(DS.tblUserActivity); 

     // Starts over for a new user 
     return DS; 
    } 

    // This function saves the user activity 
    public static void SaveUserActivity(string Database, string FormAccessed) 
    { 
     // Defines the connection to the database 
     OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" + 
      "Data Source=" + Database); 
     conn.Open(); 
     OleDbCommand command = conn.CreateCommand(); 
     string strSQL; 

     strSQL = "Insert into tblUserActivity (UserIP, FormAccessed) values ('" + 
      GetIP4Address() + "', '" + FormAccessed + "')"; 

     command.CommandType = CommandType.Text; 
     command.CommandText = strSQL; 
     command.ExecuteNonQuery(); 
     conn.Close(); 
    } 

    // This function gets the IP Address 
    public static string GetIP4Address() 
    { 
     string IP4Address = string.Empty; 

     foreach (IPAddress IPA in 
        Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress)) 
     { 
      if (IPA.AddressFamily.ToString() == "InterNetwork") 
      { 
       IP4Address = IPA.ToString(); 
       break; 
      } 
     } 

     if (IP4Address != string.Empty) 
     { 
      return IP4Address; 
     } 

     foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName())) 
     { 
      if (IPA.AddressFamily.ToString() == "InterNetwork") 
      { 
       IP4Address = IPA.ToString(); 
       break; 
      } 
     } 

     return IP4Address; 
    } 




    public clsDataLayer() 
    { 

    } 



    public static dsPersonnel GetPersonnel(string p) 
    { 
     throw new NotImplementedException(); 
    } 
} 

我需要添加此代碼,但每次我這樣做,我得到的是說沒有重載方法「GetPersonnel」採取「1」參數錯誤

// This function gets the user activity from the tblPersonnel 
    public static dsPersonnel GetPersonnel(string Database, string strSearch) 
    { 
     dsPersonnel DS; 
     OleDbConnection sqlConn; 
     OleDbDataAdapter sqlDA; 

     //create the connection string 
     sqlConn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" + 
     "Data Source=" + Database); 

     string query; 
     if (strSearch == "" || strSearch.Trim().Length == 0) 
     { 
      query = "SELECT * from tblPersonnel"; 
     } 
     else 
     { 
      query = "select * from tblPersonnel where LastName = '" + strSearch + "'"; 
     } 


     // Defines sqlDA and what each will consist of 
     sqlDA = new OleDbDataAdapter("select * from tblPersonnel", sqlConn); 

     // Defines DS and what each will consist of 
     DS = new dsPersonnel(); 

     // Outputs the results from the information gathered 
     sqlDA.Fill(DS.tblPersonnel); 

     // Starts over for a new user 
     return DS; 
    } 

    // This function saves the user activity 
    public static void SavePersonnel(string Database, string FormAccessed) 
    { 
     // Defines the connection to the database 
     OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" + 
      "Data Source=" + Database); 
     conn.Open(); 
     OleDbCommand command = conn.CreateCommand(); 
     string strSQL; 

     strSQL = "Insert into tblPersonnel (UserIP, FormAccessed) values ('" + 
      GetIP4Address() + "', '" + FormAccessed + "')"; 

     command.CommandType = CommandType.Text; 
     command.CommandText = strSQL; 
     command.ExecuteNonQuery(); 
     conn.Close(); 

    } 
+3

我看不到你在哪裏調用GetPersonnel通常這個錯誤是在調用該方法的代碼上 –

回答

1

它看起來像你在同一個類中定義

public static dsPersonnel GetPersonnel 

兩次。我懷疑你正在用雙精靈版本替換單精靈版本,但是你仍然在調用單精靈版本。

我知道你沒有要求這種投入的,但我不能幫助我自己......

你應該換你OleDbConnections在使用塊,以確保他們得到關閉,像這樣:

using (OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" + 
     "Data Source=" + Database)) 
{ 
    conn.Open(); 
    ... 
{ 

不知道在您的strSearch數據是從哪裏來的,但你在和自己的一個討厭的SQL注入攻擊這一行:

query = "select * from tblPersonnel where LastName = '" + strSearch + "'";  

你應該使用SQL參數s或一個存儲過程。