Linq to sql直接执行sql语句(转)

查询:

返回一个值

            string strSql = "SELECT COUNT(0) FROM Person(NOLOCK)";

            var query = dataContext.ExecuteQuery<int>(strSql);

            int result = query.First<int>();

            Console.WriteLine();

            Console.WriteLine("total count:{0}", result);

            return result;

返回集合

                string sql = "select * from WorkGroups";

                var collection =     s_DataContext.ExecuteQuery<Trendware.EMS.DataLinq.Sys.WorkGroups>(sql);

                return collection.ToList();

增删改

            string strSql = "UPDATE Person SET Weight={0} WHERE Id={1}";

            int result = dataContext.ExecuteCommand(strSql, new object[] { destWeight, id });

            Console.WriteLine();

            Console.WriteLine("affect num:{0}", result);

            return result;

posted @ 2012-07-12 17:56  zhcnblog  阅读(3788)  评论(0编辑  收藏  举报