本文介绍了在一个类中定义函数并在另一个类中调用不是继承控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在一个类中定义了函数

I have defined the function in one class like

public static DataSet GetAllPrimaryKeyTables()
{
  //An instance of the connection string is created to manage the contents of the connection string.
  using(var sConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]))
  {
    //To Open the connection.
    sConnection.Open();

    //Query to select the table_names that have PRIMARY_KEYS.
    string selectPrimaryKeys = @"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
                                 WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME <> 'dtProperties'
                                 ORDER BY TABLE_NAME";

    //Create the command object
    using(var sCommand = new SqlCommand(selectPrimaryKeys, sConnection))
    {
      try
      {
        //Create the dataset.
        DataSet dsPrimaryKeyTables = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS ");

        //Create the dataadapter object.
        SqlDataAdapter daPrimaryKeyTables = new SqlDataAdapter(selectPrimaryKeys, sConnection);

        //Provides the master mapping between the sourcr table and system.data.datatable
        daPrimaryKeyTables.TableMappings.Add("Table", "INFORMATION_SCHEMA.TABLE_CONSTRAINTS ");

        //Fill the dataadapter.
        daPrimaryKeyTables.Fill(dsPrimaryKeyTables);

        //Bind the result combobox with non primary key table names
        DataViewManager dsvPrimaryKeyTables = dsPrimaryKeyTables.DefaultViewManager;

        return dsPrimaryKeyTables;
      }
      catch(Exception ex)
      {
        //Handles the exception and log that to the EventLog with the original message.
        EventLog log = new EventLog("Application");
        log.Source = "MFDBAnalyser";
        log.WriteEntry(ex.Message);

        return null;
      }
      finally
      {
        //checks whether the connection is still open.
        if(sConnection.State != ConnectionState.Closed)
        {
          sConnection.Close();
        }
      }
    }
  }
}

现在我应该如何编码,以便我可以在 dafault 数据集中的另一个类中调用该函数.

And now how should i code so that I can call that function in another class in a dafault dataset.

有人可以帮我吗??

推荐答案

使此功能成为 扩展方法 如果我理解你,DataSet 类型将解决你的问题,

Making this function an Extension Method on DataSet type will sole your problem if I understand you,

这篇关于在一个类中定义函数并在另一个类中调用不是继承控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 03:06