c#
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Diagnostics;
namespace MauiInvoiceApplication.Classes
{
class DBConnect : IDisposable
{
MySqlConnection conn;
string myConnectionString;
static string host = "phpmyadmin.adonkers.com";
static string database = "InvoiceApp";
static string userDB = "";
static string password = "";
static string ssltype = "None";
static string dbport = "3306";
static string dbpubkeyre = "true";
static string Multipleact = "true";
static string strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password + ";SslMode=" + ssltype + ";Port=" + dbport + "AllowPublicKeyRetrieval=" + dbpubkeyre + "MultipleActiveResultSets=" + Multipleact;
public bool Open()
{
strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password +
";SslMode=" + ssltype + ";Port=" + dbport + ";AllowPublicKeyRetrieval=" + dbpubkeyre;
conn = new MySqlConnection(strProvider);
conn.Open();
return true;
}
public void Close()
{
conn.Close();
conn.Dispose();
}
public DataSet ExecuteDataSet(string sql)
{
try
{
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
da.Fill(ds, "result");
return ds;
}
catch (Exception ex)
{
Debug.WriteLine("Error DBConnect" + ex.Message);
}
return null;
}
public MySqlDataReader ExecuteReader(string sql)
{
try
{
MySqlDataReader reader;
MySqlCommand cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();
return reader;
}
catch (Exception ex)
{
Debug.WriteLine("Error DBConnect" + ex.Message);
}
return null;
}
public int ExecuteNonQuery(string sql)
{
try
{
int affected;
MySqlTransaction mytransaction = conn.BeginTransaction();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
affected = cmd.ExecuteNonQuery();
mytransaction.Commit();
return affected;
}
catch (Exception ex)
{
Debug.WriteLine("Error DBConnect" + ex.Message);
}
return -1;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (conn != null)
{
conn.Dispose();
conn = null;
}
}
}
}
}