Recently, I was working on a project and found that the class provided by Mysql to C # operations is not very easy to use, so I tried to write a convenient Mysql Data Layer class.
For example, a query was previously executed.
Code public DataSet Getdataset (long pkid ){
String SQL = "select * from mytable where pkid = @ pkid ";
MySqlCommand _ dbcomm = _ dbconn. CreateCommand ();
MySqlParameterCollection mysqlparacoll = _ dbcomm. Parameters;
Mysqlparacoll. Add (new MySqlParameter (n, DbType. Object)
{
Value = 123
});
DbDataAdapter dbad = new MySqlDataAdapter (_ dbcomm );
DataSet ds = new DataSet ();
Dbad. Fill (ds );
Return ds;
}
So much code is required to execute a simple query statement with parameters.
Simple Way to show the bottom
Code public DataSet getDataSet (long pkid)
{
String SQL = "select * from mytable where pkid = @ pkid ";
Using (MyDbHelper db = new MyDbHelper ()){
DataSet ds = db. getDataSet (SQL, pkid );
Return ds;
}
}
// GetDataSet Method
/*
Public System. Data. DataSet getDataSet (string SQL, params object [] paramlist)
{
MySqlCommand dbcomm = CreateDBComm (SQL, paramlist );
DbDataAdapter dbad = new MySqlDataAdapter (dbcomm );
DataSet ds = new DataSet ();
Dbad. Fill (ds );
Return ds;
}
*/
In the CreateDBComm (SQL, paramlist); the method is not displayed,
Here the Regular Expression and reflection are used, and the MySqlDbType parameter is automatically created.