using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace LabTaskFileHandling
{
internal class Program
{
static void Main(string[] args)
{
WriteData();
ReadAndWriteToDatabase();
}
public static void WriteData()
{
FileStream fs = new FileStream("C:\\Users\\ARFA TECH\\Desktop\\
[Link]", [Link], [Link]);
StreamWriter sw = new StreamWriter(fs);
[Link]("Please enter sentences to write on the file (comma-
separated format: ID,Name,Age):");
string str = [Link]();
[Link](str);
[Link]();
[Link]();
[Link]();
[Link]("Data written successfully!");
}
public static void ReadAndWriteToDatabase()
{
string connectionString = "Integrated Security=SSPI;Persist Security
Info=False;Initial Catalog=BiiT;Data Source=DESKTOP-N57UVTA"; ; // Replace with
your database connection string
string filePath = "C:\\Users\\ARFA TECH\\Desktop\\[Link]";
try
{
using (FileStream fs = new FileStream(filePath, [Link],
[Link]))
using (StreamReader sr = new StreamReader(fs))
{
string line;
while ((line = [Link]()) != null)
{
// Split the line by comma
string[] data = [Link](',');
if ([Link] == 3)
{
int id = [Link](data[0]);
string name = data[1];
int age = [Link](data[2]);
// Write to database
using (SqlConnection connection = new
SqlConnection(connectionString))
{
[Link]();
string query = "INSERT INTO YourTableName (ID,
Name, Age) VALUES (@ID, @Name, @Age)";
using (SqlCommand command = new SqlCommand(query,
connection))
{
[Link]("@ID", id);
[Link]("@Name", name);
[Link]("@Age", age);
[Link]();
}
}
[Link]($"Data written to database: ID={id},
Name={name}, Age={age}");
}
else
{
[Link]("Invalid data format. Skipping
line.");
}
}
}
}
catch (Exception ex)
{
[Link]($"An error occurred: {[Link]}");
}
[Link]();
}
}
}