[英]Using Visual Studio query builder to filter data
Currently I have two tables created in MS SQL Server( postings
table and user
table) outputting to visual studio 2012 to a grid view and details view. 目前,我在MS SQL Server中创建了两个表(
postings
表和user
表),输出到Visual Studio 2012的网格视图和详细信息视图。 I am trying to figure a way to ensure that when the details view is paged into a certain user (there is only two users) then the only the postings of made by that user in the past will show in the gridview. 我正在尝试一种方法,以确保将详细信息视图分页到某个用户(只有两个用户)时,该用户过去所做的仅有的帖子将显示在gridview中。
The postings
table has an ID
(primary key) that is in two parts with the first part being a unique identifier of that poster with the second part being a unique identifier of that POST. postings
表的ID
(主键)分为两部分,第一部分是该发布者的唯一标识符,第二部分是该POST的唯一标识符。 For Poster1 has ID of 45678987654_98765456789, 45678987654_234565432123, 45678987654_999999999 etc and Poster 2 has the id 98765678987_987654866, 98765678987_2828288282, 98765678987_1111111111 all up about 700 entries. 对于海报1的ID为45678987654_98765456789、45678987654_234565432123、45678987654_999999999等,而海报2的ID为98765678987_987654866、98765678987_2828288282、98765678987_1111111111总计约700个条目。
In the User table there is a unique ID as well that is the same for that Poster eg. 在“用户”表中,还有一个唯一的ID,与该“海报”相同。 Poster1 has id 45678987654_98765456789 and Poster2 has 98765678987_987654866.
Poster1的ID为45678987654_98765456789,Poster2的为98765678987_987654866。 I tried to filter in query builder in visual studio by =, Like% and CONTAINS because in theory if the first digits of the ID for both POST and USER were analysed and compared then the specifications could be achieved.
我尝试在Visual Studio的查询生成器中按=,Like%和CONTAINS进行过滤,因为从理论上讲,如果对POST和USER的ID的第一位数字进行了分析和比较,则可以实现规范。 As the current ID in the details view could be compared to all the gridview IDs and only those that are alike and share the first 15 or so digits would then be accepted onto the details view.
由于详细信息视图中的当前ID可以与所有gridview ID进行比较,因此只有相似且共享前15个左右数字的ID才会被接受到详细信息视图中。
However all of the ways I tried to do this in query builder using Post_Id = table_Id
or like/contains formats didn't work. 但是,我尝试使用
Post_Id = table_Id
或点Post_Id = table_Id
/包含格式在查询生成器中执行此操作的所有方法均无效。 If there is another way to do it that or if there is a slight modification to the current method then that would be great to hear about. 如果有另一种方法可以做到这一点,或者对当前方法进行了微小的修改,那真是太好了。
The first thing you need to do is to change the postings table. 您需要做的第一件事就是更改发布表。 Where you have concatenated two values to make the PK, split it out into two columns, the first containing the userId, and the second the unique id of the post that you've generated.
在您将两个值连接起来以构成PK的位置,将其分成两列,第一列包含userId,第二列包含您生成的帖子的唯一ID。
Having done that, set the PK to be the combination of the two, and tell the db that the userId in postings is an FK referencing the PK of User table. 完成此操作后,将PK设置为两者的组合,并告诉db,发布中的userId是引用User表PK的FK。
Once this is done, the data will be in a format that VS can probably understand better, and the db design will certainly be improved. 完成此操作后,数据将采用VS可能会更好理解的格式,并且数据库设计肯定会得到改善。
EDIT 编辑
SQL Code to create the FK: 创建FK的SQL代码:
ALTER TABLE postings WITH CHECK ADD CONSTRAINT [FK_postings_user] FOREIGN KEY([userId])
REFERENCES user ([userId])
GO
ALTER TABLE postings CHECK CONSTRAINT [FK_postings_user]
GO
If there are values in postings.UserId which have no matching value in user.UserId then this will fail. 如果postings.UserId中的值与user.UserId中的值不匹配,则此操作将失败。 To check, run something like:
要进行检查,请运行以下命令:
select * from postings p left outer join user u on u.UserId = p.UserId where u.UserId is null 从发布中选择* * u.UserId = p.UserId上的左外部加入用户u,其中u.UserId为null
This will give a list of posting rows where there is no matching user row - ones where your data conversion maybe had a problem? 这将提供一个列表行的列表,其中没有匹配的用户行-您的数据转换可能存在问题的行? Or there was 'orphaned' data there already.
或者那里已经有“孤立的”数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.