Published June 26, 2007 | https://doi.org/10.59348/gepnb-vc464

C# DataExecutor class available

Creators & Contributors

One of the questions I see most frequently on Freenode's ##csharp irc channel is how to use a MySql Database in .NET. I've therefore provided the class that I use for basic database operations. You can find it at http://www.martineve.com/2007/06/25/c-mysql-dataexecutor-class/.

The class supports both strongly and weakly typed datasets and usage is as follows:

  1. Get the MySql Connector.NET
  2. Reference it in your project
  3. Use the following for strongly typed datasets:
  4. </ul>
    MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("SELECT * FROM table WHERE blah=@blah");
    cmd.Parameters.Add("@blah", "test");
    
    Tools.DataExecutor de = null;
    
    try{
    
    	de = new Tools.DataExecutor(cmd, false);
    
    	DataSchemas.app.tableDataTable test = new DataSchemas.app.tableDataTable();
    	de.DataSetSchema(test);
    
    	DataSchemas.app.tableRow row = null;
    
    	if (test.Rows.Count &gt; 0)
    	{
    		row = (DataSchemas.app.tableRow)test.Rows[0];
    	}
    
    } finally {
    	de.Close();
    }

    and this for weakly typed datasets:

    MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("SELECT * FROM table WHERE blah=@blah");
    cmd.Parameters.Add("@blah", "test");
    
    Tools.DataExecutor de = new Tools.DataExecutor(cmd, false);
    
    DataSet ds = new DataSet();
    
    de.Adapter.Fill(ds);

    Easy eh? :)

    The parameterization of the queries will protect you against SQL Injection exploits so I'd recommend you do that at all times. Your connection string should go in your Web.Config file and then be specified inside the DataExecutor class. It's worth pointing out that if you don't set oldsyntax=true; then you must ensure that you use ? for your parameter names as opposed to @.

C# DataExecutor class available was originally published by Martin Paul Eve at eve.gd: Martin Paul Eve on June 26, 2007.

Files

10.59348_gepnb-vc464.pdf

Files (120.2 kB)

Name Size Download all
md5:d9d5dd8a4d944983024c3d00810ea4d3
120.2 kB Preview Download

Additional details

Description

One of the questions I see most frequently on Freenode's ##csharp irc channel is how to use a MySql Database in .NET. I've therefore provided the class that I use for basic database operations.

Identifiers

GUID
https://doi.org/10.59348/gepnb-vc464
URL
https://eve.gd/2007/06/26/c-dataexecutor-class-available/

Dates

Issued
2007-06-26T00:00:00Z
Updated
2007-06-26T14:38:11Z