-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
XuanThuLab
committed
Aug 23, 2020
1 parent
a4a4257
commit 170b9d4
Showing
8 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
// Use IntelliSense to find out which attributes exist for C# debugging | ||
// Use hover for the description of the existing attributes | ||
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": ".NET Core Launch (console)", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "build", | ||
// If you have changed target frameworks, make sure to update the program path. | ||
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/ADO_01_SqlConnection.dll", | ||
"args": [], | ||
"cwd": "${workspaceFolder}", | ||
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console | ||
"console": "internalConsole", | ||
"stopAtEntry": false | ||
}, | ||
{ | ||
"name": ".NET Core Attach", | ||
"type": "coreclr", | ||
"request": "attach", | ||
"processId": "${command:pickProcess}" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"build", | ||
"${workspaceFolder}/ADO_01_SqlConnection.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "publish", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"publish", | ||
"${workspaceFolder}/ADO_01_SqlConnection.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "watch", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"watch", | ||
"run", | ||
"${workspaceFolder}/ADO_01_SqlConnection.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<!-- <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" /> --> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.6" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.6" /> | ||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.6" /> | ||
<!-- <PackageReference Include="System.Data.SqlClient" Version="4.6.1" /> --> | ||
<PackageReference Include="System.Data.SqlClient" Version="4.8.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
|
||
namespace ADO_01_SqlConnection | ||
{ | ||
public class Exam1 { | ||
|
||
public static void Test () { | ||
|
||
// TẠO CHUỖI KẾT NỐI bằng SqlConnectionStringBuilder | ||
SqlConnectionStringBuilder stringBuilder = new SqlConnectionStringBuilder (); | ||
stringBuilder["Server"] = "127.0.0.1,1433"; | ||
stringBuilder["Database"] = "xtlab"; | ||
stringBuilder["User Id"] = "SA"; | ||
stringBuilder["Password"] = "Password123"; | ||
String sqlConnectionString = stringBuilder.ToString (); | ||
|
||
SqlConnection connection = new SqlConnection (sqlConnectionString); | ||
// kích hoạt chế độ thu thập thông tin thống kê khi truy vấn | ||
connection.StatisticsEnabled = true; | ||
|
||
Console.WriteLine ($"{"ConnectionString ", 17} : {stringBuilder}"); | ||
Console.WriteLine ($"{"DataSource ", 17} : {connection.DataSource}"); | ||
|
||
// Bắt sự kiện trạng thái kết nối thay đổi | ||
connection.StateChange += (object sender, StateChangeEventArgs e) => { | ||
Console.WriteLine ($"Kết nối thay đổi: {e.CurrentState}, trạng thái trước: " + $"{e.OriginalState}"); | ||
}; | ||
|
||
// mở kết nối | ||
connection.Open (); | ||
|
||
// Dùng SqlCommand thi hành SQL - sẽ tìm hiểu sau | ||
using (SqlCommand command = connection.CreateCommand ()) { | ||
|
||
// Câu truy vấn SQL | ||
command.CommandText = "select top(5) * from Sanpham"; | ||
var reader = command.ExecuteReader (); | ||
// Đọc kết quả truy vấn | ||
Console.WriteLine ("\r\nCÁC SẢN PHẨM:"); | ||
Console.WriteLine ($"{"SanphamID ", 10} {"TenSanpham "}"); | ||
while (reader.Read ()) { | ||
Console.WriteLine ($"{reader["SanphamID"], 10} {reader["TenSanpham"]}"); | ||
} | ||
} | ||
|
||
|
||
|
||
// Lấy thống kê và in số liệu thống kê | ||
Console.WriteLine("Thông tin thống kê các tương tác đã thực hiện trên kết nôis"); | ||
var dicStatics = connection.RetrieveStatistics (); | ||
foreach (var key in dicStatics.Keys) { | ||
Console.WriteLine ($"{key, 17} : {dicStatics[key]}"); | ||
} | ||
|
||
// Không dùng đến kết nối thì phải đóng lại (giải phóng) | ||
connection.Close (); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Configuration.Json; | ||
using System.IO; | ||
|
||
namespace ADO_01_SqlConnection | ||
{ | ||
class Program | ||
{ | ||
// Lấy chuỗi kết nối từ file config định dạng json, | ||
// Điểm lưu: csl:ketnoi2 | ||
public static string GetConnectString() { | ||
var configBuilder = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) // file config ở thư mục hiện tại | ||
.AddJsonFile("appconfig.json"); // nạp config định dạng JSON | ||
var configurationroot = configBuilder.Build(); // Tạo configurationroot | ||
return configurationroot["csdl:ketnoi2"]; | ||
|
||
} | ||
|
||
static void Main(string[] args) | ||
{ | ||
// Exam1.Test(); | ||
|
||
String sqlConnectString = GetConnectString(); | ||
SqlConnection connection = new SqlConnection(GetConnectString()); | ||
connection.StatisticsEnabled = true; | ||
connection.FireInfoMessageEventOnUserErrors = true; | ||
|
||
connection.StateChange += (object sender, StateChangeEventArgs e) => { | ||
Console.WriteLine($"Trạng thái hiện tại: {e.CurrentState}, trạng thái trước: " + $"{e.OriginalState}"); | ||
}; | ||
|
||
// Mở kết nối | ||
connection.Open(); | ||
|
||
// Thực hiện các truy vấn tại đây ... | ||
|
||
connection.Close(); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"csdl" : { | ||
"ketnoi1" : "Data Source=127.0.0.1,1433;Initial Catalog=xtlab;User ID=SA;Password=Password123", | ||
"ketnoi2" : "Data Source=localhost,1433;Initial Catalog=xtlab;User ID=SA;Password=Password123" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.