1

我想要两列的结果,而不仅仅是一列。我试过了:

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"));
    }
}
4

2 回答 2

3

执行GetString()两次,每个字段一次:

while (reader.Read())
{
    String traceID = reader.GetString("traceid");
    String idRef = reader.GetString("idref");
    MessageBox.Show(traceID + " - " + idRef);
}
于 2013-04-16T07:53:18.667 回答
2

reader.GetString() 仅从当前记录的一列返回结果。将它们分别用于具有串联的每个列:

MessageBox.Show(reader.GetString("traceid")+ " "+reader.GetString("idref")); 
于 2013-04-16T07:53:31.073 回答