我有很多与以下结构相同的类,我想在它们中实现一个接口。那么,是否有人对此有解决方案?
public static class ClientDAL
{
private static string connString = ConfigurationManager.ConnectionStrings["default"].ToString();
public static ClientEntity NewClient(ClientEntity client)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = connString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "InsertNewClient";
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@ClientID", client.ClientID);
cmd.Parameters.AddWithValue("@ClientName", client.ClientName);
cmd.Parameters.AddWithValue("@ClientNif", client.ClientNIF);
cmd.Parameters.AddWithValue("@ClientAddress", client.ClientAddress);
cmd.Parameters.AddWithValue("@ClientCountry", client.ClientCountry);
cmd.Parameters.AddWithValue("@ClientProvincy", client.ClientProvincy);
cmd.Parameters.AddWithValue("@ClientCity", client.ClientCity);
cmd.Parameters.AddWithValue("@ClientPhone", client.ClientPhone);
cmd.Parameters.AddWithValue("@ClientMobile", client.ClientMobile);
cmd.Parameters.AddWithValue("@ClientFax", client.ClientFax);
cmd.Parameters.AddWithValue("@ClientEmail", client.ClientEmail);
cmd.Parameters.AddWithValue("@ClientInfo", client.ClientInfo);
cmd.ExecuteNonQuery();
}
return client;
}
/// <summary>
///
/// </summary>
/// <param name="clientID"></param>
/// <returns></returns>
public static ClientEntity GetClientByID(Int64 clientID)
{
ClientEntity client=null;
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = connString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SelectClient";
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@ClientID", clientID);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
client=LoadClient(reader);
}
}
return client;
}
/// <summary>
///
/// </summary>
/// <param name="ClientID"></param>
/// <returns></returns>
public static bool Exists(Int64 ClientID)
{
int numberOfRecord = 0;
using (SqlConnection conn=new SqlConnection())
{
conn.ConnectionString = connString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SelectValidClient";
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@ClientID", ClientID);
numberOfRecord = Convert.ToInt32(cmd.ExecuteScalar());
}
return numberOfRecord > 0;
}
public static List<ClientEntity> GetAllClients()
{
List<ClientEntity> lista = new List<ClientEntity>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = connString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SelectAllClient";
cmd.Connection = conn;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
lista.Add(LoadClient(reader));
}
}
return lista;
}
public static ClientEntity UpdateClient(ClientEntity client)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = connString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "UpdateClient";
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@ClientID", client.ClientID);
cmd.Parameters.AddWithValue("@ClientName", client.ClientName);
cmd.Parameters.AddWithValue("@ClientNif", client.ClientNIF);
cmd.Parameters.AddWithValue("@ClientAddress", client.ClientAddress);
cmd.Parameters.AddWithValue("@ClientCountry", client.ClientCountry);
cmd.Parameters.AddWithValue("@ClientProvincy", client.ClientProvincy);
cmd.Parameters.AddWithValue("@ClientCity", client.ClientCity);
cmd.Parameters.AddWithValue("@ClientPhone", client.ClientPhone);
cmd.Parameters.AddWithValue("@ClientMobile", client.ClientMobile);
cmd.Parameters.AddWithValue("@ClientFax", client.ClientFax);
cmd.Parameters.AddWithValue("@ClientEmail", client.ClientEmail);
cmd.Parameters.AddWithValue("@ClientInfo", client.ClientInfo);
cmd.ExecuteNonQuery();
}
return client;
}
private static ClientEntity LoadClient(IDataReader reader)
{
ClientEntity list= new ClientEntity();
list.ClientID =Convert.ToInt64(reader["ClientID"]);
list.ClientName = Convert.ToString(reader["ClientName"]);
list.ClientNIF = Convert.ToInt64(reader["ClientNif"]);
list.ClientAddress = reader["ClientAddress"]==null?string.Empty:Convert.ToString(reader["ClientAddress"]);
list.ClientCountry = reader["ClientCountry"]==null?string.Empty:Convert.ToString(reader["ClientCountry"]);
list.ClientProvincy = reader["ClientProvincy"]==null?string.Empty:Convert.ToString(reader["ClientProvincy"]);
list.ClientCity = reader["ClientCity"] == null ? string.Empty : Convert.ToString(reader["ClientCity"]);
list.ClientPhone = reader["ClientPhone"] == null ? string.Empty : Convert.ToString(reader["ClientPhone"]);
list.ClientMobile = reader["ClientMobile"] == null ? String.Empty : Convert.ToString(reader["ClientMobile"]);
list.ClientFax = reader["ClientFax"] == null ? String.Empty : Convert.ToString(reader["ClientFax"]);
list.ClientEmail = reader["ClientEmail"] == null ? string.Empty : Convert.ToString(reader["ClientEmail"]);
list.ClientInfo = reader["ClientInfo"] == null ? string.Empty : Convert.ToString(reader["ClientInfo"]);
return list;
}
}