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

时间:2022-04-16 20:54:23

查询:

返回一个值

            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;