2017-07-20 4 views
0

Je ne suis donc pas en mesure de remplir un DataSet. Je reçois l'exception suivante:Impossible de remplir DataSet mais MySqlCommand fonctionne correctement

A network-related or instance-specific error occurred while establishing a connection to SQL Server.

The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.

(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 1326)

Mais avec le même ConnectionString je suis en mesure d'exécuter une requête en utilisant la classe MySqlCommand.

Cela fonctionne ici:

var con = ConfigurationManager.ConnectionStrings["conString"]; 
_helper = new MySqlHelper(con.ConnectionString); 
string query = "SELECT Bezeichnung FROM stationen"; 
var reader = _helper.ExecuteReader(query); 
while(reader.Read()) 
{ 
    //do something 
} 

Mais si j'utilise DataSets, je reçois l'erreur en essayant de remplir le DataSet

var con = ConfigurationManager.ConnectionStrings["conString"]; 
string query = "SELECT Bezeichnung FROM stationen"; 

using (SqlDataAdapter adapter = new SqlDataAdapter(query, con.ConnectionString)) 
{ 
    var dataset = new U2ZFDataSet(); 
    adapter.Fill(dataset, "stationen"); 
} 

Qu'est-ce que je fais mal? Pourquoi le DataSet ne fonctionne pas correctement?

Aussi, voici le code MySqlHelpter:

private MySqlConnection _connection; 

public MySqlHelper(string connectionString) 
{ 
    try 
    { 
     _connection = new MySqlConnection(connectionString); 
     _connection.Open(); 
    } 
    catch (Exception ex) 
    { 
     if (_connection != null) 
     { 
      _connection.Dispose(); 
      _connection = null; 
     } 

     throw new Exception(connectionString, ex); 
    } 
} 

public MySqlDataReader ExecuteReader(string sqlString) 
{ 
    try 
    { 
     if (_connection == null || _connection.State != System.Data.ConnectionState.Open) 
      throw new Exception("Connection closed"); 

     using (MySqlCommand command = new MySqlCommand(sqlString, _connection)) 
     { 
      return command.ExecuteReader(); 
     } 
    } 
    catch (Exception ex) 
    { 
     throw new Exception(sqlString, ex); 
    } 
} 
+0

Mettez le code 'MySqlHelper' ici –

+0

@AliAdlavaran J'ai édité ma question – user2877820

+0

où est l'initialisation de' _connection'? mettre le code –

Répondre

1

Vous devez ouvrir la connexion avant d'utiliser le SqlDataAdapter. Dans le premier exemple, vous utilisez l'assistant, qui le fait pour vous mais dans le second cas, vous n'utilisez plus l'assistant.

var con = ConfigurationManager.ConnectionStrings["conString"]; 
string query = "SELECT Bezeichnung FROM stationen"; 

con.Open(); 

using (SqlDataAdapter adapter = new SqlDataAdapter(query, con.ConnectionString)) 
{ 
    var dataset = new U2ZFDataSet(); 
    adapter.Fill(dataset, "stationen"); 
} 

con.Close() -- do not forget to close it as well 

Vérifiez cet exemple here.

Il serait préférable que vous enveloppiez l'ouverture de connexion dans un bloc try-catch, comme vous le faites dans la classe d'assistance.