// 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;
}
}
}
Do'stlaringiz bilan baham: |