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);
}
}
Mettez le code 'MySqlHelper' ici –
@AliAdlavaran J'ai édité ma question – user2877820
où est l'initialisation de' _connection'? mettre le code –