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

怎么将SQL查询出来的值 赋给 控件的text属性
conn = new SqlConnection System.Configuration.ConfigurationManager.ConnectionStrings["BBS_jshaoConnectionString"].ConnectionString);
  conn.Open();
  例如 this.labe1.text="selece id from table where id='1'"

  conn.Close();


------解决方案--------------------
C# code
    /// <summary>
    /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略额外的列或行
    /// </summary>
    /// <param name="strSQL">string</param>
    /// <returns>object</returns>
    public static object ExecuteScalarSql(string strSQL)
    {
        SqlConnection myCn = new SqlConnection(CONNECTION_STRING);
        SqlCommand myCmd = new SqlCommand(strSQL, myCn);
        try
        {
            myCn.Open();
            object result = myCmd.ExecuteScalar();
            return result;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            myCmd.Dispose();
            myCn.Close();
        }
    }