我想要两列的结果,而不仅仅是一列。我试过了:
MessageBox.Show(reader.GetString("traceid", "idref"));
但它不起作用。我是C#的初学者,有人可以帮忙吗?
String str = @"server=localhost;database=asianimport;userid=tera;password=******;";
MySqlConnection con = null;
try
{
con = new MySqlConnection(str);
con.Open(); //open the connection
MessageBox.Show("connect " );
String cmdText = "SELECT * FROM tracerecord limit 3";
MySqlCommand cmd = new MySqlCommand(cmdText, con);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MessageBox.Show(reader.GetString("traceid", "idref"));
}
}
答案 0 :(得分:3)
执行GetString()
两次,每场一次:
while (reader.Read())
{
String traceID = reader.GetString("traceid");
String idRef = reader.GetString("idref");
MessageBox.Show(traceID + " - " + idRef);
}
答案 1 :(得分:2)
reader.GetString()仅返回当前记录的一列结果。对于每个具有连接的colunm,分别使用它们:
MessageBox.Show(reader.GetString("traceid")+ " "+reader.GetString("idref"));