返回顶部

收藏

C#查询SqlServer数据库并返回单个值

更多
static public string GetSqlAsString(string sqlText,
                       SqlParameter[] sqlParameters,
                       string databaseConnectionString) {
    string result = "";
    SqlDataReader reader;
    SqlConnection connection = new SqlConnection(databaseConnectionString);
    using (connection) {
        SqlCommand sqlcommand = connection.CreateCommand();
        sqlcommand.CommandText = sqlText;
        if (sqlParameters != null) {
            sqlcommand.Parameters.AddRange(sqlParameters);
        }
        connection.Open();
        reader = sqlcommand.ExecuteReader();
        if (reader != null)
            if (reader.Read()) {
                result = reader.GetString(0);
            }
    }
    return result;
}
//该片段来自于http://outofmemory.cn

标签:c#,.net,数据库

收藏

0人收藏

支持

0

反对

0

发表评论