日期:2014-05-18  浏览次数:20862 次

请教个textbox的问题

想把数据库的内容直接放到textbox里面,如何操作?

textbox好像没有数据库邦定的操作.又不想使用datagridview.请教了

------解决方案--------------------
先建立一个与数据库的连接,然后用做查询,之后将查询结果赋值给textbox.text;
[code=C#][/code]
using System.Data;
using System.Data.SqlClient;


string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "Data Source=..服务器名;" + "Initial Catalog=..数据库名;Integrated Security=True"; 
string strCmd = "select ..查询";
SqlCommand myCommand = new SqlCommand(strCmd, myConnection);
myCommand.Connection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
TextBox.Text = myReader.GetString(0);.........;
}
myReader.Close();
myCommand.Connection.Close();
myConnection.Close();
------解决方案--------------------
要看你查出来的是什么类型的值了,如果你只返回一个string,直接对TextBox.txt赋值就可以了
如果是Table类型的,你就要从table中取出你要用的那一列
比如:table.Rows[n+1/行索引].cell[n+1/第几个单元格].ToString();