views:

801

answers:

4

I am new to web service,

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

any ideas ?

+3  A: 

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

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.
Andrew Hare
@Andrew Hare, I know the normal execution of stored procedures.I want to know, how to call and execute the sp from web service.what is the roll of web service here. How to do ?
Anuya
+1 That's a good tutorial for accessing ADO.NET through Stored Procedures. Does anyone know of an equally good one for the Web Services to ADO.NET piece?
Robert Harvey
@karthik, I think you are going to need this anyway.
Robert Harvey
I guess I don't really understand - why can't you just use ADO.NET right in the web service?
Andrew Hare
You can. I think his question is how to do just that. You want to field that one too? I'll vote it up.
Robert Harvey
so, can i do the same coding in the .asmx file for executing the stored procedure ??
Anuya
Yeah, do your database stuff in the web service. Basically it's just a page to accept data and then it does the DB stuff and outputs data back to the requesting page.
AKofC
A: 

Um... just have your page pass the stored proc values to the web service via a GET, the web service then feeds those values into the stored proc, and then the web service outputs the result back to the requesting page via JSON or XML?

AKofC
+3  A: 

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

In this case, you'll need:

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

[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):

[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:

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

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.

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.

Marc

marc_s
Fantastic answer!
davewasthere