将数组添加到我的 ExecuteReader() 函数
Add arrays to my ExecuteReader() function
我正在尝试从我在 MYSQL 中创建的数据库中获取数据。我相信我的代码中的 DML 是正确的。我在另一个线程上看到我需要使用数组,这样我就可以从我的 table 中获取所有行,但我正在努力研究如何将它们包含在我的代码中:
string cs = "server...";
MySqlConnection connection = new MySqlConnection(cs);
try
{
Console.WriteLine("Connection sur MySQL...");
connection.Open();
string Insertion ="insert into Passager(...)values(...)";
string Insert2 = "insert into Reservation(...)values(...)";
string req1 = "SELECT CodePassager,";
string req2 = "SELECT CodePassager,...";
string req3 = "SELECT CodePassager,...";
string joint = "SELECT Reservation...";
MySqlCommand NQuery = new MySqlCommand(Insertion, connection);
MySqlCommand NQuery2 = new MySqlCommand(Insert2, connection);
MySqlCommand requete1 = new MySqlCommand(req1, connection);
MySqlCommand requete2 = new MySqlCommand(req2, connection);
MySqlCommand requete3 = new MySqlCommand(req3, connection);
MySqlCommand reqjoint = new MySqlCommand(joint, connection);
NQuery.ExecuteNonQuery();
NQuery2.ExecuteNonQuery();
//1st request
using (MySqlDataReader reader1 = requete1.ExecuteReader())
{
reader1.Read();
int CodePassager;
string Nom, Prenom;
CodePassager = (int)reader1[0];
Nom = (string)reader1[1];
Prenom = (string)reader1[2];
Console.WriteLine("code: {0},nom:{1},prenom:{2}", CodePassager, Nom, Prenom);
reader1.Close();
}
//2nd request
using (MySqlDataReader reader2 = requete2.ExecuteReader())
{
reader2.Read();
int CodePassager;
string Nom, Prenom, Adresse;
CodePassager = (int)reader2[0];
Nom = (string)reader2[1];
Prenom = (string)reader2[2];
Adresse = (string)reader2[3];
Console.WriteLine("code:{0},...);
reader2.Close();
}
//3rd request
using (MySqlDataReader reader3 = requete3.ExecuteReader())
{
reader3.Read();
int CodePassager;
string Nom, Prenom, Adresse;
CodePassager = (int)reader3[0];
Nom = (string)reader3[1];
Prenom = (string)reader3[2];
Adresse = (string)reader3[3];
Console.WriteLine("code: {0}...);
Console.WriteLine("---------------------------------------------------");
Console.WriteLine("Reservation(s) pour ce client:");
Console.WriteLine("---------------------------------------------------");
reader3.Close();
//request to show every reservation per passenger
using (MySqlDataReader reader4 = reqjoint.ExecuteReader())
{
reader4.Read();
int CodeReservation;
string StatutReservation, DateReservation;
CodeReservation = (int)reader4[0];
StatutReservation = (string)reader4[1];
DateReservation = (string)reader4[2];
Console.WriteLine("code reservation:{0}...);
reader4.Close();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
connection.Close();
}
使用我当前的代码,每个请求只能得到一个结果,但我需要所有数据。
(抱歉,英语不是我的母语。)
如上所述,答案在 MySqlDataReader 的示例部分,但代码写得不是很好。更好的版本应该清楚如何从 select 查询中读取多个值:
public void ReadMyData(string myConnString) {
string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
using(MySqlConnection myConnection = new MySqlConnection(myConnString))
using(MySqlCommand myCommand = new MySqlCommand(mySelectQuery,myConnection))
{
myConnection.Open();
using(MySqlDataReader myReader = myCommand.ExecuteReader())
{
// Always call Read before accessing data.
while (myReader.Read()) {
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}
// always call Close when done reading.
myReader.Close();
}
// Close the connection when done with it.
myConnection.Close();
}
}
许多类型都是一次性的,因此它们可以与 using
部分一起使用。此外,MySqlConnection.Close
方法表明它是关闭连接的首选方式,因此应该手动关闭它而不是依赖于处置。尽管文档没有说明任何内容,但我对 MySqlDataReader.Close
的假设相同。
感谢所有回答的人,我发现了我的错误,直到你指出它我才完全理解 while() 并且我修复了它除了最后一个 while 需要我有一个 while 循环在一个开放的 reader(第 3 个和第 4 个请求)中,我只找到 this and this,我不确定我是否走对了路。
这就是我修复答案的方式:
//1st request
using (MySqlDataReader reader1 = requete1.ExecuteReader())
{
while (reader1.Read())
{
int CodePassager;
string Nom, Prenom;
CodePassager = (int)reader1[0];
Nom = (string)reader1[1];
Prenom = (string)reader1[2];
Console.WriteLine("code: {0}...");
}
reader1.Close();
}
//2nd request
using (MySqlDataReader reader2 = requete2.ExecuteReader())
{
while (reader2.Read())
{
int CodePassager;
string Nom, Prenom, Adresse;
CodePassager = (int)reader2[0];
Nom = (string)reader2[1];
Prenom = (string)reader2[2];
Adresse = (string)reader2[3];
Console.WriteLine("code: {0}...");
}
reader2.Close();
}
//3rd request
using (MySqlDataReader reader3 = requete3.ExecuteReader())
{
while (reader3.Read())
{
int CodePassager;
string Nom, Prenom, Adresse;
CodePassager = (int)reader3[0];
Nom = (string)reader3[1];
Prenom = (string)reader3[2];
Adresse = (string)reader3[3];
Console.WriteLine("code: {0}...");
reader3.Close();
using (MySqlDataReader reader4 = reqjoint.ExecuteReader())
{
while (reader4.Read())
{
int CodeReservation;
string StatutReservation, DateReservation;
CodeReservation = (int)reader4[0];
StatutReservation = (string)reader4[1];
DateReservation = (string)reader4[2];
Console.WriteLine("code...");
}
reader4.Close();
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
connection.Close();
}
}
}
我正在尝试从我在 MYSQL 中创建的数据库中获取数据。我相信我的代码中的 DML 是正确的。我在另一个线程上看到我需要使用数组,这样我就可以从我的 table 中获取所有行,但我正在努力研究如何将它们包含在我的代码中:
string cs = "server...";
MySqlConnection connection = new MySqlConnection(cs);
try
{
Console.WriteLine("Connection sur MySQL...");
connection.Open();
string Insertion ="insert into Passager(...)values(...)";
string Insert2 = "insert into Reservation(...)values(...)";
string req1 = "SELECT CodePassager,";
string req2 = "SELECT CodePassager,...";
string req3 = "SELECT CodePassager,...";
string joint = "SELECT Reservation...";
MySqlCommand NQuery = new MySqlCommand(Insertion, connection);
MySqlCommand NQuery2 = new MySqlCommand(Insert2, connection);
MySqlCommand requete1 = new MySqlCommand(req1, connection);
MySqlCommand requete2 = new MySqlCommand(req2, connection);
MySqlCommand requete3 = new MySqlCommand(req3, connection);
MySqlCommand reqjoint = new MySqlCommand(joint, connection);
NQuery.ExecuteNonQuery();
NQuery2.ExecuteNonQuery();
//1st request
using (MySqlDataReader reader1 = requete1.ExecuteReader())
{
reader1.Read();
int CodePassager;
string Nom, Prenom;
CodePassager = (int)reader1[0];
Nom = (string)reader1[1];
Prenom = (string)reader1[2];
Console.WriteLine("code: {0},nom:{1},prenom:{2}", CodePassager, Nom, Prenom);
reader1.Close();
}
//2nd request
using (MySqlDataReader reader2 = requete2.ExecuteReader())
{
reader2.Read();
int CodePassager;
string Nom, Prenom, Adresse;
CodePassager = (int)reader2[0];
Nom = (string)reader2[1];
Prenom = (string)reader2[2];
Adresse = (string)reader2[3];
Console.WriteLine("code:{0},...);
reader2.Close();
}
//3rd request
using (MySqlDataReader reader3 = requete3.ExecuteReader())
{
reader3.Read();
int CodePassager;
string Nom, Prenom, Adresse;
CodePassager = (int)reader3[0];
Nom = (string)reader3[1];
Prenom = (string)reader3[2];
Adresse = (string)reader3[3];
Console.WriteLine("code: {0}...);
Console.WriteLine("---------------------------------------------------");
Console.WriteLine("Reservation(s) pour ce client:");
Console.WriteLine("---------------------------------------------------");
reader3.Close();
//request to show every reservation per passenger
using (MySqlDataReader reader4 = reqjoint.ExecuteReader())
{
reader4.Read();
int CodeReservation;
string StatutReservation, DateReservation;
CodeReservation = (int)reader4[0];
StatutReservation = (string)reader4[1];
DateReservation = (string)reader4[2];
Console.WriteLine("code reservation:{0}...);
reader4.Close();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
connection.Close();
}
使用我当前的代码,每个请求只能得到一个结果,但我需要所有数据。
(抱歉,英语不是我的母语。)
如上所述,答案在 MySqlDataReader 的示例部分,但代码写得不是很好。更好的版本应该清楚如何从 select 查询中读取多个值:
public void ReadMyData(string myConnString) {
string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
using(MySqlConnection myConnection = new MySqlConnection(myConnString))
using(MySqlCommand myCommand = new MySqlCommand(mySelectQuery,myConnection))
{
myConnection.Open();
using(MySqlDataReader myReader = myCommand.ExecuteReader())
{
// Always call Read before accessing data.
while (myReader.Read()) {
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}
// always call Close when done reading.
myReader.Close();
}
// Close the connection when done with it.
myConnection.Close();
}
}
许多类型都是一次性的,因此它们可以与 using
部分一起使用。此外,MySqlConnection.Close
方法表明它是关闭连接的首选方式,因此应该手动关闭它而不是依赖于处置。尽管文档没有说明任何内容,但我对 MySqlDataReader.Close
的假设相同。
感谢所有回答的人,我发现了我的错误,直到你指出它我才完全理解 while() 并且我修复了它除了最后一个 while 需要我有一个 while 循环在一个开放的 reader(第 3 个和第 4 个请求)中,我只找到 this and this,我不确定我是否走对了路。 这就是我修复答案的方式:
//1st request
using (MySqlDataReader reader1 = requete1.ExecuteReader())
{
while (reader1.Read())
{
int CodePassager;
string Nom, Prenom;
CodePassager = (int)reader1[0];
Nom = (string)reader1[1];
Prenom = (string)reader1[2];
Console.WriteLine("code: {0}...");
}
reader1.Close();
}
//2nd request
using (MySqlDataReader reader2 = requete2.ExecuteReader())
{
while (reader2.Read())
{
int CodePassager;
string Nom, Prenom, Adresse;
CodePassager = (int)reader2[0];
Nom = (string)reader2[1];
Prenom = (string)reader2[2];
Adresse = (string)reader2[3];
Console.WriteLine("code: {0}...");
}
reader2.Close();
}
//3rd request
using (MySqlDataReader reader3 = requete3.ExecuteReader())
{
while (reader3.Read())
{
int CodePassager;
string Nom, Prenom, Adresse;
CodePassager = (int)reader3[0];
Nom = (string)reader3[1];
Prenom = (string)reader3[2];
Adresse = (string)reader3[3];
Console.WriteLine("code: {0}...");
reader3.Close();
using (MySqlDataReader reader4 = reqjoint.ExecuteReader())
{
while (reader4.Read())
{
int CodeReservation;
string StatutReservation, DateReservation;
CodeReservation = (int)reader4[0];
StatutReservation = (string)reader4[1];
DateReservation = (string)reader4[2];
Console.WriteLine("code...");
}
reader4.Close();
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
connection.Close();
}
}
}