Created
March 8, 2019 15:55
-
-
Save t3knoid/4f36040d06f99e07548d5bc0f2775719 to your computer and use it in GitHub Desktop.
A handy class to interface with Microsoft SQL
This file contains hidden or 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
using System.Data.SqlClient; | |
using System.Text; | |
class MSSQLQuery | |
{ | |
#region Public Properties | |
public string Database | |
{ | |
get; | |
set; | |
} | |
public string Server | |
{ | |
get; | |
set; | |
} | |
public string Timeout | |
{ | |
get; | |
set; | |
} | |
public string UserID | |
{ | |
get; | |
set; | |
} | |
public string Password | |
{ | |
get; | |
set; | |
} | |
#endregion | |
#region Fields | |
private readonly string connectionFormat = "Database={0}; Server={1}; Connect Timeout={2}; User Id={3};Password={4};"; | |
private string connectionString = String.Empty; | |
#endregion | |
#region Constructor | |
public MSSQLQuery() | |
{ | |
connectionString = String.Format(connectionFormat, Database, Server, Timeout, UserID, Password); | |
} | |
public MSSQLQuery(string database, string server, string timeout, string userid, string password) | |
{ | |
Database = database; | |
Server = server; | |
Timeout = timeout; | |
UserID = userid; | |
Password = password; | |
} | |
#endregion | |
#region Methods | |
public bool IsServerConnected() | |
{ | |
using (SqlConnection connection = new SqlConnection(connectionString)) | |
{ | |
try | |
{ | |
connection.Open(); | |
return true; | |
} | |
catch (SqlException) | |
{ | |
return false; | |
} | |
} | |
} | |
public MSSQLResult selectQuery(string queryString) | |
{ | |
string connectionString = String.Format(connectionFormat, Database, Server, Timeout, UserID, Password); | |
SqlConnection connection = new SqlConnection(connectionString); | |
SqlCommand command = new SqlCommand(queryString, connection); | |
try | |
{ | |
connection.Open(); | |
SqlDataReader reader = command.ExecuteReader(); | |
return new MSSQLResult{success = true, reader = reader, connection = connection}; | |
} | |
catch (Exception e) | |
{ | |
connection.Close(); | |
connection.Dispose(); | |
return new MSSQLResult{success = false, message = e.Message}; | |
} | |
} | |
public MSSQLResult updateQuery(string queryString) | |
{ | |
string connectionString = String.Format(connectionFormat, Database, Server, Timeout, UserID, Password); | |
SqlConnection connection = new SqlConnection(connectionString); | |
SqlCommand command = new SqlCommand(queryString, connection); | |
try | |
{ | |
connection.Open(); | |
command.ExecuteNonQuery(); | |
return new MSSQLResult{success = true, connection = connection}; | |
} | |
catch (Exception e) | |
{ | |
connection.Close(); | |
connection.Dispose(); | |
return new MSSQLResult{success = false, message = e.Message}; | |
} | |
} | |
public string getData(SqlDataReader reader, string column) | |
{ | |
switch (Type.GetTypeCode(reader.GetFieldType(reader.GetOrdinal(column)))) | |
{ | |
case TypeCode.Boolean: | |
return reader.GetBoolean(reader.GetOrdinal(column)).ToString(); | |
case TypeCode.Byte: | |
return reader.GetByte(reader.GetOrdinal(column)).ToString(); | |
case TypeCode.Char: | |
return reader.GetChar(reader.GetOrdinal(column)).ToString(); | |
case TypeCode.DateTime: | |
return reader.GetDateTime(reader.GetOrdinal(column)).ToString("yyyy/mm/dd HH:mm:ss"); | |
case TypeCode.Decimal: | |
return reader.GetDecimal(reader.GetOrdinal(column)).ToString(); | |
case TypeCode.Double: | |
return reader.GetDouble(reader.GetOrdinal(column)).ToString(); | |
case TypeCode.Int16: | |
return reader.GetInt16(reader.GetOrdinal(column)).ToString(); | |
case TypeCode.Int32: | |
return reader.GetInt32(reader.GetOrdinal(column)).ToString(); | |
case TypeCode.Int64: | |
return reader.GetInt64(reader.GetOrdinal(column)).ToString(); | |
case TypeCode.String: | |
return reader.GetString(reader.GetOrdinal(column)); | |
default: | |
return ""; | |
} | |
} | |
#endregion | |
} | |
public class MSSQLResult | |
{ | |
public bool success | |
{ | |
get; | |
set; | |
} | |
public string message | |
{ | |
get; | |
set; | |
} | |
public SqlDataReader reader | |
{ | |
get; | |
set; | |
} | |
public SqlConnection connection | |
{ | |
get; | |
set; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment