-4


我创建了一个实体框架应用程序来检索数据库值,但我想在单个标签而不是 gridview 中显示它们?

EmployEntities2 fd = new EmployEntities2();
int idToupdate = Convert.ToInt32(TextBox1.Text);
  var jj = (from bn in fd.Contacts
              where bn.Id == idToupdate
              select bn);

  GridView1.DataSource = jj;
    GridView1.DataBind();
4

2 回答 2

0

建立连接

SqlConnection con = new SqlConnection("CONNECTION_STRING); SqlCommand cmd = new SqlCommand(); 接着,

cmd.CommandText = "select * from table where Condition ;
cmd.Connection = con

Label1.text = ((string)cmd.ExecuteScalar());

试试这个。。

于 2013-09-05T11:01:09.167 回答
0

您应该使用 SQLDataReader 类。根据结构中的数据类型,您应该调用 SQLDataReader 对象的不同方法。例如,如果您需要检索一个整数值并将其显示在标签中,这是执行此操作的代码 sinppet:

string queryString = "SELECT integer_value FROM table_name";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(queryString, connection);
    connection.open();
    SqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        label1.Text = reader.getSqlInt32(0).ToString();
    }
    reader.close();
}

这是我能做的最好的,因为你没有提供额外的信息。查看此链接以获取有关 SqlDataReader 类的信息:SqlDataReader 参考

于 2013-09-05T11:08:11.383 回答