如何使用c#通过webservice调用存储过程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1490498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:03:10  来源:igfitidea点击:

How to call a stored procedure thru webservice using c#?

c#wcfweb-servicesstored-procedures

提问by Anuya

I am new to web service,

我是网络服务的新手,

The database access should be through Web Services using ADO.NET to access stored procedures.

数据库访问应该通过 Web Services 使用 ADO.NET 来访问存储过程。

any ideas ?

有任何想法吗 ?

采纳答案by Andrew Hare

Please see The C# Station ADO.NET Tutorial - Lesson 07: Using Stored Procedures:

请参阅C# Station ADO.NET 教程 - 第 7 课:使用存储过程

This lesson shows how to use stored procedures in your data access code. Here are the objectives of this lesson:

  • Learn how to modify the SqlCommand object to use a stored procedure.
  • Understand how to use parameters with stored procedures.

本课展示了如何在数据访问代码中使用存储过程。以下是本课的目标:

  • 了解如何修改 SqlCommand 对象以使用存储过程。
  • 了解如何在存储过程中使用参数。

回答by marc_s

If you start fresh, I would strongly recommend you start using WCF (instead of the old-style ASMX web services).

如果您重新开始,我强烈建议您开始使用 WCF(而不是旧式的 ASMX Web 服务)。

In this case, you'll need:

在这种情况下,您需要:

1) a Service Contract(an interface defining the operation(s) on your web service):

1)服务合同(定义 Web 服务操作的接口):

[ServiceContract]
public interface IMyDataService
{
    [OperationContract]
    YourDataType GetData(int idValue);
}

2) A Data Contractwhich will define the data structures for your calls (here: the return type YourDataType):

2)一个数据契约,它将为您的调用定义数据结构(这里:返回类型YourDataType):

[DataContract]
public class YourDataType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

3) A Service Classthat will actually implement your web service method and make the call to the database using the stored procedure - something like this:

3) 一个服务类,它将实际实现您的 Web 服务方法并使用存储过程调用数据库 - 如下所示:

public class YourDataService : IMyDataService
{
    public YourDataType GetData(int idValue)
    {
        YourDataType result = new YourDataType();

        using(SqlConnection _con = new SqlConnection("server=(local);database=test;integrated security=SSPI;"))
        {
            using(SqlCommand _cmd = new SqlCommand("YourStoredProcName", _con))
            {
                _cmd.Parameters.AddWithValue("@ID", idValue);

                _cmd.Parameters.Add("@OutStringValue", SqlDbType.VarChar, 20)
                               .Direction = ParameterDirection.Output;
                _cmd.Parameters.Add("@OutBoolValue", SqlDbType.Bit)
                               .Direction = ParameterDirection.Output;

                _cmd.ExecuteNonQuery();

                result.StringValue = _cmd.Parameters["@OutStringValue"].Value.ToString();
                result.BoolValue = Convert.ToBoolean(_cmd.Parameters["@OutBoolValue"].Value);

            }
        }

        return result;
    }
}

Here, be aware I am totally assuming what your stored procedure looks like (in my case, something like:

在这里,请注意我完全假设您的存储过程是什么样的(在我的情况下,类似于:

CREATE PROCEDURE dbo.YourStoredProcName
   (@ID INT, @OutStringValue VARCHAR(20) OUTPUT, @OutBoolValue BIT OUTPUT)

This might be totally different for you! You need to make sure to adapt this to your actual case!

这对你来说可能完全不同!您需要确保根据您的实际情况进行调整!

4) Ultimately, you'll need a way to host your WCF service (in IIS, or yourself in a console app or NT Service) - that's just standard WCF work

4)最终,您将需要一种方式来托管您的 WCF 服务(在 IIS 中,或者您自己在控制台应用程序或 NT 服务中)——这只是标准的 WCF 工作

5) Last but not least, your client app will need to "Add Service Reference" to that WCF service in order to be able to call it - again: this is totally standard WCF stuff, no special steps needed here.

5) 最后但并非最不重要的一点是,您的客户端应用程序需要向该 WCF 服务“添加服务引用”,以便能够调用它 - 再说一遍:这完全是标准的 WCF 内容,这里不需要特殊步骤。

So there you have it - a WCF service calling your stored proc in the database, returning some values in a custom data type back to the caller.

所以你有它 - 一个 WCF 服务调用你在数据库中存储的过程,将自定义数据类型中的一些值返回给调用者。

Marc

马克