2010-12-01 12 views
1

ich eine Methode, wie dieseSql Exception gefangen wurde

haben
 public static DataSet GetAllDataBaseNames() 
     { 

     //Instance of connection is created 
     SqlConnection sConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); 

     //To Open the connection. 
     sConnection.Open(); 
     string selectDatabase = @"SELECT 
             [NAME] 
            FROM  
             [master..sysdatabases]"; 

     //Instance of command is created. 
     SqlCommand sCommand = new SqlCommand(selectDatabase, sConnection); 

     try 
      { 
      //Create the dataset. 
      DataSet dsListOfDatabases = new DataSet("master..sysdatabases"); 

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

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

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

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

     } 

Aber wenn ich nenne es wie dieses

 public void BindDBDropDown() 
     { 

      DataSet dsTablesWithoutForeignKeys = default(DataSet); 

     try 
      { 
      //The function GetAllForeignKeyTables() is called from the class PluginManager. 
      dsTablesWithoutForeignKeys = DataAccessMaster.GetAllDataBaseNames(); 

      dgResultView.DataSource = dsTablesWithoutForeignKeys.Tables["master..sysdatabases"]; 
      } 
     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); 
      } 
     } 

Es ist die SQL-Ausnahme im catch-Block gefangen bekommen

Can Leute, bitte schaut es euch an.

+7

Was ist die SQL-Ausnahmemeldung? –

+0

Als ** best practice ** sollten Sie ** immer ** Ihre 'SqlConnection' und' SqlCommand' in 'using (.....) {.....}' Blöcke setzen, um sicherzustellen, dass sie es sind entsorgt werden, wenn man nicht mehr in Reichweite ist ... siehe Marc Gravells Antwort auf ein "How-To" –

Antwort

2

Versuchen:

SELECT [name] 
FROM  [master]..[sysdatabases] // **not** [master..sysdatabases] 

(beachten Sie die eckigen Klammern)

Auch; Es wäre weit vorzuziehen, using für SqlConnection, SqlCommand usw. zu verwenden - alles, was IDisposable implementiert - das wird sicherstellen, dass Sie keine Blutungen verursachen.

heißt

using(var sConnection = new 
    SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"])) 
{ 

    //To Open the connection. 
    sConnection.Open(); 
    string selectDatabase = @" 
     SELECT [NAME] 
     FROM [master]..[sysdatabases]"; 

    //Instance of command is created. 
    using(var sCommand = new SqlCommand(selectDatabase, sConnection)) { 
     // etc 
    } 
} 
Verwandte Themen