C#中的LINQ to SQL - 动态选择表 [英] C# linq to sql - selecting tables dynamically

查看:447
本文介绍了C#中的LINQ to SQL - 动态选择表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情形:有,每年产生一个新的logTable的数据库。它开始于2001年,目前有11个表。它们都具有相同的结构,因此相同的字段,索引,PK的,等

I have the following scenario: there are a database that generates a new logTable every year. It started on 2001 and now has 11 tables. They all have the same structure, thus the same fields, indexes,pk's, etc.

我有些类叫经理们 - 如它的名字说 - 对管理的每个操作这个数据库。对于每一个不同的表,我有一个经理,除了这logTable,我只有一个经理。

I have some classes called managers that - as the name says - manages every operation on this DB. For each different table i have a manager, except for this logTable which i have only one manager.

我已经读了很多,并试图像使用ITable得到不同的东西动态表或我的所有表实现的接口。不幸的是,我失去了强类型的属性,并与我不能做任何搜索或更新或任何东西,因为我不能用 logTable.Where(Q => q.ID == paramId)

I've read a lot and tried different things like using ITable to get tables dynamically or an interface that all my tables implements. Unfortunately, i lose strong-typed properties and with that i can't do any searches or updates or anything, since i can't use logTable.Where(q=> q.ID == paramId).

考虑到这些表具有相同的结构,即从2010年搜索日志可以是一个确切,从2011年和搜索日志查询上。

Considering that those tables have the same structure, a query that searches logs from 2010 can be the exact one that searches logs from 2011 and on.

我只要求这一点,因为我不希望重写相同的代码为每个表,因为他们是在它的结构相同。

I'm only asking this because i wouldn't like to rewrite the same code for each table, since they are equal on it's structure.

修改

我使用LINQ to SQL中我的ORM。而且这些表使用所有DB操作,而不仅仅是选择。

I'm using Linq to SQL as my ORM. And these tables uses all DB operations, not just select.

推荐答案

只要每个查询都返回相同的形状,你可以使用的executeQuery<日志与GT(选择COLS从LogTable+实例)。要知道,是的executeQuery一种情况的LINQ to SQL使得SQL注入。我讨论如何在的 http://www.thinqlinq.com/Post.aspx/Title/Does-LINQ-to-SQL-eliminate-the-possibility-of-SQL-Injection

As long as each of your queries return the same shape, you can use ExecuteQuery<Log>("Select cols From LogTable" + instance). Just be aware that ExecuteQuery is one case where LINQ to SQL allows for SQL Injection. I discuss how to parameterize ExecuteQuery at http://www.thinqlinq.com/Post.aspx/Title/Does-LINQ-to-SQL-eliminate-the-possibility-of-SQL-Injection.

这篇关于C#中的LINQ to SQL - 动态选择表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆