如何将数据库信息显示到C#textbox控件里

例如:2005SQL数据库中有姓名name。性别sex,
C#窗体里有二个textbox控件,怎样才能把姓名和性别在这二个数据显示窗体中,那位好心的大哥哥帮帮小弟啊~~~~~~

是不是可以这样理解 - 把登陆窗体中TextBox和ComBoBox中的值传到窗体2,然后在窗体2用TextBox显示出来?

如果是这样的话--首先在窗体2中定义2个变量用来接收从登陆窗体传过来的值
窗体2定义的变量用Public修饰
Public string name = "";
public string type = "";
Form2窗体的Load方法
TextBox1.Text = name + type;

在登陆窗体的Button按钮里面写
From2 f2 = new Form2();
f2.name = this.TextBox1.Text;
f2.type = this.ComboBox1.SelectedItem.ToString();
f2.Show();
另外,团IDC网上有许多产品团购,便宜有口碑
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2017-11-24
private void DataBind()
{
DataTable dt = GetData(); //从数据库获取数据
txtName.Text=dt.Rows[0]["name"].ToString();
txtSex.Text=dt.Rows[0]["sex"].ToString();
}

在窗体加载时调用这个方法。本回答被网友采纳
第2个回答  2011-07-05
先把值从数据库取出来 然后赋值给已经定义好的控件就可以了!程序加载的时候执行
txtName.Text=dt.Rows[0]["name"].ToString();
txtSex.Text=dt.Rows[0]["sex"].ToString();
第3个回答  2011-06-30
protected void Page_Load(object sender, EventArgs e)
{
getStudent_sourceIdByName()
}
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = "server=localhost;database=数据库名;uid=sa;password=123456";

if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}

private void getStudent()
{
String sql = " SELECT * from dbo.UserInfo ";
SqlCommand cmd = new SqlCommand(sql, Connection);
DataTable dt = (int)cmd.ExecuteReader();
txtName.Text=dt.Rows[0]["name"].ToString();
txtSex.Text=dt.Rows[0]["sex"].ToString();

}

我是从“上海全鼎软件学院”毕业的————————
第4个回答  2011-07-02
把数据从数据库表中取出来,直接用控件接就可以了
相似回答