在 C# 中使用 MySqlDataReader
Use MySqlDataReader in C#
我正在开发一个使用 DAO 模式的应用程序。 MySql
中的连接位于我的 DAOFactory-Class 中:
namespace GSB
{
class DAOFactory
{
private MySqlConnection connectionBDD;
public DAOFactory()
{
this.InitConnexion();
}
public void InitConnexion()
{
string connexion = "SERVER=127.0.0.1; DATABASE=gsb_c#; UID=root; PASSWORD=root";
this.connectionBDD = new MySqlConnection(connexion);
}
private bool OpenConnection()
{
try
{
connectionBDD.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
Console.WriteLine("Cannot connect to server");
break;
case 1045:
Console.WriteLine("Invalid UserName/Password");
break;
}
return false;
}
}
public void deconnexion()
{
this.connectionBDD.Close();
}
public void execSqlRead(String req)
{ //à remplir
this.OpenConnection();
MySqlCommand cmd = this.connectionBDD.CreateCommand();
MySqlDataReader myReader;
myReader= cmd.ExecuteReader(); //stop here
try
{
while (myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
}
finally
{
Console.WriteLine("Yolo");
this.deconnexion();
}
//return myReader;
}
public void execSqlWrite(String req)
{
this.OpenConnection();
MySqlCommand cmd = this.connectionBDD.CreateCommand();
//Création d'une commande SQL en fonction de l'objet connection
cmd.CommandText = req; //on entre la req sql
cmd.ExecuteNonQuery(); //on ex la req
this.deconnexion();
}
}
}
目前我的 execSqlWrite
方法正在工作,但我的 exexSqlRead
没有,因为当我想执行 reader 时它已经停止了。
我不知道如何解决这个问题,我只想从我选择的请求中获取结果。
我该怎么做?
如果仔细查看 Writer 代码,您会看到该命令接收命令字符串。 reader 代码
中缺少此行
public void execSqlRead(String req)
{
this.OpenConnection();
MySqlCommand cmd = this.connectionBDD.CreateCommand();
cmd.CommandText = req;
MySqlDataReader myReader;
myReader= cmd.ExecuteReader(); //stop here
try
{
while (myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
}
finally
{
Console.WriteLine("Yolo");
this.deconnexion();
}
//return myReader;
}
我正在开发一个使用 DAO 模式的应用程序。 MySql
中的连接位于我的 DAOFactory-Class 中:
namespace GSB
{
class DAOFactory
{
private MySqlConnection connectionBDD;
public DAOFactory()
{
this.InitConnexion();
}
public void InitConnexion()
{
string connexion = "SERVER=127.0.0.1; DATABASE=gsb_c#; UID=root; PASSWORD=root";
this.connectionBDD = new MySqlConnection(connexion);
}
private bool OpenConnection()
{
try
{
connectionBDD.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
Console.WriteLine("Cannot connect to server");
break;
case 1045:
Console.WriteLine("Invalid UserName/Password");
break;
}
return false;
}
}
public void deconnexion()
{
this.connectionBDD.Close();
}
public void execSqlRead(String req)
{ //à remplir
this.OpenConnection();
MySqlCommand cmd = this.connectionBDD.CreateCommand();
MySqlDataReader myReader;
myReader= cmd.ExecuteReader(); //stop here
try
{
while (myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
}
finally
{
Console.WriteLine("Yolo");
this.deconnexion();
}
//return myReader;
}
public void execSqlWrite(String req)
{
this.OpenConnection();
MySqlCommand cmd = this.connectionBDD.CreateCommand();
//Création d'une commande SQL en fonction de l'objet connection
cmd.CommandText = req; //on entre la req sql
cmd.ExecuteNonQuery(); //on ex la req
this.deconnexion();
}
}
}
目前我的 execSqlWrite
方法正在工作,但我的 exexSqlRead
没有,因为当我想执行 reader 时它已经停止了。
我不知道如何解决这个问题,我只想从我选择的请求中获取结果。
我该怎么做?
如果仔细查看 Writer 代码,您会看到该命令接收命令字符串。 reader 代码
中缺少此行public void execSqlRead(String req)
{
this.OpenConnection();
MySqlCommand cmd = this.connectionBDD.CreateCommand();
cmd.CommandText = req;
MySqlDataReader myReader;
myReader= cmd.ExecuteReader(); //stop here
try
{
while (myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
}
finally
{
Console.WriteLine("Yolo");
this.deconnexion();
}
//return myReader;
}