You want to use the ADO.NET functions in the System.Data.SqlClient namespace. ExecuteScalar is an easy-to-use method when you want to get only one result. For multiple results, you can use SqlDataReader.
using System.Data.SqlClient; string resultVar = String.Empty; string ServerName="localhost"; string DatabaseName="foo"; SqlConnection conn=new SqlConnection(String.Format("Data Source={0};Initial Catalog={1};Integrated Security=SSPI",ServerName,DatabaseName)); SqlCommand cmd=new SqlCommand(Query,conn); try { conn.Open(); } catch (SqlException se) { throw new InvalidOperationException(String.Format( "Connection error: {0} Num:{1} State:{2}", se.Message,se.Number, se.State)); } resultVar = (string)cmd.ExecuteScalar().ToString(); conn.Close();
source share