本文实例讲述了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;}
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答