Connect to an SQL Server DB

using System;

usingSystem.Data;

usingSystem.Data.SqlClient;

namespace ConsoleApplication2

{

classSqlServerAccessTest

{

privateIDbConnectionsqlServerConn;

privatestringsqlServerName = "CHEETAH\\CSCSQLSERVER";

privatestringdatabaseName = "sample";

privatevoidtestConn()

{

stringconnString = "Data Source=" + sqlServerName +

"; Initial Catalog=" + databaseName +

";User ID=ist318std;Password=password";

System.Console.WriteLine(connString);

System.Console.Read();

sqlServerConn = newSqlConnection(connString);

sqlServerConn.Open();

System.Console.WriteLine("Connection established..");

System.Console.Read();

}

staticvoid Main(string[] args)

{

SqlServerAccessTest test = newSqlServerAccessTest();

test.testConn();

}

}

}

Retrieving data from an Access DB and then display results to console

using System;

usingSystem.Data.OleDb;

namespace workshop2

{

classDBAccessor

{

publicstaticvoid Main()

{

Stringdbstring = "Provider=Microsoft.ACE.OLEDB.12.0;" +

@"data source=c:\test.accdb";

OleDbConnection con = newOleDbConnection(dbstring);

con.Open();

Console.WriteLine("connected!");

OleDbCommandcmd = con.CreateCommand();

cmd.CommandText = "select * from student";

OleDbDataReader reader = cmd.ExecuteReader();

Console.WriteLine("Retrieving Student Info...");

Console.WriteLine("StdId \t\tName \t\tActive?");

while (reader.Read())

{

Console.WriteLine("{0} \t{1} \t{2}", reader.GetString(1),

reader.GetString(2) + " " + reader.GetString(3),

reader.GetBoolean(4));

}

con.Close();

Console.Read();

}

}

}

Retrieving data from an Access DB and then display results in a form

using System;

usingSystem.Data.OleDb;

usingSystem.Windows.Forms;

usingSystem.Drawing;

namespace workshop2

{

classDBAccessor

{

publicstaticvoid Main()

{

Stringdbstring = "Provider=Microsoft.ACE.OLEDB.12.0;" +

@"data source=c:\test.accdb";

stringtransString = "";

OleDbConnection con = newOleDbConnection(dbstring);

con.Open();

Console.WriteLine("connected!");

OleDbCommandcmd = con.CreateCommand();

cmd.CommandText = "select * from student";

OleDbDataReader reader = cmd.ExecuteReader();

Console.WriteLine("Retrieving Student Info...");

Console.WriteLine("StdId \t\tName \t\tActive?");

transString = "StdId \t\tName \t\tActive?\n";

while (reader.Read())

{

Console.WriteLine("{0} \t{1} \t{2}", reader.GetString(1),

reader.GetString(2) + " " + reader.GetString(3),

reader.GetBoolean(4));

transString += reader.GetString(1) + " \t" +

reader.GetString(2) + " " + reader.GetString(3) + " \t" +

reader.GetBoolean(4) + "\n";

}

con.Close();

TranscriptsViewer viewer = newTranscriptsViewer();

viewer.SetTrans(transString);

Application.Run(viewer);

//Console.Read();

}

}

classTranscriptsViewer : System.Windows.Forms.Form

{

privatestring trans;

publicTranscriptsViewer()

{

Size = newSize(300, 500);

Text = "Transcripts Viewer";

}

publicvoidSetTrans(string trans)

{

this.trans = trans;

}

protectedoverridevoidOnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

g.DrawString(trans, Font, Brushes.BlueViolet, 20f, 20f);

base.OnPaint(e);

}

}

}