调试给出结果但未填充

发布于 2024-10-05 13:55:42 字数 2865 浏览 4 评论 0原文

我在一个类中有一个这样的函数

public static DataSet GetAllUppercasedTables()
{
  //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 tables having their names in uppercased format.
     string selectUppercasedTables = @"SELECT  NAME
                                       FROM  sysobjects 
                                       WHERE UPPER(name) COLLATE Latin1_General_BIN = name COLLATE Latin1_General_BIN 
                                       AND OBJECTPROPERTY(ID,N'IsTable')=1
                                       AND OBJECTPROPERTY(ID,N'IsMSShipped')=0 ";
      //Create the command object
      using(var sCommand = new SqlCommand(selectUppercasedTables, sConnection))
      {
         try
         {
           //Create the dataset.
           DataSet dsUppercasedTables = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS ");

           //Create the dataadapter object.
           SqlDataAdapter da = new SqlDataAdapter(selectUppercasedTables, sConnection);

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

           //Fill the dataadapter.
           da.Fill(dsUppercasedTables);

           //Bind the result combobox with non primary key table names
           DataViewManager dsv = dsUppercasedTables.DefaultViewManager;
           return dsUppercasedTables;
         }
         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();
           }
         }
       }
     }
   }

,我在另一个类中调用这个函数

public void GetTablesWithUpperCaseName()
{
  DataSet dsUppercasedTables = default(DataSet);
  try
  {
    dsUppercasedTables = DataAccessMaster.GetAllUppercasedTables();

    **dgResultView.DataSource** = dsUppercasedTables.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"];
  }
  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);
  }
}

,我几乎在每个需要的点都对其进行了调试,它给出了很好的结果,直到dgResultView.DataЫource....

谁能帮帮我!

I have a function like this in one class

public static DataSet GetAllUppercasedTables()
{
  //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 tables having their names in uppercased format.
     string selectUppercasedTables = @"SELECT  NAME
                                       FROM  sysobjects 
                                       WHERE UPPER(name) COLLATE Latin1_General_BIN = name COLLATE Latin1_General_BIN 
                                       AND OBJECTPROPERTY(ID,N'IsTable')=1
                                       AND OBJECTPROPERTY(ID,N'IsMSShipped')=0 ";
      //Create the command object
      using(var sCommand = new SqlCommand(selectUppercasedTables, sConnection))
      {
         try
         {
           //Create the dataset.
           DataSet dsUppercasedTables = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS ");

           //Create the dataadapter object.
           SqlDataAdapter da = new SqlDataAdapter(selectUppercasedTables, sConnection);

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

           //Fill the dataadapter.
           da.Fill(dsUppercasedTables);

           //Bind the result combobox with non primary key table names
           DataViewManager dsv = dsUppercasedTables.DefaultViewManager;
           return dsUppercasedTables;
         }
         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();
           }
         }
       }
     }
   }

And I am calling this function in another class like this

public void GetTablesWithUpperCaseName()
{
  DataSet dsUppercasedTables = default(DataSet);
  try
  {
    dsUppercasedTables = DataAccessMaster.GetAllUppercasedTables();

    **dgResultView.DataSource** = dsUppercasedTables.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"];
  }
  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);
  }
}

I have debugged it at almost every required point, it is giving fine result till the dgResultView.DataЫource....

Can anybody help me out!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉睡月亮 2024-10-12 13:55:42

无论如何,也将您的 SqlConnectionDataSetSqlDataAdapter 封装在 using 块中。

Anyway wrap your SqlConnection, DataSet and SqlDataAdapter within using block too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文