亚音速的内连接问题
Table:Account
AccountID|AccountName|AccountTypeID|IsActive
17 |aaaa |5 |1
18 |bbbb |5 |1
19 |cccc |5 |1
Table:AccountAddress
AddressID|AccountID|CityId
1734 |17 |2721
1823 |18 |2721
1912 |19 |2722
Table: City
CityID|StateProvID|CityName
2721 |28 |ablinne
2728 |27 |aberdeen
Table: StateProv
StateProvID|CountryID|StateProvName
27 |1 |-
28 |2 |-
Table: Country
CountryID|RegionID|CountryName
27 |111 |Algena
28 |112 |Argentina
Table: RegionID
RegionID|RegionName
111 |Africa
112 |Asia
SQL 查询如下
select Account.AccountID,AccountName,CityName,StateProvName,CountryName,RegionName from Account
join AccountAddress on AccountAddress.AccountID=Account.AccountID
join City on City.CityID=AccountAddress.CityID
join StateProv on StateProv.StateProvID=City.StateProvID
join Country on Country.CountryID=StateProv.CountryID
join Region on Region.RegionID=Country.RegionID
where Account.AccountTypeID=5
and Account.IsActive=1
and City.CityID=2721
我想将上面的查询转换为亚音速查询...所以我写如下
DataSet accounts = new Select(
Account.Columns.AccountName,
City.Columns.CityName,
Country.Columns.CountryName,
Region.Columns.RegionName,
StateProv.Columns.StateProvName)
.From(Account.Schema)
.InnerJoin(AccountAddress.Schema)
.InnerJoin(City.Schema)
.InnerJoin(StateProv.Schema)
.InnerJoin(Country.Schema)
.InnerJoin(Region.Schema)
.Where(Account.Columns.AccountTypeID).IsEqualTo(accountTypeId)
.And(Account.Columns.IsActive).IsEqualTo(isActive)
.And(City.CityIDColumn).IsEqualTo(cityId)
.ExecuteDataSet();
它不起作用,我收到错误“对象引用未设置为对象的实例。” 请告诉我如何检索我喜欢的数据?
我使用的是 Subsonic 2.1 版本,它生成如下 sql 查询
SELECT [dbo].[Account].[AccountName], CityName, CountryName, RegionName,StateProvName
FROM [dbo].[Account]
INNER JOIN [dbo].[AccountAddress] ON [dbo].[Account].[AccountID] = [dbo].[AccountAddress].[AccountID]
INNER JOIN [dbo].[City] ON [dbo].[AccountAddress].[CityID] = [dbo].[City].[CityID]
INNER JOIN [dbo].[StateProv] ON [dbo].[City].[StateProvID] = [dbo].[StateProv].[StateProvID]
INNER JOIN [dbo].[Country] ON [dbo].[StateProv].[CountryID] = [dbo].[Country].[CountryID]
INNER JOIN [dbo].[Region] ON [dbo].[Country].[RegionID] = [dbo].[Region].[RegionID]
WHERE [dbo].[Account].[AccountTypeID] = @AccountTypeID0
AND [dbo].[Account].[IsActive] = @IsActive1
AND CityID = @CityID2
Table:Account
AccountID|AccountName|AccountTypeID|IsActive
17 |aaaa |5 |1
18 |bbbb |5 |1
19 |cccc |5 |1
Table:AccountAddress
AddressID|AccountID|CityId
1734 |17 |2721
1823 |18 |2721
1912 |19 |2722
Table: City
CityID|StateProvID|CityName
2721 |28 |ablinne
2728 |27 |aberdeen
Table: StateProv
StateProvID|CountryID|StateProvName
27 |1 |-
28 |2 |-
Table: Country
CountryID|RegionID|CountryName
27 |111 |Algena
28 |112 |Argentina
Table: RegionID
RegionID|RegionName
111 |Africa
112 |Asia
SQL QUERY IS BELOW
select Account.AccountID,AccountName,CityName,StateProvName,CountryName,RegionName from Account
join AccountAddress on AccountAddress.AccountID=Account.AccountID
join City on City.CityID=AccountAddress.CityID
join StateProv on StateProv.StateProvID=City.StateProvID
join Country on Country.CountryID=StateProv.CountryID
join Region on Region.RegionID=Country.RegionID
where Account.AccountTypeID=5
and Account.IsActive=1
and City.CityID=2721
I want to convert above query into subsonic query... so i written as below
DataSet accounts = new Select(
Account.Columns.AccountName,
City.Columns.CityName,
Country.Columns.CountryName,
Region.Columns.RegionName,
StateProv.Columns.StateProvName)
.From(Account.Schema)
.InnerJoin(AccountAddress.Schema)
.InnerJoin(City.Schema)
.InnerJoin(StateProv.Schema)
.InnerJoin(Country.Schema)
.InnerJoin(Region.Schema)
.Where(Account.Columns.AccountTypeID).IsEqualTo(accountTypeId)
.And(Account.Columns.IsActive).IsEqualTo(isActive)
.And(City.CityIDColumn).IsEqualTo(cityId)
.ExecuteDataSet();
it is not working and i received error "Object reference not set to an instance of an object."
Please tell me how to retrive the data as i like?
I am using Subsonic version 2.1 and it generates the sql query as below
SELECT [dbo].[Account].[AccountName], CityName, CountryName, RegionName,StateProvName
FROM [dbo].[Account]
INNER JOIN [dbo].[AccountAddress] ON [dbo].[Account].[AccountID] = [dbo].[AccountAddress].[AccountID]
INNER JOIN [dbo].[City] ON [dbo].[AccountAddress].[CityID] = [dbo].[City].[CityID]
INNER JOIN [dbo].[StateProv] ON [dbo].[City].[StateProvID] = [dbo].[StateProv].[StateProvID]
INNER JOIN [dbo].[Country] ON [dbo].[StateProv].[CountryID] = [dbo].[Country].[CountryID]
INNER JOIN [dbo].[Region] ON [dbo].[Country].[RegionID] = [dbo].[Region].[RegionID]
WHERE [dbo].[Account].[AccountTypeID] = @AccountTypeID0
AND [dbo].[Account].[IsActive] = @IsActive1
AND CityID = @CityID2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其分解为单独的部分,以便您可以找出什么对象为空。
例如:
Break it down into separate sections so you can find out what object is null.
E.g.:
堆栈跟踪会有所帮助。
很奇怪
抛出异常
。您的解决方法:
不起作用,因为 City.Columns.CityId 以字符串形式返回列的名称(没有 Tablename,生成的 sql 如下所示:
并且 sql 服务器不知道您是否要选择 tableA.tableAID 或 tableB .tableAID 因此它会抛出异常(即使在独立的查询工具中),
如果您使用的是结构
而不是
多个表,您应该始终将它们与 Table.Schema.QualifiedName 连接,因为它们只是列的字符串表示。姓名。
A stack trace would help.
It is very strange that
throws an exception.
Your workaround:
does not work because City.Columns.CityId return the name of the column as a string (without the Tablename and the generated sql looks like this:
and the sql server does not know if you want to select tableA.tableAID or tableB.tableAID so the it throws the exception (even in a standalone query tool).
If you are working with the
struct instead of
and multiple tables you should always concatenate them with the Table.Schema.QualifiedName since they are just string represenations of the columns name.