我面临的问题是,我无法在文本框c中显示mysql中的表。我试过这样的代码。请帮忙。感谢
string cons = "Server=server;DataBase=base;Uid=u0827;pwd=kkda";
MySqlConnection mycon = new MySqlConnection(cons);
mycon.Open();
string con = "SELECT usd FROM curs";
MySqlCommand com = new MySqlCommand(con);
MySqlDataAdapter da = new MySqlDataAdapter(com);
MySqlDataReader DR = com.ExecuteReader();
com.CommandText = @"select TOP 1 usd from curs";
object res = com.ExecuteScalar();
if (res != null) usd.Text = res.ToString();
DR.Close();
mycon.Close();
运行此代码时,会出现错误-System.InvalidOperationException:"连接必须有效且打开">
string cons = "Server=ser;DataBase=base;Uid=u082na;pwd=kkdNwijs";
MySqlConnection mycon = new MySqlConnection(cons);
mycon.Open();
string con = "select * from curs";
MySqlCommand com = new MySqlCommand(con, mycon);
MySqlDataReader myReader;
myReader = com.ExecuteReader();
try
{
while (myReader.Read())
{
usd.Text = (myReader.GetString(0));
}
}
finally
{
myReader.Close();
mycon.Close();
}