C#中的查询数据库问题

来源:百度知道 编辑:UC知道 时间:2024/06/04 18:52:35
比如我的help类的文本框控件:txtage.text 要显示我的数据库中表:age的A列的标识列为5的数据如何实现呢?

先查出标识为5的一行数据 放到dataset里
DataSet ds=new DataSet();
然后 txtage.text=ds.Tables[0].Rows[0]["A"].toString();

sql语句:select A from 表名 where A=5
查出数据之后绑定到txtage.text

写个sql查询A列值为5的语句,连接数据可以得到的

public static string GetAge()
{
const string connString = "Data Source=localhost;Initial Catalog=DataBaseName;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
string sql = "select * from age where id=5";
string result = null;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
result = reader["A"].ToString();
reader.Close();
reader.Di