小弟想连接到本地数据库
通过筛选获得一个值赋给一个TEXTBOX里
例如表如下
id   name   score
01   Tom    70
02   Mary   80在SQL上查询"select id from point where name='Tom'"
结果为 01
我的源代码如下:
string connectionString = GetConnectionString();
SqlConnection sqlConnection = new SqlConnection(connectionString);
sqlConnection.Open();
string SQLStmt = " SELECT ID FROM point WHERE Name='Tom'";
SqlCommand myCommand = new SqlCommand(SQLStmt, sqlConnection);sqldata.Text = ??????????????????????????????????????????????????????????????????;
            
sqlConnection.Close();请问"???..."哪里应该怎样添加源代码
才能把结果输出到TEXTBOX里呢?

解决方案 »

  1.   

    myCommand.nonex具体忘了反正是n开头的一个方法
      

  2.   

    string connectionString = GetConnectionString(); 
    SqlConnection sqlConnection = new SqlConnection(connectionString); string SQLStmt = " SELECT ID FROM point WHERE Name='Tom'"; 
    SqlCommand myCommand = new SqlCommand(SQLStmt, sqlConnection); sqlConnection.Open(); 
    sqldata.Text = myCommand.ExecuteScalar().ToString();         
    sqlConnection.Close(); 
      

  3.   

    数字的话,用 ExecuteScalar,可能是其它类型的话,用 SqlDataReader,以 string 为例:
    using (SqlDataReader dr = myCommand.ExecuteReader())
    {
        if (dr.Read())
        {
            sqldata.Text = dr.GetString(0);
        }
        dr.Close();
    }
      

  4.   

    用ExecuteScalar()方法返回一个值