读书人

如何将SQL查询出来的值 赋给 控件的te

发布时间: 2012-01-15 22:57:48 作者: rapoo

怎么将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();        }    } 

读书人网 >C#

热点推荐