2010-12-03 11 views
0

を継承していない私は、一つのクラスで定義されている。このような機能を持っている:コントロールが他のクラスから

using System; 
using System.Collections.Generic; 

using System.Linq; 
using System.Text; 

using System.Data; 
using System.Data.SqlClient; 

using System.Windows.Forms; 
using System.Configuration; 
using System.Diagnostics; 
using MFDBAnalyser; 

namespace MFDBAnalyser 

{ 

    public class DataAccessMaster:MFDBAnalyser 

    { 


     //  /// <summary> 
     //  /// This function gets the list of all the databases present in the local server. 
     //  /// </summary> 
     //  /// <returns></returns> 


     public static DataSet GetAllDataBaseNames() 

     { 

      SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder(); 
      objConnectionString.DataSource = txtHost.Text; 
      objConnectionString.UserID = txtUsername.Text; 
      objConnectionString.Password = txtPassword.Text; 

      SqlConnection sConnection = new SqlConnection(objConnectionString.ConnectionString); 

      //If connected then give this message to user 
      lblMessage.Visible = true; 
      lblMessage.Text = "You are connected to the SQL Server...."; 

      try 
      { 
       //To Open the connection. 
       sConnection.Open(); 

       //Query to select the list of databases. 
       string selectDatabaseNames = @"SELECT 
                NAME 
               FROM 
                [MASTER]..[SYSDATABASES]"; 

       //Create the command object 
       SqlCommand sCommand = new SqlCommand(selectDatabaseNames, sConnection); 

       //Create the data set 
       DataSet sDataset = new DataSet("master..sysdatabases"); 

       //Create the dataadapter object 
       SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectDatabaseNames, sConnection); 
       sDataAdapter.TableMappings.Add("Table", "master..sysdatabases"); 

       //Fill the dataset 
       sDataAdapter.Fill(sDataset); 

       //Bind the database names in combobox 
       DataViewManager dsv = sDataset.DefaultViewManager; 

      } 
      catch(Exception ex) 
      { 
       //All the exceptions are handled and written in the EventLog. 
       EventLog logException = new EventLog("Application"); 
       logException.Source = "MFDBAnalyser"; 
       logException.WriteEntry(ex.Message); 
       MessageBox.Show("Login Failed!!", "Error Occured"); 
      } 
      finally 
      { 
       //If connection is not closed then close the connection 
       if(sConnection.State != ConnectionState.Closed) 
       { 
        sConnection.Close(); 
       } 
      } 
     } 
    } 
} 

をそして私はこのような別のクラスでは、この関数を呼び出した:

public void BindDBDropDown() 

     { 

DataSet dsTablesWithoutForeignKeys = default(DataSet); 

      try 
      { 
       //The function GetAllForeignKeyTables() is called from the class PluginManager. 
       dsTablesWithoutForeignKeys = DataAccessMaster.GetAllDataBaseNames(); 

       cmbDatabases.DisplayMember = "TABLE_NAME"; 
       cmbDatabases.ValueMember = ""; 
       cmbDatabases.DataSource = dsTablesWithoutForeignKeys.Tables["master..sysdatabases"]; 
      } 
      catch(Exception ex) 
      { 
       //All the exceptions are handled and written in the EventLog. 
       EventLog logException = new EventLog("Application"); 
       logException.Source = "MFDBAnalyser"; 
       logException.WriteEntry(ex.Message); 
      } 
     } 

しかし、そこにありますtxtHost etxのようなエラーが表示され、designer.csクラスの保護された修飾子をpublicに変更すると、エラーも表示されます...

C誰かが何が起こっているのか教えてください?

答えて

1

テキストボックスがMFDBAnayserクラスで定義されていると仮定しても、GetAllDataBaseNames関数でテキストボックスにアクセスすることはできません。 GetAllDataBaseNamesは静的関数であるため、テキストボックスやその他のコントロールなどのインスタンス変数にアクセスすることはできません。

0

txtHostの修飾子も保護されているか、公開されていることを確認してください。

0

Uは別のクラスのフォームの値を取得できません。パラメータを使用してみてください。値を取得するには、properties {get ;set;}を使用します。私はいくつかのプロパティを使用して、テキストボックスの値を割り当てることができると思います。もしあなたがwinformsでやっているのであれば、静的変数を使うことができます。しかし、Webの場合、引数を渡さなければなりません。

関連する問題