using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Hugo.BookShop.DbBase
{
/// <summary>
/// base.
/// </summary>
public abstract class Base
{
#region "Fields of base calss"
/// <summary>
/// connecting to Database
/// </summary>
protected static string strConn = ConfigurationSettings.AppSettings["strConnection"];
/// <summary>
/// SQL command
/// </summary>
protected static string strSQL;
#endregion
#region "Properties of base class"
private int m_ID;
private string m_Name;
/// <summary>
/// Property:ID
/// </summary>
public int ID
{
get
{
return m_ID;
}
set
{
m_ID = value;
}
}
/// <summary>
/// name
/// </summary>
public string Name
{
get
{
return m_Name;
}
set
{
m_Name = value;
}
}
#endregion
#region "Functions of base class"
public Base()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// executing SQL commands
/// </summary>
/// <param name="strSQL">string</param>
/// <returns>return int</returns>
protected static int ExecuteSql(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
{
myCn.Open();
myCmd.ExecuteNonQuery();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
/// <summary>
///executing SQL commands
/// </summary>
/// <param name="strSQL">要执行的SQL语句,为字符串类型string</param>
/// <returns>返回执行情况,整形int</returns>
protected static int ExecuteSqlEx(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
{
myCn.Open();
SqlDataReader myReader = myCmd.ExecuteReader();
if(myReader.Read())
{
return 0;
}
else
{
return 1;
}
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
/// <summary>
/// get dataset
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>(DataSet)</returns>
protected static DataSet ExecuteSql4Ds(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
try
{
myCn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strSQL,myCn);
DataSet ds = new DataSet("ds");
sda.Fill(ds);
return ds;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCn.Close();
}
}
/// <summary>
/// get single value
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>(int)</returns>
protected static int ExecuteSql4Value(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
{
myCn.Open();
object r = myCmd.ExecuteScalar();
if(Object.Equals(r,null))
{
throw new Exception("value unavailable!");
}
else
{
return (int)r;
}
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
/// <summary>
/// get object
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>(object)</returns>
protected static object ExecuteSql4ValueEx(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
{
myCn.Open();
object r = myCmd.ExecuteScalar();
if(Object.Equals(r,null))
{
throw new Exception("object unavailable!");
}
else
{
return r;
}
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
/// <summary>
/// execute multipul SQL commands
/// </summary>
/// <param name="strSQLs">string</param>
/// <returns>int</returns>
protected static int ExecuteSqls(string[] strSQLs)
{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand();
int j=strSQLs.Length;
try
{
myCn.Open();
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
SqlTransaction myTrans = myCn.BeginTransaction();
try
{
myCmd.Connection = myCn;
myCmd.Transaction = myTrans;
foreach(string str in strSQLs)
{
myCmd.CommandText = str;
myCmd.ExecuteNonQuery();
}
myTrans.Commit();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
{
myTrans.Rollback();
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
#endregion
}
}
============================================
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace Hugo.BookShop
{
/// <summary>
/// Order description。
/// </summary>
public class Order:DbBase.Base,IEnumerator
{
private ArrayList allBooks = new ArrayList();
private int userId;
//private byte status;
private DateTime date = System.DateTime.Now.Date;
private int position=-1;
public Order()
{
}
#region Properties of order class
object IEnumerator.Current
{
get
{
return allBooks[position];
}
}
/// <summary>
/// set detail of order form
/// </summary>
public int Count
{
get
{
strSQL = "Select Count(*) From OrderDetails Where OrderId=" + this.ID;
try
{
return ExecuteSql4Value(strSQL);
}
catch
{
return -1;
}
}
}
/// <summary>
/// sum
/// </summary>
public double Total
{
get
{
strSQL = "Select Sum(A.Price * A.Discount * C.Quantity) as Total From Book A,Orders B ,OrderDetails C Where A.id = C.BookId and B.id = C.OrderId And C.OrderId=" + this.ID;
try
{
return (double)ExecuteSql4ValueEx(strSQL);
}
catch
{
return 0.0;
}
}
}
/// <summary>
/// User ID
/// </summary>
public int UserId
{
get
{
return userId;
}
set
{
userId = value;
}
}
#endregion
#region Functions of order class
/// <summary>
/// return details of this order
/// </summary>
/// <returns></returns>
public ICollection GetItems()
{
return allBooks;
}
public OrderDetails this[int index]
{
get
{
return (OrderDetails)allBooks[index];
}
}
public void Clear()
{
allBooks.Clear();
}
public void Add(OrderDetails value)
{
allBooks.Add(value);
}
bool IEnumerator.MoveNext()
{
position++;
if(position>=allBooks.Count)
{
return false;
}
else
{
return true;
}
}
void IEnumerator.Reset()
{
position = -1;
}
public void RemoveAt(int index)
{
allBooks.RemoveAt(index);
}
public void Remove(int itemId)
{
foreach(OrderDetails item in allBooks)
{
if(itemId == item.BookId)
{
allBooks.Remove(item);
return;
}
}
}
public int Have(int userId)
{
try
{
string [] arrSQL = new String[allBooks.Count];
strSQL = "Insert into Orders(UserId) values(" + userId.ToString() + ")";
ExecuteSql(strSQL);
strSQL = "Select Max(Id) From Orders";
int orderId = ExecuteSql4Value(strSQL);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
Hugo.BookShop.OrderDetails item = new Hugo.BookShop.OrderDetails();
for(int i=0;i<allBooks.Count;i++)
{
item = (Hugo.BookShop.OrderDetails)allBooks[i];
sb.Append("Insert into OrderDetails(OrderId,BookId,Quantity) values(");
sb.Append(orderId.ToString());
sb.Append(",");
sb.Append(item.BookId.ToString());
sb.Append(",");
sb.Append(item.Quantity.ToString());
sb.Append(")");
arrSQL[i] = sb.ToString();
sb.Remove(0,sb.Length);
}
ExecuteSqls(arrSQL);
return orderId;
}
catch
{
throw new Exception("Order books FAILED!");
}
}
public static bool Deal(int orderId)
{
strSQL = "Update OrdersV Set Status =1 Where Id=" + orderId.ToString();
try
{
ExecuteSql4Ds(strSQL);
strSQL = "Update Book set Sales=Sales+1 Where id in(SELECT b.BookId FROM Orders a INNER JOIN OrderDetails b ON a.Id = b.OrderId AND a.Id = " + orderId.ToString() + ")";
ExecuteSql4Ds(strSQL);
return true;
}
catch
{
throw new Exception("Deal with the order failed!");
}
}
public static DataSet GetOrder(int orderId)
{
strSQL = "SELECT UserId, OrderDate,CASE WHEN Status = '1' THEN 'dealt' ELSE 'on progress' END AS Status FROM Orders Where Id=" + orderId.ToString();
try
{
return ExecuteSql4Ds(strSQL);
}
catch
{
throw new Exception("Get order failed!");
}
}
public static DataSet GetOrders()
{
strSQL = "SELECT * FROM OrdersV Where Status=0 or Status IS NULL";
try
{
return ExecuteSql4Ds(strSQL);
}
catch
{
throw new Exception("Get orders failed!");
}
}
public static DataSet GetOrders(int userId)
{
strSQL = "SELECT Id,UserId, OrderDate,CASE WHEN Status = '1' THEN 'dealt' ELSE 'on progress' END AS Status FROM Orders Where UserId=" + userId.ToString();
try
{
return ExecuteSql4Ds(strSQL);
}
catch
{
throw new Exception("Get order failed!");
}
}
public static DataSet GetDetails(int orderId)
{
strSQL = "Select * from DetailsV Where orderId=" + orderId.ToString();
try
{
return ExecuteSql4Ds(strSQL);
}
catch
{
throw new Exception("Get order details failed!");
}
}
#endregion
}
}
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Hugo.BookShop.DbBase
{
/// <summary>
/// base.
/// </summary>
public abstract class Base
{
#region "Fields of base calss"
/// <summary>
/// connecting to Database
/// </summary>
protected static string strConn = ConfigurationSettings.AppSettings["strConnection"];
/// <summary>
/// SQL command
/// </summary>
protected static string strSQL;
#endregion
#region "Properties of base class"
private int m_ID;
private string m_Name;
/// <summary>
/// Property:ID
/// </summary>
public int ID
{
get
{
return m_ID;
}
set
{
m_ID = value;
}
}
/// <summary>
/// name
/// </summary>
public string Name
{
get
{
return m_Name;
}
set
{
m_Name = value;
}
}
#endregion
#region "Functions of base class"
public Base()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// executing SQL commands
/// </summary>
/// <param name="strSQL">string</param>
/// <returns>return int</returns>
protected static int ExecuteSql(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
{
myCn.Open();
myCmd.ExecuteNonQuery();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
/// <summary>
///executing SQL commands
/// </summary>
/// <param name="strSQL">要执行的SQL语句,为字符串类型string</param>
/// <returns>返回执行情况,整形int</returns>
protected static int ExecuteSqlEx(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
{
myCn.Open();
SqlDataReader myReader = myCmd.ExecuteReader();
if(myReader.Read())
{
return 0;
}
else
{
return 1;
}
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
/// <summary>
/// get dataset
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>(DataSet)</returns>
protected static DataSet ExecuteSql4Ds(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
try
{
myCn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strSQL,myCn);
DataSet ds = new DataSet("ds");
sda.Fill(ds);
return ds;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCn.Close();
}
}
/// <summary>
/// get single value
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>(int)</returns>
protected static int ExecuteSql4Value(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
{
myCn.Open();
object r = myCmd.ExecuteScalar();
if(Object.Equals(r,null))
{
throw new Exception("value unavailable!");
}
else
{
return (int)r;
}
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
/// <summary>
/// get object
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>(object)</returns>
protected static object ExecuteSql4ValueEx(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
{
myCn.Open();
object r = myCmd.ExecuteScalar();
if(Object.Equals(r,null))
{
throw new Exception("object unavailable!");
}
else
{
return r;
}
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
/// <summary>
/// execute multipul SQL commands
/// </summary>
/// <param name="strSQLs">string</param>
/// <returns>int</returns>
protected static int ExecuteSqls(string[] strSQLs)
{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand();
int j=strSQLs.Length;
try
{
myCn.Open();
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
SqlTransaction myTrans = myCn.BeginTransaction();
try
{
myCmd.Connection = myCn;
myCmd.Transaction = myTrans;
foreach(string str in strSQLs)
{
myCmd.CommandText = str;
myCmd.ExecuteNonQuery();
}
myTrans.Commit();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
{
myTrans.Rollback();
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
#endregion
}
}
============================================
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace Hugo.BookShop
{
/// <summary>
/// Order description。
/// </summary>
public class Order:DbBase.Base,IEnumerator
{
private ArrayList allBooks = new ArrayList();
private int userId;
//private byte status;
private DateTime date = System.DateTime.Now.Date;
private int position=-1;
public Order()
{
}
#region Properties of order class
object IEnumerator.Current
{
get
{
return allBooks[position];
}
}
/// <summary>
/// set detail of order form
/// </summary>
public int Count
{
get
{
strSQL = "Select Count(*) From OrderDetails Where OrderId=" + this.ID;
try
{
return ExecuteSql4Value(strSQL);
}
catch
{
return -1;
}
}
}
/// <summary>
/// sum
/// </summary>
public double Total
{
get
{
strSQL = "Select Sum(A.Price * A.Discount * C.Quantity) as Total From Book A,Orders B ,OrderDetails C Where A.id = C.BookId and B.id = C.OrderId And C.OrderId=" + this.ID;
try
{
return (double)ExecuteSql4ValueEx(strSQL);
}
catch
{
return 0.0;
}
}
}
/// <summary>
/// User ID
/// </summary>
public int UserId
{
get
{
return userId;
}
set
{
userId = value;
}
}
#endregion
#region Functions of order class
/// <summary>
/// return details of this order
/// </summary>
/// <returns></returns>
public ICollection GetItems()
{
return allBooks;
}
public OrderDetails this[int index]
{
get
{
return (OrderDetails)allBooks[index];
}
}
public void Clear()
{
allBooks.Clear();
}
public void Add(OrderDetails value)
{
allBooks.Add(value);
}
bool IEnumerator.MoveNext()
{
position++;
if(position>=allBooks.Count)
{
return false;
}
else
{
return true;
}
}
void IEnumerator.Reset()
{
position = -1;
}
public void RemoveAt(int index)
{
allBooks.RemoveAt(index);
}
public void Remove(int itemId)
{
foreach(OrderDetails item in allBooks)
{
if(itemId == item.BookId)
{
allBooks.Remove(item);
return;
}
}
}
public int Have(int userId)
{
try
{
string [] arrSQL = new String[allBooks.Count];
strSQL = "Insert into Orders(UserId) values(" + userId.ToString() + ")";
ExecuteSql(strSQL);
strSQL = "Select Max(Id) From Orders";
int orderId = ExecuteSql4Value(strSQL);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
Hugo.BookShop.OrderDetails item = new Hugo.BookShop.OrderDetails();
for(int i=0;i<allBooks.Count;i++)
{
item = (Hugo.BookShop.OrderDetails)allBooks[i];
sb.Append("Insert into OrderDetails(OrderId,BookId,Quantity) values(");
sb.Append(orderId.ToString());
sb.Append(",");
sb.Append(item.BookId.ToString());
sb.Append(",");
sb.Append(item.Quantity.ToString());
sb.Append(")");
arrSQL[i] = sb.ToString();
sb.Remove(0,sb.Length);
}
ExecuteSqls(arrSQL);
return orderId;
}
catch
{
throw new Exception("Order books FAILED!");
}
}
public static bool Deal(int orderId)
{
strSQL = "Update OrdersV Set Status =1 Where Id=" + orderId.ToString();
try
{
ExecuteSql4Ds(strSQL);
strSQL = "Update Book set Sales=Sales+1 Where id in(SELECT b.BookId FROM Orders a INNER JOIN OrderDetails b ON a.Id = b.OrderId AND a.Id = " + orderId.ToString() + ")";
ExecuteSql4Ds(strSQL);
return true;
}
catch
{
throw new Exception("Deal with the order failed!");
}
}
public static DataSet GetOrder(int orderId)
{
strSQL = "SELECT UserId, OrderDate,CASE WHEN Status = '1' THEN 'dealt' ELSE 'on progress' END AS Status FROM Orders Where Id=" + orderId.ToString();
try
{
return ExecuteSql4Ds(strSQL);
}
catch
{
throw new Exception("Get order failed!");
}
}
public static DataSet GetOrders()
{
strSQL = "SELECT * FROM OrdersV Where Status=0 or Status IS NULL";
try
{
return ExecuteSql4Ds(strSQL);
}
catch
{
throw new Exception("Get orders failed!");
}
}
public static DataSet GetOrders(int userId)
{
strSQL = "SELECT Id,UserId, OrderDate,CASE WHEN Status = '1' THEN 'dealt' ELSE 'on progress' END AS Status FROM Orders Where UserId=" + userId.ToString();
try
{
return ExecuteSql4Ds(strSQL);
}
catch
{
throw new Exception("Get order failed!");
}
}
public static DataSet GetDetails(int orderId)
{
strSQL = "Select * from DetailsV Where orderId=" + orderId.ToString();
try
{
return ExecuteSql4Ds(strSQL);
}
catch
{
throw new Exception("Get order details failed!");
}
}
#endregion
}
}