Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi all,
 
How to call stored procedure in Silverlight Web Application.
 
I'm working on Silverlight apllication and using WCF services for database connections.
 
I'm writing queries to retrieve data from database. But I need to retrieve data using stored procedures. How to achieve it?
 
There is no SQLHelper in Silverlight Application though..
 
TIA,
Posted 26-Mar-13 21:29pm
Dee_Bee3.5K
Edited 26-Mar-13 22:21pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

<pre>//Well, assuming you use ADO.Net in the service side to access the SQL Server:

//Remember, that SQL Service calls sits in the .Net service application - which is using 
//clean .Net (2.0 -> 4.5) and that, has ADO.Net access. You cannot access ADO.Net 
//directly from the Silverlight side of the application...

//The following code is a method on the service side. It's referred to as a service call. 
//It goes in the .svc file, as a new method.

[OperationContract]
public List<resultingClass>() RunSomeDataFromProcedure()
{
  //Resulting class is the class / type you want to return over the service.
  //You must create it - and that is beyond the scope of the question
  List<resultingClass> result = new List<resultingclass>();
 
  //The normal ADO.Net or SQL Server connection string
  String connectionString = "Your connection string";
 
  //A connection to the database
  System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
  conn.ConnectionString = connectionString; //Set the connection's connection string

  //A SQL Command object, to execute the command against
  System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
  cmd.Connection = conn;
  cmd.CommandType = System.Data.CommandType.Text;
  cmd.CommandText = "EXECUTE [DBNAME].[dbo].[StoredProcName] Parameters";
 
  //Execute the command
  System.Data.SqlDataReader dataReader = cmd.ExecuteReader();
 
  while (dataReader.Read())
  {  
     //Build resulting class / object / model here
     ResultingClass item = new ResultingClass();
     item.Field1 = dataReader["Field1"].ToString();
     ...
     ..
     .
  }
 
  return result;
}
 
//Note: I have not done any exception handling, again, beyond the scope of the question

//Now, when you update your service, a new service call method will appear on the 
//client side. You call this, and a list of ResultingClass will be returned, over 
//the WCF Enabled Silverlight web service.
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 295
1 OriginalGriff 274
2 Shweta N Mishra 216
3 PIEBALDconsult 184
4 CPallini 155
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100