Call MysqlDB in C #

Source: Internet
Author: User

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.

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.