简体   繁体   English

如何使用C#通过Web服务调用存储过程?

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

I am new to web service, 我是Web服务的新手,

The database access should be through Web Services using ADO.NET to access stored procedures. 数据库访问应该通过使用ADO.NET的Web服务来访问存储过程。

any ideas ? 有任何想法吗 ?

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 Contract which 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 Class that 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服务调用数据库中存储的proc,将自定义数据类型中的某些值返回给调用者。

Marc

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. 了解如何修改SqlCommand对象以使用存储过程。
  • Understand how to use parameters with stored procedures. 了解如何在存储过程中使用参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM