You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
5.7 KiB
214 lines
5.7 KiB
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;
|
|
}
|
|
}
|
|
}
|
|
|