开发者

Calling the functionfrom one class to another in a default datatable

I have a function like this in one of my classes and then I need to call it in another class and get the value in a default datatable.

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 SqlCo开发者_开发知识库mmand 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;
    }

Can anyone help me in this, every time I try, the controls are not inherited from one class to another.


I am not sure what you mean by 'controls not inherited from one class to another'.

you will create an object of this class in your another class and call the method on it.

something like this

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();

     }

}

Either you pass 'dgResultView' as a parameter to the method or you use the above code snippet. Controls are defined as 'Protected', hence they won't be accessible in the other class. dgResultView.DataSource = dtListOfPrimaryKeyTables; used in the function is not gonna work.

It is a good idea to have your connection string and other information in a config file and is accessed from there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜