A simple C # 's Access action class

Source: Internet
Author: User
Tags one table

Have been busy these days with c#+access write a WinForm application, use to query from the Access database, and return the data to the DataGridView.

Since there is no experience with. NET development, it takes a bit of effort to connect to an Access database, and finally find that to bind the data to the DataGridView, you just have to set the DataGridView datasource as a DataTable read from the database, to find this idea and write a Access's action class, it's easy to use, and I added a method to return a container dataset to a DataTable in this class, to support new or added to an existing dataset, to avoid overwriting a table of the same name in the original dataset, and the operation returned to the original dataset. There is an overload. There is also a method for performing database operations that are not returned, which can generally be used for deletion.

All operations are directly in the SQL language. If it is another database such as Oracle, the operation is very similar, and the difference is mainly on the default connection string of the constructor. SQL Server may have some changes, because the first using namespaces are different, but the operations are similar because the parent classes such as Connection,adapter are all the same and can eventually be used to communicate with datasets or DataTable.

PS Bingdingsource is also a very useful class in the database operation process, this article does not say much.

Suppose there is an Access database path of "C:/db.mdb", which has a table named student, and there is already a Table control dataGridView1, the use example of this class is as follows: ...
Using Accessdb;
...
Initialize, load database path
Accessdbclass mydb = new Accessdbclass ("C:/db.mdb");

Returns a DataTable that conforms to the SQL requirements and binds to the control dataGridView1
DataTable dt = new DataTable ();
DT = MyDB. Selecttodatatable (@ "SELECT * from student");
this. datagridview1.datasource = DT;

Returns a DataSet that includes a DataTable that conforms to the SQL requirements and given name and binds to the control dataGridView1
DataSet ds = new DataSet ();
ds = MyDB. Selecttodataset (@ "SELECT * FROM Student", "student");
this. Datagridview1.datasource = ds. tables["Student"];

Close Database
MyDB. Close ();

Because the usage is similar, no more examples are given, and it is really very simple to use.

If you have more than one table, it is recommended to read a dataset for ease of use and management.

Needless to say, the following is the code of the Operation class

Invs2005. NET 2.0 using System;
Using System.Data;
Using System.Data.OleDb;

Namespace Accessdb
... {
/**////<summary>
For a summary of ACCESSDB, please keep the following information intact
Call the Close () method after the data has been passed, closing the data link.
</summary>
public class Accessdbclass
... {

Variable declaration at #region Variable declaration
Public OleDbConnection Conn;
public string connstring;//Connection string
#endregion


Constructors and connections shutdown database #region Constructors and connections close database
/**////<summary>
Constructors
</summary>
<param name= "dbpath" >access database path </param>
Public Accessdbclass (String dbpath)
... {
connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=";
ConnString + = DBPath;
Conn = new OleDbConnection (connstring);
Conn.Open ();
}

/**////<summary>
Open a Data source link
</summary>
<returns></returns>
Public OleDbConnection Dbconn ()
... {
Conn.Open ();
return Conn;
}

/**////<summary>
Call the function after the data is passed and close the data link.
</summary>
public void Close ()
... {
Conn.close ();
}
#endregion


Database basic Operation #region Database basic operation

Related Article

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.