如何计算行数并显示它

时间:2010-12-02 05:34:08

标签: c# visual-studio winforms

我正在开发一个桌面应用程序,它通过此函数返回在数据网格中具有外键的表的列表。

       public void GetPrimaryKeyTable()

        {

        //An instance of the connection string is created to manage the contents of the connection string.
        var sqlConnection = new SqlConnectionStringBuilder();
        sqlConnection.DataSource = "192.168.10.3";
        sqlConnection.UserID = "gp";
        sqlConnection.Password = "gp";
        sqlConnection.InitialCatalog = Convert.ToString(cmbDatabases.SelectedValue);
        string connectionString = sqlConnection.ConnectionString;

        SqlConnection sConnection = new SqlConnection(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
        SqlCommand sCommand = new SqlCommand(selectPrimaryKeys, sConnection);

        try
            {
            //Create the dataset
            DataSet dsListOfPrimaryKeys = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS");

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

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

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

            //Bind the result combobox with primary key tables
            DataViewManager dvmListOfPrimaryKeys = dsListOfPrimaryKeys.DefaultViewManager;
            dgResultView.DataSource = dsListOfPrimaryKeys.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"];
            }
        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);
            }
        finally
            {
            //If connection is not closed then close the connection
            if(sConnection.State != ConnectionState.Closed)
                {
                sConnection.Dispose();
                }
            }
        }

现在我想计算属于这个类别的表格的数量,并将其显示在一个标签中,表明这些表格属于此类别。

你们能帮我吗

1 个答案:

答案 0 :(得分:2)

我相信你可以使用DataSet.Tables.Count方法。 http://msdn.microsoft.com/en-us/library/system.data.internaldatacollectionbase.count.aspx

或者,DataSet.Tables [i] .Rows.Count