本文實例講述了C#查詢SqlServer數據庫並返回單個值的方法。分享給大家供大家參考。具體實現方法如下:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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#程序設計有所幫助。