Pinned
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
// Replace with your connection string
string connectionString = @"Data Source=.;Initial Catalog=BH_Report;Integrated Security=true;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open(); // Open the connection
Console.WriteLine("Connection successful!");
// Example of executing a query
string query = "SELECT * FROM YourTable"; // Replace with your table name
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader[0]); // Print the first column of each row
}
reader.Close();
}
catch (SqlException ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
} // Connection is automatically closed here
}
}