2010-12-04 5 views
0

나는 내 클래스 중 하나에서 이와 같은 함수를 가지고 있으며, 다른 클래스에서 이것을 호출하고 디폴트 데이터 테이블에 값을 가져와야합니다.기본 데이터 테이블에있는 하나의 클래스에서 다른 클래스로 함수 호출하기

public DataTable GetPrimaryKeyTables(string localServer, string userName, string password, string selectedDatabase) 
{ 
     // Create the datatable 
     DataTable dtListOfPrimaryKeyTables = new DataTable("tableNames"); 

     SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder(); 
     objConnectionString.DataSource = localServer; ; 
     objConnectionString.UserID = userName; 
     objConnectionString.Password = password; 
     objConnectionString.InitialCatalog = selectedDatabase; 

     // Query to select primary key tables. 
     string selectPrimaryKeyTables = @"SELECT 
               TABLE_NAME 
               AS 
               TABLES 
              FROM 
               INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
              WHERE 
               CONSTRAINT_TYPE = 'PRIMARY KEY' 
             ORDER BY 
               TABLE_NAME"; 

     // put your SqlConnection and SqlCommand into using blocks! 
     using(SqlConnection sConnection = new SqlConnection(objConnectionString.ConnectionString)) 
     using(SqlCommand sCommand = new SqlCommand(selectPrimaryKeyTables, sConnection)) 
     { 
      try 
      { 
       // Create the dataadapter object 
       SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectPrimaryKeyTables, sConnection); 

       // Fill the datatable - no need to open the connection, the SqlDataAdapter will do that all by itself 
       // (and also close it again after it is done) 
       sDataAdapter.Fill(dtListOfPrimaryKeyTables); 
       dgResultView.DataSource = dtListOfPrimaryKeyTables; 
      } 
      catch(Exception ex) 
      { 
       //All the exceptions are handled and written in the EventLog. 
       EventLog log = new EventLog("Application"); 
       log.Source = "MFDBAnalyser"; 
       log.WriteEntry(ex.Message); 
      } 
     } 

     // return the data table to the caller 
     return dtListOfPrimaryKeyTables; 
    } 

누구든지 나를 도와 줄 수 있습니다. 시도 할 때마다 컨트롤이 한 클래스에서 다른 클래스로 상속되지 않습니다.

+0

어제 물어 본 것과 정확히 같은 질문입니다. http://stackoverflow.com/questions/4345506/defining-function-in-one-class-and-calling-in-other-class-is-not-inheriting- the-c / – jvanrhyn

답변

3

'한 클래스에서 다른 클래스로 상속되지 않은 컨트롤'이라는 것이 무슨 뜻인지 잘 모르겠습니다.

이 클래스의 객체를 다른 클래스에 만들고 메소드를 호출합니다. 이

class class1 
{ 
    public DataTable GetPrimaryKeyTables(string localServer, string userName, string password, string selectedDatabase) 


    ....... 
    ........ 
    return dtListOfPrimaryKeyTables; 


} 
class Class2 
{ 
    protected void BindControl(....) 
    { 
     DataTable dt = new class1().GetPrimaryKeyTables(......); 
     dgResultView.DataSource = dt; 
     dgResultView.DataBind(); 

    } 

} 

같은

뭔가 어느 쪽이든 당신은 방법에 매개 변수로 'dgResultView'을 통과하거나 위의 코드를 사용합니다. 컨트롤은 'Protected'로 정의되므로 다른 클래스에서 액세스 할 수 없습니다. 기능에 사용 된 dgResultView.DataSource = dtListOfPrimaryKeyTables;이 작동하지 않습니다.

연결 문자열 및 기타 정보를 구성 파일에 저장하고 거기에서 액세스하는 것이 좋습니다.

관련 문제