14- ma’ruza. Ma’lumotlar bazasiga murojaatni tashkil etishda odbc va ob’yеktga yo‘naltirilgan dasturlar foydalanish Reja


DML bilan ishlash (qo‘shish, yangilash, tanlash, o‘chirish)


Download 114.78 Kb.
bet3/3
Sana04.01.2023
Hajmi114.78 Kb.
#1078473
1   2   3
Bog'liq
14- ma\'ruza

DML bilan ishlash (qo‘shish, yangilash, tanlash, o‘chirish).
Odatda, kiritish, yangilash va o‘chirish ma'lumotlar bazasida ma'lumotlarni yozish yoki o‘zgartirish uchun ishlatiladi, Select esa ma'lumotlarni o‘qish uchun ishlatiladi.
Shu sababli, biz ushbu so‘rovlarni bajarish uchun har xil usullarga egamiz. Usullari quyidagilar:
ExecuteNonQuery : misol uchun, har qanday ma'lumotlarni qaytarib bo‘lmaydi buyruqni bajarishi uchun ishlatiladi, Insert, Update yoki Delete.
ExecuteReader : 0 yoki undan ortiq yozuvlarni qaytaradigan buyruqni bajarish uchun foydalaniladi, masalan Select .
ExecuteScalar : Faqat 1 qiymatni qaytaradigan buyruqni bajarish uchun foydalaniladi, masalan, Hisoblashni tanlang ( *).
public void Insert()
{
string query = "INSERT INTO tableinfo (name, age) VALUES('John Smith', '33')";
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection); cmd.ExecuteNonQuery();
this.CloseConnection();
}
}
public void Update()
{
string query = "UPDATE tableinfo SET name='Joe', age='22' WHERE name='John Smith'";
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 query = "DELETE FROM tableinfo WHERE name='John Smith'";
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection); cmd.ExecuteNonQuery();
this.CloseConnection();
}
}
MySQLni C ++ ga ulash.
Endi MySQLni C ++ ga ulashni ko‘rib chiqamiz.
SQL (Structured Query Language) - bu to‘rtinchi avlod tili (4GL) bo‘lib, u MBBTni aniqlash, boshqarish va boshqarish uchun ishlatiladi.
C: \ SQLAPI \ lib \ libsqlapiddll.a
C: \ Program Files \ CodeBlocks \ MinGW \ lib \ libuser32.a
C: \ Program Files \ CodeBlocks \ MinGW \ lib \ libversion.a
C: \ Program Files \ CodeBlocks \ MinGW \ lib \ liboleaut32.a
C: \ Program Files \ CodeBlocks \ MinGW \ lib \ libole32.a
Yuqoridagi kod C/C ++ dasturini SQLAPI bilan ulash uchun kutubxona fayllarini qo‘shish uchun ishlatiladi.
Asosan, ikkita bosqich mavjud:
1. Ma'lumotlar bazasiga ulanish (va xatolar bilan ishlov berish) // Ma'lumotlar bazasiga ulanish uchun C ++ pgroram (va xatolar bilan ishlash) #include
#include // main SQLAPI++ header
int main(int argc, char* argv[])
{
// create connection object to connect to database
SAConnection con; try
{
con.Connect ("test", // database name
"tester", // user name
"tester", // password
SA_Oracle_Client); //Oracle Client
printf("Ulandi!\n"); con.Disconnect();
printf("Uzildi!\n");
}
catch(SAException & x)
{ try
{
con.Rollback ();
}
catch(SAException &)
{
}
printf("%s\n", (const char*)x.ErrText());
}
return 0;
}
SQL oddiy buyrug‘ini bajarish.
Endi biz sodda SQL so‘rovini bajaramiz. Dastlab ma'lumotlar bazasi uchun jadval yaratamiz:
create table tb1(id number, name varchar(20);
Endi, so‘rovni ma'lumotlar bazasiga yuborish uchun cmd.setCommandText usulidan foydalanish kerak, u quyidagicha ko‘rsatiladi:
con.Connect("test", "tester", "tester", SA_Oracle_Client); cmd.setCommandText("create table tb1(id number, name varchar(20));”); and now, to execute the query we have to use the following command:
cmd.Execute(); Full Code:
#include
#include // main SQLAPI++ header
int main(int argc, char* argv[])
{
SAConnection con; // connection object to connect to database
SACommandcmd; // create command object try {
// connect to database (Oracle in our example)
con.Connect("test", "tester", "tester", SA_Oracle_Client);
// associate a command with connection
// connection can also be specified in SACommand constructor cmd.setConnection(&con);
// create table
cmd.setCommandText("create table tbl(id number, name varchar(20));"); cmd.Execute(); // insert value
cmd.setCommandText("Insert into tbl(id, name) values (1,”Vinay”)"); cmd.setCommandText("Insert into tbl(id, name) values (2,”Kushal”)"); cmd.setCommandText("Insert into tbl(id, name) values
(3,”Saransh”)"); cmd.Execute();
// commit changes on success con.Commit();
printf("Table created, row inserted!\n");
}
catch(SAException &x)
{
try
{
// on error rollback changes con.Rollback();
}
catch(SAException &)
{
}
printf("%s\n", (const char*)x.ErrText());
}
return 0;
}
con.Commit ( );
Yuqorida biz ma’lumotlar bazasiga murojaatni tashkil etishda C++,C# dasturlariga bog‘lash va ularni kod jixatdan taxlilini keltirib o‘tdik.

Nazorat savollari


  1. Ma’lumotlar bazalarining ichki himoya vazifasini keltiring?

  2. SQL Server himoya vositasiga deganda nimani tushunasiz?

  3. GRANT privilege nima vazifani bajaradi? Misol keltiring.

  4. GRANT UPDATE uchun misollar yozing?

GRANT REFERENCES uchun misol keltiring?
Download 114.78 Kb.

Do'stlaringiz bilan baham:
1   2   3




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling