C # read/write Access database and table datagridview form display code instance

Source: Internet
Author: User
C # read/write Access database and table datagridview form display code instance recently used in the project C # Read/Write Access database tables. mdb operations, learn about the relevant things, here first sort out C # operations on the Access database, the operations on the MySQL and Oracle databases are put behind and then write. Access is a compilation of Microsoft databases.

C # read/write Access database and table datagridview form display code instance recently used in the project C # Read/Write Access database tables. mdb operations, learn about the relevant things, here first sort out C # operations on the Access database, the operations on the MySQL and Oracle databases are put behind and then write. Access is a compilation of Microsoft databases.

C # read/write Access database and table datagridview form display code instance

Recently, C # is used to read and write Access database tables. mdb operations, learn about the relevant things, here first sort out C # operations on the Access database, the operations on the MySQL and Oracle databases are put behind and then write.

Access is the Microsoft database editing software. The database file generated by Access is. mdb or. accdb. Therefore, in Visual Studio, you do not need to use the MySQL database driver as you do when manipulating mysql. There are several types of database manipulation in the system library. Below are several main classes used:

System. Data;

System. Data. OleDb;

System. Data. Odbc;

System. Data. SqlClient;

Let's briefly describe the basic operating principles of C # Access database:

C # operation Access connection string

String connectionString = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = mydata. mdb ";

C # create a connection through Access

OleDbConnection connection = new OleDbConnection (connectionString );

C # Use OleDbCommand to execute SQL statements in operation Access

OleDbCommand cmd = new OleDbCommand (SQL, connection );

Connection. Open ();

Cmd. ExecuteNonQuery ();

[1] connecting to the Access Database

OleDbConnection conn = new OleDbConnection ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + Application. StartupPath + "\ mydata. mdb ");

[2] connecting to the SQL Server2000 Database

SqlConnection conn = new SqlConnection ("server =.; uid = admin; pwd = 123456; database = mydata ");

[3] trusted SQL Server2000 Database

SqlConnection conn = new SqlConnection ("workstation id = localhost; Integrated Security = SSPI; database = mydata ");

[4] connecting SQL Server/Access Database in ASP(Webpage)

(SQL Server ):

Conn. connectionstring = "driver = {SQL server}; server = (local); uid = admin; pwd = 123456; database = mydata"

(Access ):

Conn. connectionstring = "Driver = {Microsoft Access Driver (*. mdb)}; DBQ =" & Server. Mappath ("mydata. mdb ")

!!! Note: → In asp, ";" is not required when the statement ends ";"

Certificate --------------------------------------------------------------------------------------------------------------------------------------------

1. Create a. mdb database

// Create. mdb. Note that the path parameter is. the complete path of mdb (excluding the table name) // ADO of C. NET cannot create a new ACCESS (MDB) database through programming, so you can only use the ADOX operation of the COM link library to operate public static bool CreateMdbDatabase (string path) {try {ADOX. catalogClass cat = new ADOX. catalogClass (); cat. create ("Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ path +"; "); cat = null; return true;} catch {return false ;}}


II, C # Write records Access Database

You can insert only one record at a time for SQL database insert operations."Loop"Implementation, low efficiency;

Or, it's either faster.Start transaction(Ensure data consistency) → insert cyclically → import the last data together.

[Instance] Create a textbox and a button on the Form, and click the button to write the data in the textbox to the access database.

1. Create an empty Access Database datainput. mdb and write the following fields:


2. Shut down the database when reading and writing the database, and then run the program for write operations.

Using System. data. oledb; private void button#click (object sender, EventArgs e) {foreach (IACell cell in m_DistributedCells) {// set cell. frequencyBand, cell. ulFrequency, cell. all DlFrequency data is written to the database table. in mdb, // string SQL _0 = "insert into table (column1, column2) values ('" + textbox. text + "','" + textbox. text + "')"; // construct an SQL statement string SQL = ""; if (cell. channelIndex = 1) {SQL = "insert into datawrite (ChannelIndex, BandWidth, StartChIndex, EndChIndex, ExcludedChannels, UlFrequency, DlFrequency, ACIR) values ('"+ 1 +"', '"+ 20 + "', '"+ 0 +"', '"+ 1 +"', '"+ 1 +"', '"+ 2402 +"', '"+ 2422 + "', '"+ 0 +"') ";} else if (cell. channelIndex = 6) {SQL = "insert into datawrite (ChannelIndex, BandWidth, StartChIndex, EndChIndex, ExcludedChannels, UlFrequency, DlFrequency, ACIR) values ('" + 6 + "', '"+ 20 + "', '"+ 5 +"', '"+ 6 +"', '"+ 6 +"', '"+ 2427 + "', '"+ 2447 +"', '"+ 0 +"') ";} else if (cell. channelIndex = 11) {SQL = "insert into datawrite (ChannelIndex, BandWidth, StartChIndex, EndChIndex, ExcludedChannels, UlFrequency, DlFrequency, ACIR) values ('" + 11 + "', '"+ 20 +"', '"+ 10 +"', '"+ 11 +"', '"+ 11 +"', '" + 2452 + "', '"+ 2472 +"', '"+ 0 +"') ";} // access Database path. Note that the path must be a double slash \. Otherwise, it will be treated as a forward slash. Yarn! String dbpath = "D: \ data \ datainput. mdb "; // define the database connection object OleDbConnection con = new OleDbConnection (" Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ dbpath); OleDbCommand cmd = new OleDbCommand (SQL, con); // defines the Command object con. open (); // Open the database connection cmd. executeNonQuery (); // Execute Command con. close (); // Close the database connection }}
Run the program, click the button (the breakpoint only loops once), and open the database table again. At this time, one record has been inserted in the table.


3. Read the Database Value -- C # display the table datagridview on the page

[Instance] displays data in an existing Access database table as a datagridview in the C # form.

1. Existing databases with 50 records.

2. Code

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. text; using System. windows. forms; using System. data. oleDb; // This is actually included in the System. in the Data class, it is particularly proposed to indicate the namespace text01 {public partial class Form1: Form {public Form1 () {InitializeComponent (); oleDbConnection thisConnection = new OleDbConnection (@ "provider = Microsoft. jet. OLEDB.4.0; Data Source = D: \ dataread. mdb "); string SQL =" select * from dataread "; OleDbDataAdapter thisAdapter = new OleDbDataAdapter (SQL, thisConnection); System. data. dataSet thisDataSet = new System. data. dataSet (); thisAdapter. fill (thisDataSet, "table"); DataTable dt = thisDataSet. tables ["table"]; this. dataGridView1.DataSource = dt; thisConnection. close ();}}}

:

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.