|
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
/// <summary>
/// Summary description for DataProvider
/// </summary>
public class DataProvider : IDisposable
{
private string _connectionString = "";
/// <summary>
/// Error reading from the database
/// </summary>
private bool _error;
public DataProvider()
{
_connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindSqlServer"].ConnectionString;
}
public object GetCustomers()
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
connection.Open();
string queryString = "SELECT CustomerID, ContactName, ContactTitle, Address, CompanyName, Phone, Fax FROM Customers";
SqlCommand command = new SqlCommand(queryString, connection);
//SqlDataReader reader = command.ExecuteReader();
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
//object result = reader;
connection.Close();
//reader.Close();
return ds;
}
}
// public object GetOrders(DateTime from, DateTime to)
// {
// _error = false;
// try
// {
// using (SqlConnection connection = new SqlConnection(_connectionString))
// {
// connection.Open();
// string queryString = @"SELECT o.OrderID, c.ContactName as CustomerName, o.OrderDate, o.ShippedDate, o.ShipAddress FROM Orders o
// INNER JOIN Customers c ON (o.CustomerID = c.CustomerID) where datediff(day, @from, OrderDate) >=0 and datediff(day, @to, OrderDate) <=0";
// SqlCommand command = new SqlCommand(queryString, connection);
// SqlParameter para1 = new SqlParameter("@from", SqlDbType.DateTime);
// SqlParameter para2 = new SqlParameter("@to", SqlDbType.DateTime);
// if (from == DateTime.MinValue)
// para1.Value = System.Data.SqlTypes.SqlDateTime.MinValue.Value;
// else
// para1.Value = new System.Data.SqlTypes.SqlDateTime(from);
// if (to == DateTime.MinValue)
// para2.Value = System.Data.SqlTypes.SqlDateTime.MaxValue.Value;
// else
// para2.Value = to;
// command.Parameters.Add(para1);
// command.Parameters.Add(para2);
// IDataReader reader = command.ExecuteReader();
// List<Order> objOrders = new List<Order>();
// while (reader.Read())
// {
// Order objOrder = new Order();
// objOrder.OrderID = (int)reader["OrderID"];
// objOrder.CustomerName = (string)reader["CustomerName"];
// //objOrder.OrderDate = (DateTime)reader["OrderDate"];
// if (reader["OrderDate"] != DBNull.Value)
// objOrder.OrderDate = (DateTime)reader["OrderDate"];
// else
// objOrder.OrderDate = null;
// //objOrder.ShippedDate = (reader["ShippedDate"] == DBNull.Value?null:(DateTime)reader["ShippedDate"]);
// if (reader["ShippedDate"] != DBNull.Value)
// objOrder.ShippedDate = (DateTime)reader["ShippedDate"];
// else
// objOrder.ShippedDate = null;
// objOrder.ShipAddress = (string)reader["ShipAddress"];
// objOrders.Add(objOrder);
// }
// return objOrders;
// }
// }
// catch (System.Exception ex)
// {
// _error = true;
// return null;
// }
// }
public int SumOrderAmount(int orderID)
{
_error = false;
try
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
connection.Open();
string queryString = @"select SUM(d.UnitPrice * d.Quantity - d.Discount) as OrderAmount from [Order Details] d
where d.OrderID = @OrderID";
SqlCommand command = new SqlCommand(queryString, connection);
SqlParameter para = new SqlParameter("@OrderID", SqlDbType.Int);
para.Value = orderID;
command.Parameters.Add(para);
return (int)Math.Round((double)command.ExecuteScalar());
}
}
catch (System.Exception ex)
{
_error = true;
Exception = ex.Message;
return 0;
}
}
public object GetOrders(DateTime from, DateTime to)
{
_error = false;
try
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
connection.Open();
string queryString = @"SELECT o.OrderID, c.ContactName as CustomerName, o.OrderDate, o.ShippedDate, o.ShipAddress FROM Orders o
INNER JOIN Customers c ON (o.CustomerID = c.CustomerID) where datediff(day, @from, OrderDate) >=0 and datediff(day, @to, OrderDate) <=0 order by OrderID Desc";
SqlCommand command = new SqlCommand(queryString, connection);
SqlParameter para1 = new SqlParameter("@from", SqlDbType.DateTime);
SqlParameter para2 = new SqlParameter("@to", SqlDbType.DateTime);
if (from == DateTime.MinValue)
para1.Value = System.Data.SqlTypes.SqlDateTime.MinValue.Value;
else
para1.Value = new System.Data.SqlTypes.SqlDateTime(from);
if (to == DateTime.MinValue)
para2.Value = System.Data.SqlTypes.SqlDateTime.MaxValue.Value;
else
para2.Value = to;
command.Parameters.Add(para1);
command.Parameters.Add(para2);
IDataReader reader = command.ExecuteReader();
List<Order> objOrders = DataHelper.PopulateEntities<Order>(reader);
return objOrders;
}
}
catch (System.Exception ex)
{
_error = true;
Exception = ex.Message;
return null;
}
}
public object GetOrderDetails(int paraOrderID)
{
_error = false;
try
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
connection.Open();
string queryString = @"SELECT p.ProductName, d.UnitPrice, d.Quantity, d.Discount FROM [Order Details] d
INNER JOIN Orders o ON (o.OrderID = d.OrderID)
INNER JOIN Products p ON (p.ProductID = d.ProductID)
WHERE d.OrderID = @OrderID";
SqlCommand command = new SqlCommand(queryString, connection);
SqlParameter para = new SqlParameter("@OrderID", SqlDbType.Int);
para.Value = paraOrderID;
command.Parameters.Add(para);
IDataReader reader = command.ExecuteReader();
List<OrderDetail> objOrderDetails = DataHelper.PopulateEntities<OrderDetail>(reader);
return objOrderDetails;
}
}
catch (System.Exception ex)
{
_error = true;
Exception = ex.Message;
return null;
}
}
/// <summary>
/// Indicates some error ocurred when getting data
/// </summary>
public bool Error {
get { return _error; }
}
/// <summary>
/// Indicates some error ocurred when getting data
/// </summary>
public string Exception
{
get;
set;
}
#region IDisposable Members
/// <summary>
/// Releases resources used by this class
/// </summary>
public void Dispose()
{
GC.SuppressFinalize(this);
}
#endregion
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I love working with Web-based workflow automation systems, ERP systems for SME, Data Visualization and Augmented Intelligence.
I have been working in ASP.NET for more than 12 years. I've also been working on Java and Windows-based apps for more than 5 years. My core competencies include ASP.NET (+net core), MVC, Restful API, Advance JavaScript, JQuery, Bootstrap, SubSonic, Dapper, Entity Framework, Lucne.net, ElasticSearch Ajax... I'm particularly interested in building smart apps with great UI/UX and high earned value.
I love to write elegant code. I am a type of pragmatic personality.
Recently, I've been interested in developing SPA apps using Angular with NodeJS backend. I also love to write the progressive web apps which could be the next big thing for the mobile web.
Feel free to discuss with me at: phamdinhtruong@gmail.com