在最近的几天里,我试图从SQL表中获取数据并将其放入文本框。
表名称:“检查”。
我正在使用的代码:
SqlDataReader myReader = null; connection = new SqlConnection(System.Configuration.ConfigurationManager .ConnectionStrings["ConnectionString"].ConnectionString); connection.Open(); var command = new SqlCommand("SELECT * FROM [check]", connection); myReader = command.ExecuteReader(); while (myReader.Read()) { TextBox1.Text = myReader.ToString(); } connection.Close();
结果我什么也没得到。有人知道为什么吗?也许我没有正确调用SQL?
using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) using (var command = connection.CreateCommand()) { command.CommandText = "SELECT ColumnName FROM [check]"; connection.Open(); using (var reader = command.ExecuteReader()) { while (reader.Read()) TextBox1.Text = reader["ColumnName"].ToString(); } }
一些评论:
*
using
Text