Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Data;
- using MySql.Data.MySqlClient;
- namespace PickYourName
- {
- public class PickYourName
- {
- private readonly string connstring;
- public PickYourName()
- {
- connstring = "Server=ipaddress;Database=dbname;Uid=username;Pwd=password;";
- }
- // This method will return the first datagrid cell of a query, if aimed to have a single result will work like that
- public string ReadString(string query)
- {
- MySqlConnection conn = new MySqlConnection(connstring);
- string result = "";
- try
- {
- MySqlCommand command = conn.CreateCommand();
- MySqlDataReader reader;
- command.CommandText = query;
- conn.Open();
- reader = command.ExecuteReader();
- while (reader.Read())
- {
- result = reader.GetValue(0).ToString();
- }
- return result;
- }
- catch (MySqlException ex)
- {
- throw (ex);
- }
- finally
- {
- conn.Dispose();
- conn = null;
- }
- }
- // this method will return a dataset with the results of the query
- public DataSet ReadGrid(string query, string datatable)
- {
- MySqlConnection conn = new MySqlConnection(connstring);
- try
- {
- MySqlDataAdapter da = new MySqlDataAdapter(query, conn);
- DataSet ds = new DataSet();
- da.Fill(ds, datatable);
- return ds;
- }
- catch (MySqlException ex)
- {
- throw (ex);
- }
- finally
- {
- conn.Dispose();
- conn = null;
- }
- }
- // this method will just run "any" sql command sending it to mysql without any expectation of answer
- public void Run(string query)
- {
- MySqlConnection conn = new MySqlConnection(connstring);
- try
- {
- MySqlCommand cmd = new MySqlCommand(query, conn);
- cmd.Connection.Open();
- cmd.ExecuteNonQuery();
- }
- catch (MySqlException ex)
- {
- throw (ex);
- }
- finally
- {
- conn.Dispose();
- conn = null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment