C#中怎么用label访问mysql数据库里的内容 数据库里 有 id title descrption typeid 要访问输出descrption的内容 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先用sql语句把你需要的descrption取出来用string变量存放起来,然后再把变量赋给lable的Text属性就可以了 string sql="select descrption from 表名";SqlCommand command=new SqlCommand(sql,conn);conn.Open();SqlDataReader reader=command.ExecuteReader();string descrption="";if(reader.Read()){ descrption =(string)reader["descrption"];}reader.Close(); 楼上的把所有的数据都读到descrption里面了,觉得用DataTable好,能一个lable对一条数据。不过还是要看楼主 正解 不过 conn没有给出 还要 using MySql.Data.MySqlClient;MySqlConnection conn = new MySqlConnection("Database=DBName;Data Source=localhost;User Id=abc;Password=123");这儿有个完整的using MySql.Data.MySqlClient; public string MySQLResults() { MySqlConnection conn = null; MySqlCommand command = null; MySqlDataReader reader = null; string strReturn = null; try { conn = new MySqlConnection("Database=数据库名字;Data Source=localhost;User Id=abc;Password=123"); command = conn.CreateCommand(); command.CommandText = "select descrption from 表名 where id="; conn.Open(); reader = command.ExecuteReader(); while (reader.Read()) { strReturn = reader[0].ToString(); } } catch (MySqlException se) { return null; } finally { conn.Close(); reader.Close(); } } 然后 lable.Text = MySQLResults(); 就好了 水晶报表ReportView导出excel文件的问题 关于IIS站点属性更新的问题(100) tabControl改变选项内容 c#怎样从文本文档指定行开始读取内容呢? tabcontrol简单问题 ArrayList类什么用途? 各位快来帮帮忙呀,急呀!! 关于UserControl开发的问题? c#数字变文字 想到北京发展,请各位兄弟姐妹告诉我工资报价 如何快速、高效、高质量的开发.net应用程序? ■■请教点小问题哦●●
descrption =(string)reader["descrption"];
}reader.Close();
不过还是要看楼主
正解 不过 conn没有给出 还要 using MySql.Data.MySqlClient;
MySqlConnection conn = new MySqlConnection("Database=DBName;Data Source=localhost;User Id=abc;Password=123");这儿有个完整的using MySql.Data.MySqlClient; public string MySQLResults()
{
MySqlConnection conn = null;
MySqlCommand command = null;
MySqlDataReader reader = null;
string strReturn = null;
try
{
conn = new MySqlConnection("Database=数据库名字;Data Source=localhost;User Id=abc;Password=123");
command = conn.CreateCommand();
command.CommandText = "select descrption from 表名 where id=";
conn.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
strReturn = reader[0].ToString();
}
}
catch (MySqlException se)
{
return null;
}
finally
{
conn.Close();
reader.Close();
}
}
就好了