Harry Potter and the Sorcerer's Stone


Ma'lumotlar oqimi yordamida PostgreSQL ma'lumotlar bazasiga ma'lumotlarni yozadigan C# dasturini amalga oshiring


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

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


}
}

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   29   30   31   32   33   34   35   36   ...   62




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