0% found this document useful (0 votes)
15 views2 pages

DBConn

Uploaded by

missyveloria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

DBConn

Uploaded by

missyveloria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using MySql.Data.

MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataBaseApp
{
internal class DBConn
{
MySqlConnection myConn = new MySqlConnection("server=127.0.0.1;" +
"uid=it313;" +
"pwd=IT313.Student;" +
"database=it313_db;");
MySqlCommand myCmd;
MySqlDataReader myReader;

public DataTable ViewRecords(string fields, string tblsource, string


other_options)
{
myConn.Open();
myCmd = new MySqlCommand("SELECT " + fields + " FROM " + tblsource + "
" + other_options + ";", myConn);
myReader = myCmd.ExecuteReader();
DataTable dTbl = new DataTable();

dTbl.Load(myReader);
myReader.Close();
myConn.Close();

return dTbl;
}

public int AddRecord(string tblsource, string fields, string fieldvalues)


{
int result;
myConn.Open();
myCmd = new MySqlCommand("INSERT INTO " + tblsource + "(" + fields + ")
VALUES(" + fieldvalues + ");", myConn);
result = myCmd.ExecuteNonQuery();
myConn.Close();

return result;
}

public int UpdateRecord(string tblsource, string fieldvalues, string


filter)
{
int result;
myConn.Open();
myCmd = new MySqlCommand("UPDATE " + tblsource + " SET " + fieldvalues
+ " WHERE " + filter + ";", myConn);
result = myCmd.ExecuteNonQuery();
myConn.Close();

return result;
}

public int DeleteRecord(string tblsource, string filter)


{
int result;
myConn.Open();
myCmd = new MySqlCommand("DELETE FROM " + tblsource + " WHERE " +
filter + ";", myConn);
result = myCmd.ExecuteNonQuery();
myConn.Close();

return result;
}

public Boolean RecExist(string tblsource, string filter)


{
Boolean result;
myConn.Open();
myCmd = new MySqlCommand("SELECT * FROM " + tblsource + " WHERE " +
filter + ";", myConn);
myReader = myCmd.ExecuteReader();
result = myReader.HasRows;
myReader.Close();
myConn.Close();

return result;
}
}
}

You might also like