0% found this document useful (0 votes)
8 views5 pages

sql helper

Uploaded by

bodawarda5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

sql helper

Uploaded by

bodawarda5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

using System;

using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;

namespace MMMS.Admin.Web.Helpers
{
public class SQL
{
/// <summary>
/// The name of the machine.
/// </summary>
private static readonly string MachineName = Environment.MachineName;
private string ServerUserName { set; get; }
private string ServerPassword { set; get; }
private string InitialDB { set; get; }
private string Server { set; get; }
private string ServerPort { set; get; }
private string ConnString => Conn ?? $"Server=tcp:{Server},
{ServerPort};Initial Catalog={InitialDB};Persist Security Info=False;User
ID={ServerUserName};Password={ServerPassword};MultipleActiveResultSets=False;Encryp
t=True;TrustServerCertificate=False;Connection Timeout=30";
private string Conn { set; get; }

/// <summary>
/// Initializes a new instance of the <see cref="T:MPLModels.SQL"/> class.
/// </summary>
/// <param name="server">Server IP.</param>
/// <param name="port">Server Port.</param>
/// <param name="Username">Server Username.</param>
/// <param name="Password">Server Password.</param>
/// <param name="Database">Targeted Database.</param>
public SQL(string server, string port, string Username, string Password,
string Database)
{
ServerUserName = Username;
ServerPassword = Password;
InitialDB = Database;
Server = server;
ServerPort = port;
}

/// <summary>
/// Initializes a new instance of the <see cref="T:MPLModels.SQL"/> class.
/// </summary>
/// <param name="ConnectionString">Connection string.</param>
public SQL(string ConnectionString)
{
Conn = ConnectionString;
}

/// <summary>
/// Executes the query commands async.
/// </summary>
/// <returns>The query result in Datatable.</returns>
/// <param name="cmd">SQL Command.</param>
public async Task<DataTable> ExecuteQueryAsync(SqlCommand cmd)
{
DataTable result = new DataTable();
using (SqlConnection con = new SqlConnection(ConnString))
{
try
{
await Task.Run(() =>
{
cmd.Connection = con;

using (SqlDataAdapter da = new SqlDataAdapter(cmd))


{
da.Fill(result);
}
});
return result;

}
catch (Exception ex)
{
return result;
}
finally
{
con.Close();
}
}
}

/// <summary>
/// Executes the query commands.
/// </summary>
/// <returns>The query result in Datatable.</returns>
/// <param name="cmd" >SQL Command.</param>
public DataTable ExecuteQuery(SqlCommand cmd)
{
DataTable result = new DataTable();
using (SqlConnection con = new SqlConnection(ConnString))
{
try
{
cmd.Connection = con;

using (SqlDataAdapter da = new SqlDataAdapter(cmd))


{
da.Fill(result);
}
return result;

}
catch (Exception ex)
{
return null;
}
finally
{
con.Close();
}
}
}

/// <summary>
/// Executes the non query commands async.
/// </summary>
/// <returns>number or rows affected.</returns>
/// <param name="cmd">SQL Command.</param>
public async Task<int> ExecuteNonQueryAsync(SqlCommand cmd)
{
using (SqlConnection con = new SqlConnection(ConnString))
{
try
{
int x = -1;
await Task.Run(async () =>
{
cmd.Connection = con;
con.Open();
x = await cmd.ExecuteNonQueryAsync();
});
return x;

}
catch (Exception ex)
{
return -1;
}
finally
{
con.Close();
}
}
}

/// <summary>
/// Executes the non query commands.
/// </summary>
/// <returns>number or rows affected.</returns>
/// <param name="cmd">SQL Command.</param>
public int ExecuteNonQuery(SqlCommand cmd)
{
using (SqlConnection con = new SqlConnection(ConnString))
{
try
{
cmd.Connection = con;
con.Open();
return cmd.ExecuteNonQuery(); ;

}
catch (Exception ex)
{
return -1;
}
finally
{
con.Close();
}
}
}

/// <summary>
/// Executes the scalar command async.
/// </summary>
/// <returns>The first value in first row and first column as
string.</returns>
/// <param name="cmd">SQL Command.</param>
public async Task<string> ExecuteScalarAsync(SqlCommand cmd)
{
using (SqlConnection con = new SqlConnection(ConnString))
{
try
{
var x = new object();
await Task.Run(async () =>
{
cmd.Connection = con;
con.Open();
x = await cmd.ExecuteScalarAsync();
});

return x.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return ex.Message;
}
finally
{
con.Close();
}
}
}

/// <summary>
/// Executes the scalar command.
/// </summary>
/// <returns>The first value in first row and first column as
string.</returns>
/// <param name="cmd">SQL Command.</param>
public string ExecuteScalar(SqlCommand cmd)
{
using (SqlConnection con = new SqlConnection(ConnString))
{
try
{
cmd.Connection = con;
con.Open();
return cmd.ExecuteScalar().ToString();
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
con.Close();
}
}
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy