| |
![]() |
|
| |
![]() |
|
class Foo
{
static void Main()
{
/*Access 2003
string ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0; + "Data Source=c:\\foo.mdb"; */
/* Access 2007 */
string ConnectString = "provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=c:\\foo.accdb";
System.Data.OleDb.OleDbConnection Connection = new System.Data.OleDb.OleDbConnection(ConnectString);
try /* opening the db connection */
{
System.Console.WriteLine(ConnectString);
Connection.Open();
System.Console.WriteLine("Connection open...");
}
catch(System.Exception e)
{
System.Console.WriteLine("Problems opening..." + e.Message);
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
System.Environment.Exit(1);
}
try /* closing the connection */
{
Connection.Close();
System.Console.WriteLine("Connection closed...");
}
catch(System.Exception e)
{
System.Console.WriteLine("Problems closing..." + e.Message);
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
System.Environment.Exit(1);
}
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
System.Environment.Exit(0);
}
}
class Foo
{
static void Main()
{
string my_env = null;
my_env = System.Environment.GetEnvironmentVariable("vcsharpdb");
if (my_env == null)
{
System.Console.WriteLine("Problems getting env. var 'vcsharpdb'...");
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
System.Environment.Exit(1);
}
/*Access 2003
string ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0; + "Data Source=" + my_env; */
/* Access 2007 */
string ConnectString = "provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + my_env;
System.Data.OleDb.OleDbConnection Connection = new System.Data.OleDb.OleDbConnection(ConnectString);
try /* opening the db connection */
{
System.Console.WriteLine(ConnectString);
Connection.Open();
System.Console.WriteLine("Connection open...");
}
catch(System.Exception e)
{
System.Console.WriteLine("Problems opening..." + e.Message);
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
System.Environment.Exit(1);
}
try /* closing the connection */
{
Connection.Close();
System.Console.WriteLine("Connection closed...");
}
catch(System.Exception e)
{
System.Console.WriteLine("Problems closing..." + e.Message);
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
System.Environment.Exit(1);
}
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
System.Environment.Exit(0);
}
}
class Foo
{
static void Main()
{
string my_env = null;
my_env = System.Environment.GetEnvironmentVariable("vcsharpdb");
if (my_env == null)
{
System.Console.WriteLine("Problems getting env. var 'vcsharpdb'...");
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
System.Environment.Exit(1);
}
/*Access 2003
string ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0; + "Data Source=" + my_env; */
/* Access 2007 */
string ConnectString = "provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + my_env;
System.Data.OleDb.OleDbConnection Connection = new System.Data.OleDb.OleDbConnection(ConnectString);
System.Data.OleDb.OleDbCommand SQLCommand = null;
System.Data.OleDb.OleDbDataReader SQLDataReader = null;
string Result_string;
//open the connection
Connection = new System.Data.OleDb.OleDbConnection(ConnectString);
try
{
System.Console.WriteLine(ConnectString);
Connection.Open();
System.Console.WriteLine("Connection open...");
}
catch(System.Exception e)
{
System.Console.WriteLine("Problems opening..." + e.Message);
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
System.Environment.Exit(1);
}
// set the SQL statement
SQLCommand = new System.Data.OleDb.OleDbCommand("select my_name from foo", Connection);
//execute the statement
try
{
SQLDataReader = SQLCommand.ExecuteReader();
}
catch(System.Exception e)
{
System. Console.WriteLine("Problems executing..." + e.Message);
Connection.Close();
System. Console.WriteLine("Connection closed...");
System. Console.WriteLine();
System. Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
System.Environment.Exit(1);
}
/* at this point we know that execution succeeded so we can start reading the
results. Since we do not know how many records we get returned, we'll stick
the read in a while() loop that keeps executing until we no longer get
results from the reader. */
try
{
while (SQLDataReader.Read())
{
Result_string = SQLDataReader.GetString(0);
System.Console.WriteLine(Result_string);
}
SQLDataReader.Close();
}
catch(System.Exception e)
{
System.Console.WriteLine("Problems reading data..." + e.Message);
Connection.Close();
System.Console.WriteLine("Connection closed...");
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
System.Environment.Exit(1);
}
//we're done so we can close the connection
try
{
Connection.Close();
System.Console.WriteLine("Connection closed...");
}
catch(System.Exception e)
{
System.Console.WriteLine("Problems closing..." + e.Message);
}
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit.");
System.Console.ReadLine();
}
}