Silverlight 4:运行返回字符串的存储过程

发布于 2024-09-15 20:29:11 字数 1623 浏览 1 评论 0原文

我在 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();
        }

数据模型(选择存储过程时): alt text

存储过程:

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) :
alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-09-22 20:29:11

您返回 null 是什么意思?

首先,您的函数 GetCityByZip 返回一个 string,您正在对其执行 .Value ,但该字符串无效。

如果您从存储过程中获取 null

return ObjectContext.sp_GetCityByZip(pZip).ToString();

将会失败,因为您将尝试对 null 对象执行 ToString()

什么是ObjectContext?您使用 Linq To SQL 吗?如果是这样,您将返回结果集,该结果集应该包含所有数据的包装器,因此您可以执行以下操作:

ObjectContext.sp_GetCityByZip(pZip).City;

然后您的另一个调用将是:

txtCity.Text = _ZipcodesDomainContext.GetCityByZip(42071);

如果您将其清除一点,我也许能够找出确切的问题是,但在目前的状态下,你的问题让我感到困惑。

What do you mean you are getting null back?

Firstly, your function GetCityByZip returns a string which you are doing a .Value on which isn't valid.

If your getting null back from the stored proc:

return ObjectContext.sp_GetCityByZip(pZip).ToString();

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:

ObjectContext.sp_GetCityByZip(pZip).City;

And then your other call would just be:

txtCity.Text = _ZipcodesDomainContext.GetCityByZip(42071);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文