|
Post by dejohnny on Jun 20, 2012 12:53:49 GMT -5
A small console application used for testing database connection written in C#.
/* * Created by SharpDevelop. * User: djohnson * Date: 6/19/2012 * Time: 10:26 AM * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; using System.Data.SqlClient;
namespace TestSQLConnection { class TestDb { public static void Main(string[] args) { // ADO.NET connection string String myConnectionString = @"Server=tcp:server_name,1433; Database=db_name;User ID=some_userr@server_name;Password=some_password Trusted_Connection=False;Encrypt=True;Connection Timeout=30"; // Create new connection object SqlConnection myConnection = new SqlConnection(myConnectionString); try{ myConnection.Open(); // Query text String sql = @"SELECT table_A.column_name FROM table1 As table_A JOIN table2 As table_B ON table_A.unique_id = table_B.unique_id"; SqlCommand myCommand = new SqlCommand(sql, myConnection); // Create reader object SqlDataReader myDataReader = null; myDataReader = myCommand.ExecuteReader(); while(myDataReader.Read()){ Console.WriteLine( myDataReader["column_name"].ToString()); } myConnection.Close(); Console.ReadLine(); }catch(Exception ex){ Console.WriteLine(ex.ToString()); Console.ReadLine(); } } } }
|
|