Mysql
This commit is contained in:
@@ -1,6 +1,116 @@
|
||||
namespace Nevyn.Classes
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
namespace Nevyn.Classes
|
||||
{
|
||||
public class Mysql
|
||||
{
|
||||
private MySqlConnection connection;
|
||||
private string server;
|
||||
private string database;
|
||||
private string uid;
|
||||
private string password;
|
||||
|
||||
//Constructor
|
||||
public Mysql()
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
//Initialize values
|
||||
private void Initialize()
|
||||
{
|
||||
server = "192.168.1.108";
|
||||
database = "nevyn";
|
||||
uid = "nevyn";
|
||||
password = "3edc4RFV";
|
||||
string connectionString;
|
||||
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
|
||||
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
|
||||
|
||||
connection = new MySqlConnection(connectionString);
|
||||
}
|
||||
//open connection to database
|
||||
private bool OpenConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
return true;
|
||||
}
|
||||
catch (MySqlException ex)
|
||||
{
|
||||
//When handling errors, you can your application's response based
|
||||
//on the error number.
|
||||
//The two most common error numbers when connecting are as follows:
|
||||
//0: Cannot connect to server.
|
||||
//1045: Invalid user name and/or password.
|
||||
switch (ex.Number)
|
||||
{
|
||||
case 0:
|
||||
Console.Write("Cannot connect to server. Contact administrator");
|
||||
break;
|
||||
|
||||
case 1045:
|
||||
Console.Write("Invalid username/password, please try again");
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Close connection
|
||||
private bool CloseConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Close();
|
||||
return true;
|
||||
}
|
||||
catch (MySqlException ex)
|
||||
{
|
||||
Console.Write(ex.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Models.Wallet SelectWallet()
|
||||
{
|
||||
string query = "SELECT * FROM tblWallet";
|
||||
|
||||
//Create a list to store the result
|
||||
Models.Wallet wallet = new Models.Wallet();
|
||||
|
||||
//Open connection
|
||||
if (this.OpenConnection() == true)
|
||||
{
|
||||
//Create Command
|
||||
MySqlCommand cmd = new MySqlCommand(query, connection);
|
||||
//Create a data reader and Execute the command
|
||||
MySqlDataReader dataReader = cmd.ExecuteReader();
|
||||
|
||||
//Read the data and store them in the list
|
||||
while (dataReader.Read())
|
||||
{
|
||||
wallet.Id = (int)dataReader["idtblWallet"];
|
||||
wallet.Kort = (int)dataReader["Kort"];
|
||||
wallet.Buffert = (int)dataReader["Buffert"];
|
||||
wallet.Spara = (int)dataReader["Spara"];
|
||||
|
||||
}
|
||||
|
||||
//close Data Reader
|
||||
dataReader.Close();
|
||||
|
||||
//close Connection
|
||||
this.CloseConnection();
|
||||
|
||||
//return list to be displayed
|
||||
return wallet;
|
||||
}
|
||||
else
|
||||
{
|
||||
return wallet;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user