67. Ma'lumotlar oqimi yordamida PostgreSQL ma'lumotlar bazasiga ma'lumotlarni yozadigan C# dasturini amalga oshiring.
Javob:
using Npgsql;
using System;
namespace PostgreSQLExample
{
class Program
{
static void Main(string[] args)
{
NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;Database=mydatabase;User Id=myusername;Password=mypassword;");
conn.Open();
string sql = "INSERT INTO mytable (column1, column2) VALUES (@value1, @value2)";
NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
cmd.Parameters.AddWithValue("value1", "John");
cmd.Parameters.AddWithValue("value2", 25);
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
68. Ma'lumotlar oqimi yordamida matnli faylga ma'lumotlarni o'qiydigan va yozadigan C# dasturini yozing.
Javob:
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = "C:\Users\User\Documents\file.txt"; // matnli faylning joylashuvi
string[] lines = { "Satr 1", "Satr 2", "Satr 3" }; // yozish uchun matnlar
try
{
File.WriteAllLines(filePath, lines); // matnlarni faylga yozish
Console.WriteLine("Ma'lumotlar fayliga muvaffaqiyatli yozildi.");
}
catch (Exception ex)
{
Console.WriteLine("Xatolik yuz berdi: " + ex.Message); // xatolik haqida xabar berish
}
Console.ReadLine(); // dasturni tugatish uchun konsolni kutish
}
69. Oddiy dastur o'rniga siqilgan ma'lumotlar oqimidan foydalanish uchun mavjud C# dasturini o'zgartiring.
Javob:
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = "C:\Users\User\Documents\file.txt"; // matnli faylning joylashuvi
try
{
string[] lines = File.ReadAllLines(filePath); // fayldagi matnlarni massivga yuklash
foreach (string line in lines) // har bir qator uchun
{
Console.WriteLine(line); // matnni konsolga chiqarish
}
}
catch (Exception ex)
{
Console.WriteLine("Xatolik yuz berdi: " + ex.Message); // xatolik haqida xabar berish
}
Console.ReadLine(); // dasturni tugatish uchun konsolni kutish
}
}
Do'stlaringiz bilan baham: |