2010-12-03 138 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的情況下,你必須傳遞參數。