Code Thuyết Trình
Code Thuyết Trình
using System;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
namespace NetworkUtilities
{
class Program
{
static void Main(string[] args)
{
Bài 45
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
try
{
HttpResponseMessage response = await httpClient.GetAsync(url);
ShowHeaders(response.Headers);
response.EnsureSuccessStatusCode();
Bài 46
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
try
{
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
string htmltext = await response.Content.ReadAsStringAsync();
Console.WriteLine(htmltext);
}
catch (HttpRequestException ex)
{
Console.WriteLine("Request error: " + ex.Message);
}
}
}
Bài 47
using System;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
class SimpleHttpServer
{
private readonly HttpListener listener;
Bài 48
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace NetworkExample
{
class Program
{
public static void ShowUriInfo(string url)
{
Uri uri = new Uri(url);
Console.WriteLine($"URL : {url}");
Console.WriteLine($"Scheme : {uri.Scheme}");
Console.WriteLine($"Host : {uri.Host}");
Console.WriteLine($"Port : {uri.Port}");
Console.WriteLine($"Fragment : {uri.Fragment}");
Console.WriteLine($"Query : {uri.Query}");
Console.WriteLine($"Path : {uri.LocalPath}");
foreach (var seg in uri.Segments)
Console.WriteLine($"Segment : {seg}");
}
string ip = "192.168.1.1";
IPAddressExample(ip);
Bài 49
using System;
using System.Data;
using System.Data.SqlClient;
public class DatabaseConnectionExample
{
public static void ConnectAndQuery()
{
var connectionString = "Data Source=127.0.0.1,1433;Initial Catalog=xtlab;User
ID=SA;Password=Password123";
using var connection = new SqlConnection(connectionString);
connection.StatisticsEnabled = true;
try
{
connection.Open();
Console.WriteLine("Kết nối thành công.");
connection.Close();
Console.WriteLine("Kết nối đã đóng.");
}
var stats = connection.RetrieveStatistics();
Console.WriteLine("Thống kê kết nối:");
foreach (var key in stats.Keys)
{
Console.WriteLine($"{key}: {stats[key]}");
}
}
Bài 50
using System;
using System.Data;
using System.Data.SqlClient;
namespace SqlCommandExamples
{
class Program
{
static void Main(string[] args)
{
ExecuteScalarExample();
ExecuteNonQueryExample();
ExecuteReaderExample();
CallStoredProcedure();
}
string queryString = @"INSERT INTO Shippers (Hoten, Sodienthoai) VALUES (@Hoten, @Sodienthoai);
SELECT CAST(scope_identity() AS int)";
using SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("@Hoten", "Abc");
command.Parameters.AddWithValue("@Sodienthoai", 123456);
connection.Close();
}
connection.Close();
}
static void ExecuteReaderExample()
{
string sqlconnectStr = @"Data Source=localhost,1433;Initial Catalog=xtlab;User
ID=SA;Password=Password123";
using SqlConnection connection = new SqlConnection(sqlconnectStr);
connection.Open();
if (reader.HasRows)
{
while (reader.Read())
{
int danhmuc = reader.GetInt32(0);
string tendanhmuc = reader["TenDanhMuc"].ToString();
string mota = reader["MoTa"].ToString();
connection.Close();
}
connection.Close();
}
}
}