Using System;
Using System.Data;
Using System.Data.SqlClient;
Namespace Dbutil
{
Class Program
{
static void Main (string[] args)
{
Connecting to a database
/**
* Note If it is a local database with TRUSTED_CONNECTION=SSPI;
* is the server words with UID,PWD and other account password to verify
*/
SqlConnection mycon = new SqlConnection ();
mycon.connectionstring = @ "Data source=localhost\sqlexpress;initial catalog=test; Trusted_connection=sspi ";
Try
{
Open Database
Mycon.open ();
To submit a query command using SqlCommand
SqlCommand sql = new SqlCommand ("SELECT * from Student", mycon);
Get Data adapter
SqlDataAdapter SDA = new SqlDataAdapter ();
Sda. SelectCommand = SQL;
Populating a DataSet
DataSet ds = new DataSet ();
Sda. Fill (ds, "Student");
Console.WriteLine ("{0}", ds. GETXML ());
}
catch (Exception ex)
{
Console.WriteLine (ex. Message);
}
Finally
{
Mycon.close ();
}
}
}
}