关于C#中查询出的数据库值赋值给textbox

2024-11-30 13:27:32
推荐回答(5个)
回答1:

using (SqlCommand cmd = new SqlCommand("select UserName from loginTable where Id="+LoginForm.UserInfo.UserId,sqlcon))
{
textbox.text= (string)cmd.ExecuteScalar();

回答2:

DataTable dt = new DataTable();
using(IDataReader reader = cmd.ExecuteReader())
{
dt.Load(reader);
}
if(dt.Rows.Count >0)
{
textbox1.Text=dt.Rows[0]["userName"].ToString();
}

回答3:

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (TextBox1.Text==dr[0].ToString())
{
//赋值
}
}

回答4:

object o;
using (SqlConnection conn = new SqlConnection(ConnStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
o = cmd.ExecuteScalar();
conn.Close();
}
try
{
textBox.Text = o.ToString();
}
catch (Exception ex)
{ textBox.Text = ex.Message;}

回答5:

库,得到连接对象conn
2、用下面的select语句打开表,将数据读入RecordSet
set rs=conn.execute("select * from 表名")
3、用循环将数据读入二维数组arr
row=0
while not rs.eof
arr[row,0]=rs["id"]
arr[row,1]=rs["year"]
arr[row,2]=rs["mon"]
arr[row,3]=rs["1"]
arr[row,4]=rs["2"]
...
...
...
row=row+1
wend