C # database connection code
/// System reference
Using system. Data;
Using system. Data. oledb;
Using system. Data. sqlclient;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Private system. Data. dataset mydataset;
C # local mdb database connection
Private void menuitem2_click (Object sender, system. eventargs E)
{
Try
{
// Create an oledbconnection object
String strcon = "provider = Microsoft. Jet. oledb.4.0; Data Source =.../../database/mdb database name. mdb ";
Oledbconnection myconn = new oledbconnection (strcon );
String strcom = "select * from data table name ";
// Create a DataSet object
Mydataset = new dataset ();
Myconn. open ();
Oledbdataadapter mycommand = new oledbdataadapter (strcom, myconn );
Mycommand. Fill (mydataset, "data table name ");
Myconn. Close ();
// Close the connection
Statusbar1.text = "Local mdb database connection successful ";
}
Catch (exception ex)
{
Statusbar1.text = "Local mdb database connection failed ";
MessageBox. Show ("An error occurred while connecting to the local mdb database:" + ex. tostring (), "error! ");
}
}
C # remote mdb database connection
C # local SQL database connection
Private void menuitem3_click (Object sender, system. eventargs E)
{
Try
{
// Create a sqlconnection object
String strcon = "Integrated Security = sspi; initial catcon = 'database name'; Data Source = 'local computer name'; user id = 'login username '; password = 'login user password'; Connect timeout = 30 ";
Sqlconnection myconn = new sqlconnection (strcon );
String strcom = "select * from data table name ";
// Create a DataSet object
Mydataset = new dataset ();
Myconn. open ();
Sqldataadapter mycommand = new sqldataadapter (strcom, myconn );
Mycommand. Fill (mydataset, "data table name ");
Myconn. Close ();
// Close the connection
Statusbar1.text = "The local SQL database is connected successfully ";
}
Catch (exception ex1)
{
Statusbar1.text = "connection to local SQL database failed ";
MessageBox. Show ("An error occurred while connecting to the local SQL database:" + ex1.tostring (), "error! ");
}
}
C # Remote SQL database connection
Private void menuitem10_click (Object sender, system. eventargs E)
{
Try
{
// Create a sqlconnection object
String strcon = "Initial catalog = 'database name'; server = 'remote IP address, 100'; user id = 'login username'; Password = 'login user password '; persist Security info = true ";
Sqlconnection myconn = new sqlconnection (strcon );
String strcom = "select * from data table name ";
// Create a DataSet object
Mydataset = new dataset ();
Myconn. open ();
Sqldataadapter mycommand = new sqldataadapter (strcom, myconn );
Mycommand. Fill (mydataset, "data table name ");
Myconn. Close ();
// Close the connection
Statusbar1.text = "the remote SQL database is successfully connected ";
}
Catch (exception ex2)
{
Statusbar1.text = "failed to connect to the remote SQL database ";
MessageBox. Show ("An error occurred while connecting to the remote SQL database:" + ex2.tostring (), "error! ");
}
}