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