Professional Documents
Culture Documents
Lekcija9 - CsharpMySQL Lab 9
Lekcija9 - CsharpMySQL Lab 9
ADO.NET е важен дел од .NET околината. Тоа е спецификација која го опишува пристапот
до релационите бази, XML фајловите и други податоци за апликациите. MySQL
Connector/Net ја имплементира ADO.NET спецификацијата за MySQL базата. Тоа е
драјвер напишан во C# кој е достапен за сите .NET јазици.
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";
1
Во блокот try се пишува кодот кој се надгледува и во кој може да се јави исклучок. Во
блокот catch се обработува генерираниот исклучок. Блокот finaly се извршува без разлика
дали се јавил или не се јавил исклучок. Обично во овој блок се затвара конекцијата со
базата
conn.Close();
using System;
using MySql.Data.MySqlClient;
try
{
conn = new MySqlConnection(cs);
conn.Open();
Console.WriteLine("MySQL version : {0}", conn.ServerVersion);
} finally
{
if (conn != null)
{
conn.Close();
}
}
}
}
2
using System;
using MySql.Data.MySqlClient;
try
{
conn = new MySqlConnection(cs);
conn.Open();
} finally
{
if (conn != null)
{
conn.Close();
}
}
}
}
3
//Однапред подготвени SQL изрази
using System;
using MySql.Data.MySqlClient;
try
{
conn = new MySqlConnection(cs);
conn.Open();
} finally
{
if (conn != null) {
conn.Close();
}
}
}
}
4
//Користење на класа MySqlDataReader
using System;
using MySql.Data.MySqlClient;
try
{
conn = new MySqlConnection(cs);
conn.Open();
while (rdr.Read())
{
Console.WriteLine(rdr.GetInt32(0) + ": "
+ rdr.GetString(1));
}
} finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}
}
}
5
using System;
using MySql.Data.MySqlClient;
try
{
conn = new MySqlConnection(cs);
conn.Open();
while (rdr.Read())
{
Console.WriteLine(rdr.GetString(0).PadRight(18) +
rdr.GetString(1));
}
} finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}
}
}
6
//Primer so DataSet
using System;
using System.Data;
using MySql.Data.MySqlClient;
try
{
da.Fill(ds, "Authors");
DataTable dt = ds.Tables["Authors"];
dt.WriteXml("authors.xml");
Console.WriteLine("".PadLeft(20, '='));
}
} finally
{
if (conn != null)
{
conn.Close();
}
}
}
}
7
//Пример со DataGrid
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using MySql.Data.MySqlClient;
public MForm()
{
this.Text = "DataGrid";
this.Size = new Size(350, 300);
this.InitUI();
this.InitData();
this.CenterToScreen();
}
void InitUI()
{
dg = new DataGrid();
dg.CaptionBackColor = System.Drawing.Color.White;
dg.CaptionForeColor = System.Drawing.Color.Black;
dg.CaptionText = "Authors";
void InitData()
{
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";
try
{
8
conn = new MySqlConnection(cs);
conn.Open();
ds = new DataSet();
da = new MySqlDataAdapter(stm, conn);
da.Fill(ds, "Authors");
dg.DataSource = ds.Tables["Authors"];
} finally
{
if (conn != null)
{
conn.Close();
}
}
}
}
class MApplication
{
public static void Main()
{
Application.Run(new MForm());
}
}
9
Programa22
using System;
using MySql.Data.MySqlClient;
try
{
conn = new MySqlConnection(cs);
conn.Open();
}
catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
}
10
Programa 222
using System;
using System.Data;
using MySql.Data.MySqlClient;
try
{
conn = new MySqlConnection(cs);
conn.Open();
da.Fill(ds, "Authors");
DataTable dt = ds.Tables["Authors"];
dt.WriteXml("authors.xml");
11
conn.Close();
}
}
}
}
Programa 2222
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using MySql.Data.MySqlClient;
public MForm()
{
this.Text = "DataGrid";
this.Size = new Size(350, 300);
this.InitUI();
this.InitData();
this.CenterToScreen();
}
void InitUI()
{
dg = new DataGrid();
dg.CaptionBackColor = System.Drawing.Color.White;
dg.CaptionForeColor = System.Drawing.Color.Black;
dg.CaptionText = "Authors";
void InitData()
{
string cs = @"server=localhost;userid=user12;
12
password=user12;database=mydb";
try
{
conn = new MySqlConnection(cs);
conn.Open();
ds = new DataSet();
da = new MySqlDataAdapter(stm, conn);
da.Fill(ds, "Authors");
dg.DataSource = ds.Tables["Authors"];
}
catch (MySqlException ex)
{
Console.WriteLine("Error: " + ex.ToString());
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
}
class MApplication
{
public static void Main()
{
Application.Run(new MForm());
}
}
13
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
namespace CSharpMySql
{
class Program
{
static string getString(string msg)
{
Console.Write(msg);
return Console.ReadLine();
}
static void Main(string[] args)
{
String str = @"server=localhost;database=nova;userid=root;
password=root;";
MySqlConnection con = null;
MySqlDataReader reader = null;
try
{
con = new MySqlConnection(str);
con.Open(); //otvaranje na konekcija
int br1 = 0;
String ime1 = string.Empty;
String prez1 = string.Empty;
14
Console.WriteLine("id|ime|prez");
while (reader.Read())
{
Console.WriteLine(reader.GetString(0) + "|" +
reader.GetString(1) + "|" + reader.GetString(2));
}
reader.Close();
//primer za UPDATE
String ime2 = "neznam";
cmd = new MySqlCommand("UPDATE podatoci SET ime='" + ime2 +
"' WHERE ime='" + ime1 + "'", con);
cmd.ExecuteNonQuery();
Console.WriteLine("id|ime|prez");
while (reader.Read())
{
Console.WriteLine(reader.GetString(0) + "|" +
reader.GetString(1) + "|" + reader.GetString(2));
}
reader.Close();
//primer za DELETE
cmd = new MySqlCommand("DELETE FROM podatoci WHERE id ='"+
br1+"'", con);
cmd.ExecuteNonQuery();
Console.WriteLine("id|ime|prez");
while (reader.Read())
{
Console.WriteLine(reader.GetString(0) + "|" +
reader.GetString(1) + "|" + reader.GetString(2));
}
}
catch (MySqlException err)
{
Console.WriteLine("Greska: " + err.ToString());
}
Finally {
if (con != null) {
con.Close(); //zatvaranje na konekcija
}
if (reader != null){
reader.Close();
}
}
15
Console.WriteLine("Pritisnete bilo koe kopce za izlez.");
Console.ReadLine();
}
}
}
16