C # connect MDB and SQL database code

Source: Internet
Author: User

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! ");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.