| |
![]() |
|
| |
![]() |
|

private void QueryButton_Click(object sender, EventArgs e)
{
//code for the query and listing the results in ResultList goes here
}
private void CloseButton_Click(object sender, EventArgs e)
{
//Handles CloseButton_Click()
}
public class DB
{
public static System.Data.OleDb.OleDbConnection Connection;
public static void DBOpen()
{
//Access 2007
string ConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=c:\\temp\\foo.accdb";
Connection = new System.Data.OleDb.OleDbConnection(ConnectString);
try { Connection.Open(); }
catch(System.Exception e)
{
//Pass on the exception to the caller
throw e;
}
}
public static void DBClose()
{
try { Connection.Close(); }
catch(System.Exception e)
{
//Pass on the exception to the caller
throw e;
}
}
}
private void QueryButton_Click(object sender, EventArgs e)
{
try { DB.DBOpen(); }
catch (System.Exception openex)
{
MessageBox.Show("Error opening database", openex.Message);
System.Environment.Exit(1);
}
//query goes here
try { DB.DBClose(); }
catch (System.Exception closeex)
{
MessageBox.Show("Error closing database", closeex.Message);
System.Environment.Exit(1);
}
}
private void CloseButton_Click(object sender, EventArgs e)
{
Dispose();
}
System.Data.OleDb.OleDbCommand SQLCommand;
System.Data.OleDb.OleDbDataReader SQLDataReader = null;
string result_string;
//Open the database connection. Exit the app if this fails
try { DB.DBOpen(); }
catch(System.Exception ex)
{
MessageBox.Show("Problem opening database..." + ex.Message);
//Since we have a problem, we exit with a nonzero status.
System.Environment.Exit(1);
}
//Connection is open; now do the query
SQLCommand = new System.Data.OleDb.OleDbCommand("select my_name from foo", DB.Connection); //modify this query to fit your database
try { SQLDataReader = SQLCommand.ExecuteReader(); }
catch(System.Exception f)
{
MessageBox.Show("Problem in ExecuteReader..." + f.Message);
//Query failed; try closing the database connection on our way out
try { DB.DBClose(); }
catch(System.Exception g) { MessageBox.Show("Problem closing database..." + g.Message); }
System.Environment.Exit(1);
}
//Data got retrieved from the database; now get it from the reader; one record at the time
//and add it to the resultList
try
{
while (SQLDataReader.Read())
{
result_string = SQLDataReader.GetString(0);
ResultList.Items.Add(result_string);
}
}
catch(System.Exception h)
{
MessageBox.Show("Problems reading data..." + h.Message);
//Query failed; try closing the database connection on our way out
try { DB.DBClose(); }
catch(System.Exception i) { MessageBox.Show("Problem closing database..." + i.Message); }
System.Environment.Exit(1);
}
//Query succeeded; close the database connection
try { DB.DBClose(); }
catch(System.Exception ex)
{
MessageBox.Show("Problem closing database..." + ex.Message);
//Since we have a problem, we exit with a nonzero status.
System.Environment.Exit(1);
}
private void QueryButton_Click(object sender, EventArgs e)
{
DataTable dt;
System.Data.OleDb.OleDbDataAdapter myAdapter = null;
string QueryString = "select my_name as 'my_name', my_name as 'another my_name' from foo"; //modify this query to fit your database
try { DB.DBOpen(); }
catch(System.Exception ex)
{
MessageBox.Show("Problem opening database..." + ex.Message);
System.Environment.Exit(1);
}
try { myAdapter = new System.Data.OleDb.OleDbDataAdapter(QueryString, DB.Connection); }
catch(System.Exception g)
{
MessageBox.Show("Problem creating dataAdapter..." + g.Message);
try { DB.DBClose(); }
catch(System.Exception h) { MessageBox.Show("Problem closing database..." + h.Message); }
System.Environment.Exit(1);
}
dt = new DataTable();
myAdapter.Fill(dt);
myAdapter.Dispose();
DataGrid.DataSource = dt;
Refresh();
}