【发布时间】:2017-07-22 05:24:44
【问题描述】:
我正在开发一个使用 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 不是因为当我想执行我的阅读器时它已经停止了。
我不知道如何解决这个问题,我只想从我选择的请求中获取结果。
我该怎么做?
【问题讨论】:
-
你的读者需要一个命令来执行,你永远不会在你的读者中为
cmd.CommandText分配任何东西,我假设SELECT语句是你作为req传递的,所以你需要@987654327 @
标签: c# mysql visual-studio dao