2010-12-08 1 views
0

У меня есть функция, как этотПреобразования DataTable в строковый тип

 public DataTable GetAllPrimaryKeyTables(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' 
                AND 
                 TABLE_NAME <> 'dtProperties' 
               ORDER BY 
                 TABLE_NAME"; 

       // put your SqlConnection and SqlCommand 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); 
        } 
        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; 



      } 

Тогда я хотел бы получить возвращаемый тип как строка, а не как DataTable, так что я сделал это ..

public string GetAllPrimaryKeyTables(string ConnectionString) 
     { 
      string result = string.Empty; 

      // Query to select primary key tables. 
      string selectPrimaryKeyTables = @"SELECT 
                TABLE_NAME 
                AS 
                TABLES 
               FROM 
                INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
               WHERE 
                CONSTRAINT_TYPE = 'PRIMARY KEY' 
               AND 
                TABLE_NAME <> 'dtProperties' 
              ORDER BY 
                TABLE_NAME"; 

      // put your SqlConnection and SqlCommand into using blocks! 
      using(SqlConnection sConnection = new SqlConnection(ConnectionString)) 
      using(SqlCommand sCommand = new SqlCommand(selectPrimaryKeyTables, sConnection)) 
      { 
       try 
       { 
        // Create the dataadapter object 
        SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectPrimaryKeyTables, sConnection); 
        DataTable dtListOfPrimaryKeyTables = new DataTable("tableNames"); 

        // 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); 
        using(StringWriter sw = new StringWriter()) 
        { 
         dtListOfPrimaryKeyTables.WriteXml(sw); 
         result = sw.ToString(); 
        } 
       } 
       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 result; 
     } 

Но это не возвращает строку, которая должна ...

Можете ли вы направить меня до такой степени, что требуются изменения.

+0

Я исправил форматирование ... Становится очевидным, что есть два фрагмента кода (до и после) – 2010-12-08 10:59:49

ответ

0

Вместо StringWriter используйте StringBuilder, затем вы можете использовать методы Write и WriteLine.

0

Если вы хотите перечислить запятую, вы можете использовать что-то вроде ниже.

private string GetList(ds) 
{ 
    var result = string.Empty; 
    for(int i = 0; i < ds.Rows.Count; i++) 
    { 
     result += ds.Row[i][0] + ","; 
    } 
    result = result.Trim(','); 
    return result; 
} 

Надеюсь, это поможет.