📄 dmovement.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Entities;
using Search;
namespace DAL.SqlServer
{
public class DMovement : IDMovement
{
#region 插入实体操作部份
/// <summary>
/// 插入
/// </summary>
/// <param name="cmd">Command对象</param>
/// <param name="movement">实体类对象</param>
/// <returns>影响的记录行数</returns>
private int Insert(SqlCommand cmd, Movement movement)
{
cmd.Parameters.Clear();
cmd.CommandText = "insert into Movement (Movement_Name,Is_Valid,Movement_Color) values (@Movement_Name,@Is_Valid,@Movement_Color)";
//从实体中取出值放入Command的参数列表
cmd.Parameters.Add(new SqlParameter("@Movement_Name", movement.MovementName));
cmd.Parameters.Add(new SqlParameter("@Is_Valid", movement.IsValid));
cmd.Parameters.Add(new SqlParameter("@Movement_Color", movement.MovementColor));
return cmd.ExecuteNonQuery();
}
/// <summary>
/// 不使用事务的插入方法
/// </summary>
/// <param name="movement">实体类对象</param>
/// <returns>影响的记录行数</returns>
public int Insert(Movement movement)
{
int result = 0;
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
result = Insert(cmd, movement);
}
}
return result;
}
/// <summary>
/// 使用事务的插入方法
/// </summary>
/// <param name="connection">实现共享Command的对象</param>
/// <param name="movement">实体类对象</param>
/// <returns>影响的记录行数</returns>
public int Insert(IConnection connection, Movement movement)
{
SqlCommand cmd = connection.Command as SqlCommand;
return Insert(cmd, movement);
}
#endregion
#region 删除实体操作
/// <summary>
/// 删除
/// </summary>
/// <param name="cmd">Command对象</param>
/// <param name="movement">实体类对象</param>
/// <returns>影响的记录行数</returns>
private int ExcuteDeleteCommand(SqlCommand cmd, Movement movement)
{
cmd.Parameters.Clear();
cmd.CommandText = "delete Movement where Movement_ID=@Movement_ID";
//从实体中取出值放入Command的参数列表
cmd.Parameters.Add(new SqlParameter("@Movement_ID", movement.MovementID));
return cmd.ExecuteNonQuery();
}
/// <summary>
/// 不使用事务的删除方法
/// </summary>
/// <param name="movement">实体类对象</param>
/// <returns>影响的记录行数</returns>
public int Delete(Movement movement)
{
int result = 0;
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
result = ExcuteDeleteCommand(cmd, movement);
}
}
return result;
}
/// <summary>
/// 使用事务的删除方法
/// </summary>
/// <param name="connection">实现共享Command的对象</param>
/// <param name="movement">实体类对象</param>
/// <returns>影响的记录行数</returns>
public int Delete(IConnection connection, Movement movement)
{
int result = 0;
SqlCommand cmd = connection.Command as SqlCommand;
result = ExcuteDeleteCommand(cmd, movement);
return result;
}
/// <summary>
/// 根据查询对象构建过滤条件并使用事务的删除方法
/// </summary>
/// <param name="connection">实现共享Command的对象</param>
/// <param name="movement">实体类对象</param>
/// <returns>影响的记录行数</returns>
public int Delete(IConnection connection, MovementSearcher searcher)
{
int result = 0;
string condition = string.Empty;
SqlCommand cmd = connection.Command as SqlCommand;
cmd.Parameters.Clear();
if (searcher.MovementIDIsValid)
{
condition += " and Movement_ID=@Movement_ID";
cmd.Parameters.Add(new SqlParameter("@Movement_ID", searcher.MovementID));
}
if (searcher.MovementNameIsValid)
{
condition += " and Movement_Name=@Movement_Name";
cmd.Parameters.Add(new SqlParameter("@Movement_Name", searcher.MovementName));
}
if (searcher.IsValidIsValid)
{
condition += " and Is_Valid=@Is_Valid";
cmd.Parameters.Add(new SqlParameter("@Is_Valid", searcher.IsValid));
}
if (searcher.MovementColorIsValid)
{
condition += " and Movement_Color=@Movement_Color";
cmd.Parameters.Add(new SqlParameter("@Movement_Color", searcher.MovementColor));
}
if (condition != string.Empty)
{
condition = " where " + condition;
}
cmd.CommandText = "delete from Movement " + condition;
result = cmd.ExecuteNonQuery();
return result;
}
/// <summary>
/// 根据查询对象构建过滤条件并不使用事务的删除方法
/// </summary>
/// <param name="searcher">查询对象</param>
/// <returns>影响的记录行数</returns>
public int Delete(MovementSearcher searcher)
{
int result = 0;
string condition = string.Empty;
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
if (searcher.MovementIDIsValid)
{
condition += " and Movement_ID=@Movement_ID";
cmd.Parameters.Add(new SqlParameter("@Movement_ID", searcher.MovementID));
}
if (searcher.MovementNameIsValid)
{
condition += " and Movement_Name=@Movement_Name";
cmd.Parameters.Add(new SqlParameter("@Movement_Name", searcher.MovementName));
}
if (searcher.IsValidIsValid)
{
condition += " and Is_Valid=@Is_Valid";
cmd.Parameters.Add(new SqlParameter("@Is_Valid", searcher.IsValid));
}
if (searcher.MovementColorIsValid)
{
condition += " and Movement_Color=@Movement_Color";
cmd.Parameters.Add(new SqlParameter("@Movement_Color", searcher.MovementColor));
}
if (condition != string.Empty)
{
condition = " where " + condition;
}
cmd.CommandText = "delete from Movement " + condition;
result = cmd.ExecuteNonQuery();
}
}
return result;
}
#endregion
#region 更新实体操作
/// <summary>
/// 更新
/// </summary>
/// <param name="cmd">Command对象</param>
/// <param name="movement">实体类对象</param>
/// <returns>影响的记录行数</returns>
private int ExcuteUpdateCommand(SqlCommand cmd, Movement movement)
{
cmd.CommandText = "update Movement set Movement_Name=@Movement_Name,Is_Valid=@Is_Valid,Movement_Color=@Movement_Color where Movement_ID=@Movement_ID";
//从实体中取出值放入Command的参数列表
cmd.Parameters.Add(new SqlParameter("@Movement_Name", movement.MovementName));
cmd.Parameters.Add(new SqlParameter("@Is_Valid", movement.IsValid));
cmd.Parameters.Add(new SqlParameter("@Movement_Color", movement.MovementColor));
cmd.Parameters.Add(new SqlParameter("@Movement_ID", movement.MovementID));
return cmd.ExecuteNonQuery();
}
/// <summary>
/// 不使用事务的更新方法
/// </summary>
/// <param name="movement">实体类对象</param>
/// <returns>影响的记录行数</returns>
public int Update(Movement movement)
{
int result = 0;
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
result = ExcuteUpdateCommand(cmd, movement);
}
}
return result;
}
/// <summary>
/// 使用事务的更新方法
/// </summary>
/// <param name="connection">实现共享Command的对象</param>
/// <param name="movement">实体类对象</param>
/// <returns>影响的记录行数</returns>
public int Update(IConnection connection, Movement movement)
{
int result = 0;
SqlCommand cmd = connection.Command as SqlCommand;
result = ExcuteUpdateCommand(cmd, movement);
return result;
}
/// <summary>
/// 根据查询对象构建过滤条件并不使用事务的更新方法
/// </summary>
/// <param name="searcher">查询对象</param>
/// <param name="newValues">要更新的新值</param>
/// <returns>影响的记录行数</returns>
public int Update(MovementSearcher searcher, MovementSearcher newValues)
{
int result = 0;
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//string updateString=GetUpdateString(cmd, newValues)+condition;
string updateString = string.Empty;
string condition = string.Empty;
if (newValues.MovementIDIsValid)
{
updateString += ",Movement_ID=@Movement_ID_V";
cmd.Parameters.Add(new SqlParameter("@Movement_ID_V", newValues.MovementID));
}
if (newValues.MovementNameIsValid)
{
updateString += ",Movement_Name=@Movement_Name_V";
cmd.Parameters.Add(new SqlParameter("@Movement_Name_V", newValues.MovementName));
}
if (newValues.IsValidIsValid)
{
updateString += ",Is_Valid=@Is_Valid_V";
cmd.Parameters.Add(new SqlParameter("@Is_Valid_V", newValues.IsValid));
}
if (newValues.MovementColorIsValid)
{
updateString += ",Movement_Color=@Movement_Color_V";
cmd.Parameters.Add(new SqlParameter("@Movement_Color_V", newValues.MovementColor));
}
if (searcher.MovementIDIsValid)
{
condition += " and Movement_ID=@Movement_ID_C";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -