Harry Potter and the Sorcerer's Stone


// Ma'lumotlar bazasiga ulanamiz


Download 192.49 Kb.
bet31/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   27   28   29   30   31   32   33   34   ...   62
Bog'liq
Algaritim amaliy-2

// Ma'lumotlar bazasiga ulanamiz
sqlite_conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

return sqlite_conn;
}

static void InsertData(SQLiteConnection conn)
{
// Ma'lumotlar bazasiga ma'lumotlarni yozish
SQLiteCommand sqlite_cmd;
sqlite_cmd = conn.CreateCommand();
sqlite_cmd.CommandText = "INSERT INTO Customers (Name, Age) VALUES ('John Doe', 30);";
sqlite_cmd.ExecuteNonQuery();
}

static void ReadData(SQLiteConnection conn)
{
// Ma'lumotlar bazasidan ma'lumotlarni o'qish
SQLiteDataReader sqlite_datareader;
SQLiteCommand sqlite_cmd;
sqlite_cmd = conn.CreateCommand();
sqlite_cmd.CommandText = "SELECT * FROM Customers;";
sqlite_datareader = sqlite_cmd.ExecuteReader();
while (sqlite_datareader.Read())
{
string name = sqlite_datareader.GetString(0);
int age = sqlite_datareader.GetInt32(1);
Console.WriteLine("Name: {0}, Age: {1}", name, age);
}
}
}
}
var filter = Builders.Filter.Eq("name", "John"); // Ma'lumotlarni maydon bo'yicha qidiring
var documents = collection.Find(filter).ToList(); // Ma'lumotlarni olish

foreach (var document in documents)


{
Console.WriteLine(document);
65. Mavjud C# dasturini oddiy dastur o'rniga buferlangan ikkilik ma'lumotlar oqimidan foydalanish uchun o'zgartiring.
Javob:
using System;
using System.Data.SQLite;
namespace SQLiteDemo
{
class Program
{
static void Main(string[] args)
{
// SQLite ma'lumotlar bazasiga ulanish uchun Connection obyektini yaratamiz
SQLiteConnection sqlite_conn;
sqlite_conn = CreateConnection();

// Ma'lumotlarni yaratamiz


byte[] data = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 };

// Ma'lumotlar bazasiga ma'lumotlarni yozish


InsertData(sqlite_conn, data);

// Ma'lumotlar bazasidan ma'lumotlarni o'qish


byte[] result = ReadData(sqlite_conn);

// Ma'lumotlar bazasidan chiqish


sqlite_conn.Close();

// O'qilgan ma'lumotlarni konsolga chiqaramiz


Console.WriteLine(System.Text.Encoding.UTF8.GetString(result));
}

static SQLiteConnection CreateConnection()


{
// Ma'lumotlar bazasiga ulanish uchun Connection obyektini yaratamiz
SQLiteConnection sqlite_conn;
sqlite_conn = new SQLiteConnection("Data Source=sample.db;Version=3;New=False;Compress=True;");
try
{
// Ma'lumotlar bazasiga ulanamiz
sqlite_conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

return sqlite_conn;


}

static void InsertData(SQLiteConnection conn, byte[] data)


{
// Ma'lumotlar bazasiga ma'lumotlarni yozish
SQLiteCommand sqlite_cmd;
sqlite_cmd = conn.CreateCommand();
sqlite_cmd.CommandText = "INSERT INTO Data (Data) VALUES (@data);";
sqlite_cmd.Parameters.AddWithValue("@data", data);
sqlite_cmd.ExecuteNonQuery();
}

static byte[] ReadData(SQLiteConnection conn)


{
// Ma'lumotlar bazasidan ma'lumotlarni o'qish
SQLiteDataReader sqlite_datareader;
SQLiteCommand sqlite_cmd;
sqlite_cmd = conn.CreateCommand();
sqlite_cmd.CommandText = "SELECT * FROM Data;";
sqlite_datareader = sqlite_cmd.ExecuteReader();
sqlite_datareader.Read();
byte[] result = (byte[])sqlite_datareader["Data"];
return result;
}
}
}

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   27   28   29   30   31   32   33   34   ...   62




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