2010-12-04 5 views
0

私はこのような関数を自分のクラスの1つに入れて、別のクラスで呼び出す必要があり、デフォルトのデータテーブルで値を取得する必要があります。デフォルトのデータテーブル内のあるクラスから別のクラスに関数を呼び出す

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;は機能しません。

接続文字列とその他の情報を設定ファイルに格納し、そこからアクセスすることをお勧めします。

関連する問題