Silverlight 4:运行返回字符串的存储过程
我在 Silverlight 4/RIA 中执行存储过程时遇到问题。我得到的唯一值是 null。我的客户端和服务器端代码是否错误?
客户端:
public ZipCodesDomainContext _ZipcodesDomainContext = new ZipCodesDomainContext();
/// <summary>
/// Creates a new <see cref="MainPage"/> instance.
/// </summary>
public MainPage()
{
InitializeComponent();
this.loginContainer.Child = new LoginStatus();
LoadCity();
}
private void LoadCity()
{
txtCity.Text = _ZipcodesDomainContext.GetCityByZip(42071).Value;
}
域服务:
public string GetCityByZip(int pZip)
{
return ObjectContext.sp_GetCityByZip(pZip).ToString();
}
数据模型(选择存储过程时):
存储过程:
USE [ZIPCODES]
GO
/****** Object: StoredProcedure [dbo].[sp_GetCityByZip] Script Date: 08/23/2010 13:48:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_GetCityByZip]
@ZIP int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT City FROM ZipCodes WHERE Zip = @ZIP
END
I have a problem executing a stored procedure in Silverlight 4/RIA. The only value I get back is null. Am I doing my client and server side code wrong?
Client Side :
public ZipCodesDomainContext _ZipcodesDomainContext = new ZipCodesDomainContext();
/// <summary>
/// Creates a new <see cref="MainPage"/> instance.
/// </summary>
public MainPage()
{
InitializeComponent();
this.loginContainer.Child = new LoginStatus();
LoadCity();
}
private void LoadCity()
{
txtCity.Text = _ZipcodesDomainContext.GetCityByZip(42071).Value;
}
Domain Service :
public string GetCityByZip(int pZip)
{
return ObjectContext.sp_GetCityByZip(pZip).ToString();
}
Data Model (When selecting the stored proc) :
Stored Procedure :
USE [ZIPCODES]
GO
/****** Object: StoredProcedure [dbo].[sp_GetCityByZip] Script Date: 08/23/2010 13:48:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_GetCityByZip]
@ZIP int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT City FROM ZipCodes WHERE Zip = @ZIP
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您返回
null
是什么意思?首先,您的函数
GetCityByZip
返回一个string
,您正在对其执行.Value
,但该字符串无效。如果您从存储过程中获取
null
将会失败,因为您将尝试对 null 对象执行
ToString()
。什么是ObjectContext?您使用 Linq To SQL 吗?如果是这样,您将返回结果集,该结果集应该包含所有数据的包装器,因此您可以执行以下操作:
然后您的另一个调用将是:
如果您将其清除一点,我也许能够找出确切的问题是,但在目前的状态下,你的问题让我感到困惑。
What do you mean you are getting
null
back?Firstly, your function
GetCityByZip
returns astring
which you are doing a.Value
on which isn't valid.If your getting
null
back from the stored proc:would fail because you would be trying to do a
ToString()
on a null object.What is
ObjectContext
? Are you using Linq To SQL? If so you get back result set which should have a wrapper of all the data so you would do something like:And then your other call would just be:
If you clear it up a bit I might be able to figure out what the exact issue is but in it's current state your question has me confused.