Arkadaşlar bitirme projesi kapsamında c# yazmaya devam ediyorum Aşagıdaki kod parçası c# ile Mysql i bağlayıp temel fonksiyonlarını yerine getirmektedir

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace FingerPrint
{
public class KullaniciDatalar
{
public KullaniciDatalar(DBase database)
{
Initialize(database);
}
public KullaniciDatalar(DBase Database){
this.data_connect=Database;
}
DBase data_connect;
MySqlConnection connection;
private void Initialize(DBase database)
{
database.host = “localhost”;
database.dbName = “ParmakIzı”;
database.username = “admin”;
database.password = “Ramazan.71”;
string connectionString;
connectionString = “SERVER=” + database.host + “;” + “DATABASE=” +
database.dbName + “;” + “UID=” + database.username+ “;” + “PASSWORD=” + database.password+ “;”;
database.connection = new MySqlConnection(connectionString);
connection = database.connection;
}
//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:
throw new Exception(“Cannot connect to server.  Contact administrator”);

break;

case 1045:
throw new Exception(“Invalid username/password, please try again”);
break;
}
return false;
}
}

//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
throw new Exception(ex.Message);
return false;
}
}
public void Insert()
{
string query = “INSERT INTO tableinfo (name, age) VALUES(‘John Smith’, ’33’)”;

//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);

//Execute command
cmd.ExecuteNonQuery();

//close connection
this.CloseConnection();
}
}

//Update statement
public void Update()
{
string query = “UPDATE tableinfo SET name=’Joe’, age=’22’ WHERE name=’John Smith'”;

//Open connection
if (this.OpenConnection() == true)
{
//create mysql command
MySqlCommand cmd = new MySqlCommand();
//Assign the query using CommandText
cmd.CommandText = query;
//Assign the connection using Connection
cmd.Connection = connection;

//Execute query
cmd.ExecuteNonQuery();

//close connection
this.CloseConnection();
}
}

//Delete statement
public void Delete(string tablename,int id, string name)
{
string query;
if (name != null)
{
query = “DELETE FROM tableinfo WHERE name==”+name;
}
else
{
query=”delete from “+ tablename+”where id==”+id.ToString();
}
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.ExecuteNonQuery();
this.CloseConnection();
}
}
//Select statement
public List<string>[] Select()
{
string query = “SELECT * FROM tableinfo”;

//Create a list to store the result
List<string>[] list = new List<string>[3];
list[0] = new List<string>();
list[1] = new List<string>();
list[2] = new List<string>();

//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())
{
list[0].Add(dataReader[“id”] + “”);
list[1].Add(dataReader[“name”] + “”);
list[2].Add(dataReader[“age”] + “”);
}

//close Data Reader
dataReader.Close();

//close Connection
this.CloseConnection();

//return list to be displayed
return list;
}
else
{
return list;
}
}

}
}

iyi çalışmalar dilerim

Ramazan CESUR