/////////publicstaticintDelete(thisTabletable,Expres" />

Linq To SQL Update Delete

系统 1475 0
      
        public
      
      
        static
      
      
        class
      
       StaticClass
      
{
/// <summary>
/// 批量删除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static int Delete<T>( this Table<T> table, Expression<Func<T, bool >> predicate) where T : class
{
string tableName = table.Context.Mapping.GetTable( typeof (T)).TableName;
DbCommand command=table.Context.GetCommand(table.Where(predicate));
string sqlCondition = command.CommandText;
sqlCondition = sqlCondition.Substring(sqlCondition.LastIndexOf( " WHERE " , StringComparison.InvariantCultureIgnoreCase) + 6 );
// 去掉表中出现的 t0
sqlCondition = sqlCondition.Replace( " [t0]. " , "" );
string commandText = string .Format( " Delete FROM {0} WHERE {1} " , tableName , sqlCondition);
command.CommandText = commandText;


// 执行
try
{
if (command.Connection.State != ConnectionState.Open)
{
command.Connection.Open();
}
return command.ExecuteNonQuery();
}
finally
{
command.Connection.Close();
command.Dispose();
}

}

/// <summary>
/// 批量更新
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <param name="predicate"> 查询条件表达式 </param>
/// <param name="updater"> 更新表达式 </param>
/// <returns> 影响的行数 </returns>
public static int Update<T>( this Table<T> table, Expression<Func<T, bool >> predicate, Expression<Func<T, T>> updateExp) where T : class
{
string tableName = table.Context.Mapping.GetTable( typeof (T)).TableName;
DbCommand command = table.Context.GetCommand(table.Where(predicate));
string sqlCondition = command.CommandText;
sqlCondition = sqlCondition.Substring(sqlCondition.LastIndexOf( " WHERE " , StringComparison.InvariantCultureIgnoreCase) + 6 );

// 获取Update的赋值语句
var updateMemberExp = (MemberInitExpression)updateExp.Body;
var updateMemberCollection = updateMemberExp.Bindings.Cast<MemberAssignment>().Select(c =>
{
var p = command.CreateParameter();
p.ParameterName = c.Member.Name;
p.Value = ((ConstantExpression)c.Expression).Value;
return p;
}).ToArray();

string sqlUpdateBlock = string .Join( " , " , updateMemberCollection.Select(c => string .Format( " [{0}]=@{0} " , c.ParameterName)).ToArray());

// 组合SQL 语句
string commandText = string .Format( " UPDATE {0} SET {1} FROM {0} AS t0 WHERE {2} " , tableName, sqlUpdateBlock, sqlCondition);

// 获取参数数组
command.Parameters.AddRange(updateMemberCollection);
command.CommandText = commandText;
// 执行
try
{
if (command.Connection.State != ConnectionState.Open)
{
command.Connection.Open();
}
return command.ExecuteNonQuery();
}
finally
{
command.Connection.Close();
command.Dispose();
}
}
}

// 查询
          DataClasses1DataContext dbo = new DataClasses1DataContext();
            Table<K_Menu> tb = dbo.GetTable<K_Menu>();
            List<K_MenuCopy> list;//= new List<K_Menu>();
            using (DbConnection dbCon = tb.Context.Connection)
            {
                var query1 = tb.Select(a => new { a.BigImage, a.Image, a.MenuID });
                DbCommand dbCommand = tb.Context.GetCommand(query1);
                dbCon.Open();
                using (DbDataReader dr = dbCommand.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    list = dbo.Translate<K_MenuCopy>(dr).ToList();
                }

            }

 

Linq To SQL Update Delete


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论