///////////////////////////////////////////////////////////////////////////////////////
//
File: CustomersDAL.cs
//
Description: the CustomersDAL Created By CodeSmith .
//
---------------------
//
Copyright: @ 2010-1-5 10:08:50
//
---------------------
//
History:
///////////////////////////////////////////////////////////////////////////////////////
using
System;
using
Chhuic.Model.CustomersModel;
namespace
Chhuic.Dal.CustomersDAL
{
///
<summary>
///
Summary description for CustomersModel.
///
Description:
///
</summary>
public
class
CustomersDAL
{
#region
Constructor
///
<summary>
///
CustomersDAL Constructor
///
</summary>
public
CustomersDAL()
{
}
#endregion
#region
GetMaxID
///
<summary>
///
get the max PrimaryKey ID value
///
</summary>
///
<return>
int
<return>
public
int
GetMaxID()
{
string
sql
=
"
select Max(CustomerID)+1 from Customers
"
;
try
{
return
int
.Parse(SqlServerBase.ExecuteScalar(sql).ToString());
}
catch
(Exception ex)
{
}
return
-
1
;
}
#endregion
#region
Exist
///
<summary>
///
check the record is Exist by the CustomerID value
///
</summary>
///
<param name="CustomerID">
CustomerID
</param>
///
<return>
bool
<return>
public
bool
Exist(
int
CustomerID)
{
string
sql
=
"
select Count(*) from Customers where CustomerID=@CustomerID
"
;
try
{
object
result
=
SqlServerBase.ExecuteScalar(sql,
new
SqlParameter[]{
new
SqlParameter(
"
@CustomerID
"
,CustomerID)});
if
(result
==
null
||
int
.Parse(result.ToString())
==
0
)
{
return
false
;
}
return
true
;
}
catch
(Exception ex)
{
}
return
false
;
}
///
<summary>
///
check the record is Exist by the filter value
///
</summary>
///
<param name="filter">
filter
</param>
///
<return>
bool
<return>
public
bool
Exist(
string
filter)
{
string
sql
=
"
select Count(*) from Customers
"
;
if
(
!
string
.IsNullOrEmpty(filter))
{
sql
+=
"
where
"
+
filter;
}
try
{
object
result
=
SqlServerBase.ExecuteScalar(sql);
if
(result
==
null
||
int
.Parse(result.ToString())
==
0
)
{
return
false
;
}
return
true
;
}
catch
(Exception ex)
{
}
return
false
;
}
#endregion
#region
Add
///
<summary>
///
Add
///
</summary>
///
<param name="CustomersModel">
CustomersModel
</param>
///
<return>
int
<return>
public
int
Add(Model.CustomersModel.CustomersModel model)
{
string
sql
=
"
insert into Customers(CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax) values(@CompanyName,@ContactName,@ContactTitle,@Address,@City,@Region,@PostalCode,@Country,@Phone,@Fax);select @@IDENTITY;
"
;
SqlParameter[] parameters
=
{
new
SqlParameter(
"
@CompanyName
"
,DbType.String),
new
SqlParameter(
"
@ContactName
"
,DbType.String),
new
SqlParameter(
"
@ContactTitle
"
,DbType.String),
new
SqlParameter(
"
@Address
"
,DbType.String),
new
SqlParameter(
"
@City
"
,DbType.String),
new
SqlParameter(
"
@Region
"
,DbType.String),
new
SqlParameter(
"
@PostalCode
"
,DbType.String),
new
SqlParameter(
"
@Country
"
,DbType.String),
new
SqlParameter(
"
@Phone
"
,DbType.String),
new
SqlParameter(
"
@Fax
"
,DbType.String)
};
parameters[
0
].Value
=
model.CompanyName;
parameters[
1
].Value
=
model.ContactName;
parameters[
2
].Value
=
model.ContactTitle;
parameters[
3
].Value
=
model.Address;
parameters[
4
].Value
=
model.City;
parameters[
5
].Value
=
model.Region;
parameters[
6
].Value
=
model.PostalCode;
parameters[
7
].Value
=
model.Country;
parameters[
8
].Value
=
model.Phone;
parameters[
9
].Value
=
model.Fax;
try
{
int
result
=
int
.Parse(SqlServerBase.ExecuteScalar(sql,parameters).ToString());
return
result;
}
catch
(Exception ex)
{
}
return
-
1
;
}
#endregion
#region
Update
///
<Summary>
///
Update Customers table by Model.CustomersModel.CustomersModel
///
<Summary>
///
<param name="CustomersModel">
CustomersModel
</param>
///
<return>
int
</return>
public
int
Update(Model.CustomersModel.CustomersModel model)
{
string
sql
=
"
update Customers set CompanyName=@CompanyName,ContactName=@ContactName,ContactTitle=@ContactTitle,Address=@Address,City=@City,Region=@Region,PostalCode=@PostalCode,Country=@Country,Phone=@Phone,Fax=@Fax where CustomerID=
"
+
model.CustomerID;
SqlParameter[] parameters
=
{
new
SqlParameter(
"
@CompanyName
"
,DbType.String),
new
SqlParameter(
"
@ContactName
"
,DbType.String),
new
SqlParameter(
"
@ContactTitle
"
,DbType.String),
new
SqlParameter(
"
@Address
"
,DbType.String),
new
SqlParameter(
"
@City
"
,DbType.String),
new
SqlParameter(
"
@Region
"
,DbType.String),
new
SqlParameter(
"
@PostalCode
"
,DbType.String),
new
SqlParameter(
"
@Country
"
,DbType.String),
new
SqlParameter(
"
@Phone
"
,DbType.String),
new
SqlParameter(
"
@Fax
"
,DbType.String)
};
parameters[
0
].Value
=
model.CompanyName;
parameters[
1
].Value
=
model.ContactName;
parameters[
2
].Value
=
model.ContactTitle;
parameters[
3
].Value
=
model.Address;
parameters[
4
].Value
=
model.City;
parameters[
5
].Value
=
model.Region;
parameters[
6
].Value
=
model.PostalCode;
parameters[
7
].Value
=
model.Country;
parameters[
8
].Value
=
model.Phone;
parameters[
9
].Value
=
model.Fax;
try
{
int
result
=
int
.Parse(SqlServerBase.ExecuteNonQuery(sql,parameters).ToString());
return
result;
}
catch
(Exception ex)
{
}
return
-
1
;
}
///
<Summary>
///
Update the Customers table by the CustomerID value
///
<Summary>
///
<param name="CustomersModel">
CustomersModel
</param>
///
<param name="CustomerID">
CustomerID
</param>
///
<return>
int
</return>
public
int
Update(Model.CustomersModel.CustomersModel model,
int
CustomerID)
{
string
sql
=
"
update Customers set CompanyName=@CompanyName,ContactName=@ContactName,ContactTitle=@ContactTitle,Address=@Address,City=@City,Region=@Region,PostalCode=@PostalCode,Country=@Country,Phone=@Phone,Fax=@Fax where CustomerID=
"
+
CustomerID ;
SqlParameter[] parameters
=
{
new
SqlParameter(
"
@CompanyName
"
,DbType.String),
new
SqlParameter(
"
@ContactName
"
,DbType.String),
new
SqlParameter(
"
@ContactTitle
"
,DbType.String),
new
SqlParameter(
"
@Address
"
,DbType.String),
new
SqlParameter(
"
@City
"
,DbType.String),
new
SqlParameter(
"
@Region
"
,DbType.String),
new
SqlParameter(
"
@PostalCode
"
,DbType.String),
new
SqlParameter(
"
@Country
"
,DbType.String),
new
SqlParameter(
"
@Phone
"
,DbType.String),
new
SqlParameter(
"
@Fax
"
,DbType.String)
};
parameters[
0
].Value
=
model.CompanyName;
parameters[
1
].Value
=
model.ContactName;
parameters[
2
].Value
=
model.ContactTitle;
parameters[
3
].Value
=
model.Address;
parameters[
4
].Value
=
model.City;
parameters[
5
].Value
=
model.Region;
parameters[
6
].Value
=
model.PostalCode;
parameters[
7
].Value
=
model.Country;
parameters[
8
].Value
=
model.Phone;
parameters[
9
].Value
=
model.Fax;
try
{
int
result
=
int
.Parse(SqlServerBase.ExecuteNonQuery(sql,parameters).ToString());
return
result;
}
catch
(Exception ex)
{
}
return
-
1
;
}
///
<summary>
///
Update Customers table by the filter value
///
</summary>
///
<param name="CustomersModel">
CustomersModel
</param>
///
<param name="filter">
filter
</param>
///
<return>
int
</return>
public
int
Update(Model.CustomersModel.CustomersModel model,
string
filter)
{
string
sql
=
"
update Customers set CompanyName=@CompanyName,ContactName=@ContactName,ContactTitle=@ContactTitle,Address=@Address,City=@City,Region=@Region,PostalCode=@PostalCode,Country=@Country,Phone=@Phone,Fax=@Fax where
"
+
filter;
SqlParameter[] parameters
=
{
new
SqlParameter(
"
@CompanyName
"
,DbType.String),
new
SqlParameter(
"
@ContactName
"
,DbType.String),
new
SqlParameter(
"
@ContactTitle
"
,DbType.String),
new
SqlParameter(
"
@Address
"
,DbType.String),
new
SqlParameter(
"
@City
"
,DbType.String),
new
SqlParameter(
"
@Region
"
,DbType.String),
new
SqlParameter(
"
@PostalCode
"
,DbType.String),
new
SqlParameter(
"
@Country
"
,DbType.String),
new
SqlParameter(
"
@Phone
"
,DbType.String),
new
SqlParameter(
"
@Fax
"
,DbType.String)
};
parameters[
0
].Value
=
model.CompanyName;
parameters[
1
].Value
=
model.ContactName;
parameters[
2
].Value
=
model.ContactTitle;
parameters[
3
].Value
=
model.Address;
parameters[
4
].Value
=
model.City;
parameters[
5
].Value
=
model.Region;
parameters[
6
].Value
=
model.PostalCode;
parameters[
7
].Value
=
model.Country;
parameters[
8
].Value
=
model.Phone;
parameters[
9
].Value
=
model.Fax;
try
{
int
result
=
int
.Parse(SqlServerBase.ExecuteNonQuery(sql,parameters).ToString());
return
result;
}
catch
(Exception ex)
{
}
return
-
1
;
}
#endregion
#region
Delete
///
<summary>
///
Delete the Customers all record.
///
</summary>
///
<return>
int
<return>
public
int
Delete()
{
string
sql
=
"
delete from Customers
"
;
try
{
return
SqlServerBase.ExecuteNonQuery(sql);
}
catch
(Exception ex)
{
}
return
-
1
;
}
///
<summary>
///
Delete the Customers table record by the CustomerID value
///
</summary>
///
<param name="CustomerID">
CustomerID
</param>
///
<return>
int
<return>
public
int
Delete(
int
CustomerID)
{
string
sql
=
"
delete from Customers where CustomerID=
"
+
CustomerID;
try
{
return
SqlServerBase.ExecuteNonQuery(sql);
}
catch
(Exception ex)
{
}
return
-
1
;
}
///
<summary>
///
Delete the Customers table record by the filter value
///
</summary>
///
<param name="filter">
filter
</param>
///
<return>
int
<return>
public
int
Delete(
string
filter)
{
string
sql
=
"
delete from Customers where
"
+
filter;
try
{
return
SqlServerBase.ExecuteNonQuery(sql);
}
catch
(Exception ex)
{
}
return
-
1
;
}
#endregion
#region
DataSet
///
<summary>
///
get DataSet records
///
</summary>
///
<return>
DataSet
<return>
public
DataSet GetList()
{
string
sql
=
"
select * from Customers
"
;
try
{
return
SqlServerBase.ExecuteDataset(sql);
}
catch
(Exception ex)
{
}
return
null
;
}
///
<summary>
///
get DataSet records by CustomerID value
///
</summary>
///
<param name="CustomerID">
CustomerID
</param>
///
<return>
DataSet
<return>
public
DataSet GetList(
int
CustomerID)
{
string
sql
=
"
select * from Customers where CustomerID=
"
+
CustomerID;
try
{
return
SqlServerBase.ExecuteDataset(sql);
}
catch
(Exception ex)
{
}
return
null
;
}
///
<summary>
///
get DataSet records by the filter value
///
</summary>
///
<param name="filter">
filter
</param>
///
<return>
DataSet
<return>
public
DataSet GetList(
string
filter)
{
string
sql
=
"
select * from Customers where
"
+
filter;
try
{
return
SqlServerBase.ExecuteDataset(sql);
}
catch
(Exception ex)
{
}
return
null
;
}
#endregion
#region
Query
///
<summary>
///
Query by the sql
///
</summary>
///
<param name="sql">
sql
</param>
///
<return>
DataSet
<return>
public
DataSet Query(
string
sql)
{
if
(
string
.IsNullOrEmpty(sql))
{
return
null
;
}
try
{
return
SqlServerBase.ExecuteDataset(sql);
}
catch
{
}
return
null
;
}
#endregion
#region
Model.CustomersModel.CustomersModel
///
<summary>
///
get Model.CustomersModel.CustomersModel by the CustomerID value
///
</summary>
///
<param name="CustomerID">
CustomerID
</param>
///
<return>
CustomersModel
<return>
public
Model.CustomersModel.CustomersModel GetModel(
int
CustomerID)
{
try
{
DataSet ds
=
GetList(CustomerID);
if
(ds
==
null
||
ds.Tables.Count
==
0
||
ds.Tables[
0
].Rows.Count
==
0
)
{
return
null
;
}
return
GetModel(ds.Tables[
0
].Rows[
0
]);
}
catch
{
}
return
null
;
}
///
<summary>
///
get Model.CustomersModel.CustomersModels
///
</summary>
///
<return>
CustomersModel
<return>
public
Model.CustomersModel.CustomersModel[] GetModels()
{
try
{
DataSet ds
=
GetList();
if
(ds
==
null
||
ds.Tables.Count
==
0
||
ds.Tables[
0
].Rows.Count
==
0
)
{
return
null
;
}
return
GetModels(ds.Tables[
0
]);
}
catch
{
}
return
null
;
}
///
<summary>
///
get Model.CustomersModel.CustomersModel by the filter value
///
</summary>
///
<param name="filter">
filter
</param>
///
<return>
CustomersModel
<return>
public
Model.CustomersModel.CustomersModel GetModels(
string
filter)
{
try
{
DataSet ds
=
GetList(filter);
if
(ds
==
null
||
ds.Tables.Count
==
0
||
ds.Tables[
0
].Rows.Count
==
0
)
{
return
null
;
}
return
GetModel(ds.Tables[
0
].Rows[
0
]);
}
catch
{
}
return
null
;
}
///
<summary>
///
get Model.CustomersModel.CustomersModel by the datarow value
///
</summary>
///
<param name="dr">
dr
</param>
///
<return>
CustomersModel
<return>
public
Model.CustomersModel.CustomersModel GetModel(DataRow dr)
{
if
(dr
==
null
)
{
return
null
;
}
Model.CustomersModel.CustomersModel model
=
new
Model.CustomersModel.CustomersModel();
if
(
!
string
.IsNullOrEmpty(dr[
"
CustomerID
"
].ToString()))
{
model.CustomerID
=
dr[
"
CustomerID
"
].ToString();
}
if
(
!
string
.IsNullOrEmpty(dr[
"
CompanyName
"
].ToString()))
{
model.CompanyName
=
dr[
"
CompanyName
"
].ToString();
}
if
(
!
string
.IsNullOrEmpty(dr[
"
ContactName
"
].ToString()))
{
model.ContactName
=
dr[
"
ContactName
"
].ToString();
}
if
(
!
string
.IsNullOrEmpty(dr[
"
ContactTitle
"
].ToString()))
{
model.ContactTitle
=
dr[
"
ContactTitle
"
].ToString();
}
if
(
!
string
.IsNullOrEmpty(dr[
"
Address
"
].ToString()))
{
model.Address
=
dr[
"
Address
"
].ToString();
}
if
(
!
string
.IsNullOrEmpty(dr[
"
City
"
].ToString()))
{
model.City
=
dr[
"
City
"
].ToString();
}
if
(
!
string
.IsNullOrEmpty(dr[
"
Region
"
].ToString()))
{
model.Region
=
dr[
"
Region
"
].ToString();
}
if
(
!
string
.IsNullOrEmpty(dr[
"
PostalCode
"
].ToString()))
{
model.PostalCode
=
dr[
"
PostalCode
"
].ToString();
}
if
(
!
string
.IsNullOrEmpty(dr[
"
Country
"
].ToString()))
{
model.Country
=
dr[
"
Country
"
].ToString();
}
if
(
!
string
.IsNullOrEmpty(dr[
"
Phone
"
].ToString()))
{
model.Phone
=
dr[
"
Phone
"
].ToString();
}
if
(
!
string
.IsNullOrEmpty(dr[
"
Fax
"
].ToString()))
{
model.Fax
=
dr[
"
Fax
"
].ToString();
}
return
model;
}
///
<summary>
///
get Model.CustomersModel.CustomersModel by the datatable records
///
</summary>
///
<param name="dt">
dt
</param>
///
<return>
CustomersModel
<return>
public
Model.CustomersModel.CustomersModel[] GetModels(DataTable dt)
{
int
i
=
0
;
Model.CustomersModel.CustomersModel[] models
=
new
Model.CustomersModel.CustomersModel[dt.Rows.Count];
foreach
(DataRow dr
in
dt.Rows)
{
models[i]
=
GetModel(dr);
i
++
;
}
return
models;
}
#endregion
}
}