From 52eb1e7df71c614dcb498549c2dbe50cf2f9541f Mon Sep 17 00:00:00 2001 From: syneffort Date: Wed, 28 Dec 2022 18:24:15 +0900 Subject: [PATCH] base lib --- MSSQLServer_Console/MSSQLServer_Console.sln | 31 +++ .../MSSQLServer_Console/App.config | 6 + .../MSSQLServer_Console.csproj | 58 +++++ .../MSSQLServer_Console/Program.cs | 59 +++++ .../Properties/AssemblyInfo.cs | 36 +++ .../PSqlSeverWrapper/PSqlServer.cs | 214 ++++++++++++++++++ .../PSqlSeverWrapper/PSqlServerWrapper.csproj | 51 +++++ .../Properties/AssemblyInfo.cs | 36 +++ 8 files changed, 491 insertions(+) create mode 100644 MSSQLServer_Console/MSSQLServer_Console.sln create mode 100644 MSSQLServer_Console/MSSQLServer_Console/App.config create mode 100644 MSSQLServer_Console/MSSQLServer_Console/MSSQLServer_Console.csproj create mode 100644 MSSQLServer_Console/MSSQLServer_Console/Program.cs create mode 100644 MSSQLServer_Console/MSSQLServer_Console/Properties/AssemblyInfo.cs create mode 100644 MSSQLServer_Console/PSqlSeverWrapper/PSqlServer.cs create mode 100644 MSSQLServer_Console/PSqlSeverWrapper/PSqlServerWrapper.csproj create mode 100644 MSSQLServer_Console/PSqlSeverWrapper/Properties/AssemblyInfo.cs diff --git a/MSSQLServer_Console/MSSQLServer_Console.sln b/MSSQLServer_Console/MSSQLServer_Console.sln new file mode 100644 index 0000000..bd5cdc6 --- /dev/null +++ b/MSSQLServer_Console/MSSQLServer_Console.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32630.194 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MSSQLServer_Console", "MSSQLServer_Console\MSSQLServer_Console.csproj", "{88D9ABEA-C8C1-45B7-86BF-A0A04124B00B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSqlServerWrapper", "PSqlSeverWrapper\PSqlServerWrapper.csproj", "{34E74A48-F0B4-4F7F-9827-11AA4E0052D0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {88D9ABEA-C8C1-45B7-86BF-A0A04124B00B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {88D9ABEA-C8C1-45B7-86BF-A0A04124B00B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {88D9ABEA-C8C1-45B7-86BF-A0A04124B00B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {88D9ABEA-C8C1-45B7-86BF-A0A04124B00B}.Release|Any CPU.Build.0 = Release|Any CPU + {34E74A48-F0B4-4F7F-9827-11AA4E0052D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {34E74A48-F0B4-4F7F-9827-11AA4E0052D0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {34E74A48-F0B4-4F7F-9827-11AA4E0052D0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {34E74A48-F0B4-4F7F-9827-11AA4E0052D0}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {81236D94-A83B-4C4D-851B-6AAC290B7821} + EndGlobalSection +EndGlobal diff --git a/MSSQLServer_Console/MSSQLServer_Console/App.config b/MSSQLServer_Console/MSSQLServer_Console/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/MSSQLServer_Console/MSSQLServer_Console/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MSSQLServer_Console/MSSQLServer_Console/MSSQLServer_Console.csproj b/MSSQLServer_Console/MSSQLServer_Console/MSSQLServer_Console.csproj new file mode 100644 index 0000000..d4a3da5 --- /dev/null +++ b/MSSQLServer_Console/MSSQLServer_Console/MSSQLServer_Console.csproj @@ -0,0 +1,58 @@ + + + + + Debug + AnyCPU + {88D9ABEA-C8C1-45B7-86BF-A0A04124B00B} + Exe + MSSQLServer_Console + MSSQLServer_Console + v4.5 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + {34e74a48-f0b4-4f7f-9827-11aa4e0052d0} + PSqlServerWrapper + + + + \ No newline at end of file diff --git a/MSSQLServer_Console/MSSQLServer_Console/Program.cs b/MSSQLServer_Console/MSSQLServer_Console/Program.cs new file mode 100644 index 0000000..142f3ab --- /dev/null +++ b/MSSQLServer_Console/MSSQLServer_Console/Program.cs @@ -0,0 +1,59 @@ +using PSqlSeverWrapper; +using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MSSQLServer_Console +{ + class Program + { + static void Main(string[] args) + { + PSqlServer.ServerName = "peacecloud.synology.me,21433"; + PSqlServer.DatabaseName = "Study"; + PSqlServer.UserId = "study"; + PSqlServer.Password = "Study123$"; + + //PSqlServer.ExecuteQuery( + //@"CREATE TABLE member ( + // name VARCHAR(50), + // age INT, + // guid VARCHAR(50) NOT NULL PRIMARY KEY + //);"); + + //Console.WriteLine("table create."); + + Random rnd = new Random(); + //using (SqlConnection conn = PSqlServer.Connection) + //{ + // conn.Open(); + // for (int i = 0; i < 2000; i++) + // { + // string guid = Guid.NewGuid().ToString(); + // int age = rnd.Next(1, 100); + // string name = $"Robo#{i + 1}"; + // string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; + // PSqlServer.ExecuteQuery(conn, insertQry); + // } + + // conn.Close(); + //} + + string[] queries = new string[2000]; + for (int i = 0; i < 2000; i++) + { + string guid = Guid.NewGuid().ToString(); + int age = rnd.Next(1, 100); + string name = $"Robo#{i + 1}"; + queries[i] = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; + } + + PSqlServer.ExecuteQuery(queries); + + Console.WriteLine("finish"); + } + } +} diff --git a/MSSQLServer_Console/MSSQLServer_Console/Properties/AssemblyInfo.cs b/MSSQLServer_Console/MSSQLServer_Console/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..48c192a --- /dev/null +++ b/MSSQLServer_Console/MSSQLServer_Console/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("MSSQLServer_Console")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MSSQLServer_Console")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("88d9abea-c8c1-45b7-86bf-a0a04124b00b")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/MSSQLServer_Console/PSqlSeverWrapper/PSqlServer.cs b/MSSQLServer_Console/PSqlSeverWrapper/PSqlServer.cs new file mode 100644 index 0000000..83ccc6e --- /dev/null +++ b/MSSQLServer_Console/PSqlSeverWrapper/PSqlServer.cs @@ -0,0 +1,214 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PSqlSeverWrapper +{ + public class PSqlServer + { + public static string ServerName { get; set; } + public static string DatabaseName { get; set; } + public static string UserId { private get; set; } + public static string Password { private get; set; } + public static int MinPoolSize { get; set; } = 20; + public static int MaxPoolSize { get; set; } = 100; + public static int TimeOut { get; set; } = 15; + + private static string ConnString + { + get { return $"server={ServerName}; database={DatabaseName}; uid={UserId}; pwd={Password}; min pool size = {MinPoolSize}; max pool size = {MaxPoolSize}; connection timeout = {TimeOut};"; } + } + + public static SqlConnection Connection + { + get + { + if (!CheckBasicInfo()) + throw new ArgumentException("Invalid connection info"); + + try + { + return new SqlConnection(ConnString); + } + catch (Exception ex) + { + Debug.WriteLine(ex.ToString()); + throw; + } + } + } + + public static void ExecuteQuery(string query) + { + try + { + using (SqlConnection conn = Connection) + { + conn.Open(); + SqlCommand command = new SqlCommand(query, conn); + command.ExecuteNonQuery(); + Connection.Close(); + } + } + catch (Exception ex) + { + Debug.WriteLine(ex.ToString()); + throw; + } + } + + public static void ExecuteQuery(SqlConnection openedConn, string query) + { + try + { + SqlCommand command = new SqlCommand(query, openedConn); + command.ExecuteNonQuery(); + } + catch (Exception ex) + { + Debug.WriteLine(ex.ToString()); + throw; + } + } + + public static void ExecuteQueryAsync(SqlConnection openedConn, string query) + { + try + { + SqlCommand command = new SqlCommand(query, openedConn); + command.ExecuteNonQueryAsync(); + } + catch (Exception ex) + { + Debug.WriteLine(ex.ToString()); + throw; + } + } + + public static void ExecuteQuery(string[] queries) + { + try + { + using (SqlConnection conn = Connection) + { + conn.Open(); + ExecuteQuery(conn, "BEGIN TRAN;"); + + for (int i = 0; i < queries.Length; i++) + { + SqlCommand command = new SqlCommand(queries[i], conn); + command.ExecuteNonQuery(); + } + + ExecuteQuery(conn, "COMMIT TRAN;"); + Connection.Close(); + } + } + catch (Exception ex) + { + Debug.WriteLine(ex.ToString()); + throw; + } + } + + public static void ExecuteQueryAsync(string query) + { + try + { + using (SqlConnection conn = Connection) + { + conn.Open(); + SqlCommand command = new SqlCommand(query, conn); + command.ExecuteNonQueryAsync(); + Connection.Close(); + } + } + catch (Exception ex) + { + Debug.WriteLine(ex.ToString()); + throw; + } + } + + public static DataSet ExecuteSelectQuery(string query) + { + try + { + using (SqlConnection conn = Connection) + { + DataSet dataSet = new DataSet(); + + conn.Open(); + SqlDataAdapter adapter = new SqlDataAdapter(query, conn); + adapter.Fill(dataSet); + Connection.Close(); + + return dataSet; + } + } + catch (Exception ex) + { + Debug.WriteLine(ex.ToString()); + throw; + } + } + + public static DataSet ExecuteSelectQuery(SqlConnection openedConn, string query) + { + try + { + DataSet dataSet = new DataSet(); + SqlDataAdapter adapter = new SqlDataAdapter(query, openedConn); + adapter.Fill(dataSet); + + return dataSet; + } + catch (Exception ex) + { + Debug.WriteLine(ex.ToString()); + throw; + } + } + + public static SqlDataAdapter GetAdapterSelectQuery(SqlConnection openedConn, string query, bool withCommandBuilder = true) + { + try + { + SqlDataAdapter adapter = new SqlDataAdapter(query, openedConn); + if (withCommandBuilder) + { + SqlCommandBuilder builder = new SqlCommandBuilder(adapter); + adapter.UpdateCommand = builder.GetUpdateCommand(); + adapter.DeleteCommand = builder.GetDeleteCommand(); + adapter.InsertCommand = builder.GetInsertCommand(); + } + + return adapter; + } + catch (Exception ex) + { + Debug.WriteLine(ex.ToString()); + throw; + } + } + + private static bool CheckBasicInfo() + { + if (string.IsNullOrEmpty(ServerName)) + return false; + if (string.IsNullOrEmpty(DatabaseName)) + return false; + if (string.IsNullOrEmpty(UserId)) + return false; + if (string.IsNullOrEmpty(Password)) + return false; + + return true; + } + } +} diff --git a/MSSQLServer_Console/PSqlSeverWrapper/PSqlServerWrapper.csproj b/MSSQLServer_Console/PSqlSeverWrapper/PSqlServerWrapper.csproj new file mode 100644 index 0000000..35c3768 --- /dev/null +++ b/MSSQLServer_Console/PSqlSeverWrapper/PSqlServerWrapper.csproj @@ -0,0 +1,51 @@ + + + + + Debug + AnyCPU + {34E74A48-F0B4-4F7F-9827-11AA4E0052D0} + Library + Properties + PSqlServerWrapper + PSqlServerWrapper + v4.5 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MSSQLServer_Console/PSqlSeverWrapper/Properties/AssemblyInfo.cs b/MSSQLServer_Console/PSqlSeverWrapper/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0d8e75f --- /dev/null +++ b/MSSQLServer_Console/PSqlSeverWrapper/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("PSqlSeverWrapper")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("PSqlSeverWrapper")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("34e74a48-f0b4-4f7f-9827-11aa4e0052d0")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]