lundi 27 juillet 2015

c# return datatable using rest web services, can table object be returned?

I am trying to create my first web service. Goal being to have my desktop application run sql queries against a cloud database.

I've done a few rest tutorials and have a question on the concept.

I see the purpose of using REST is to send simple HTTP requests like GET. options using xml or json formatting.

The example I have working so far returns one item from the datatable from database. How would I return the whole datatable from query?

Can I actually return the datatable object, or do I have to do some kind of reading each string and create a new datatable on the client side desktop application?

hope this is making sense

[ServiceContract]
public interface IRestServiceImpl
{
    [OperationContract]

    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "json/")]

    string JSONData(string id);

}


        public string JSONData(string id)
    {

        using (SqlConnection con = new SqlConnection(sqlConnectionString))
        {
            //update sql table to include AccountID column
            string queryString = "SELECT " +
                                      "dbo.PrimeOpenPositions.[FileDate] " +
                                      ",dbo.PrimeOpenPositions.[AcctNum] " +
                                      ",dbo.PrimeOpenPositions.[AcctShortCode] " +
                                      ",dbo.PrimeOpenPositions.[Exchange] " +
                                      ",dbo.PrimeOpenPositions.[Sector] " +
                                      ",dbo.PrimeOpenPositions.[Quantity] " +
                                      ",dbo.PrimeOpenPositions.[Description] " +
                                      ",dbo.PrimeOpenPositions.[Strike] " +
                                      ",dbo.PrimeOpenPositions.[PutCall] " +
                                      ",dbo.PrimeOpenPositions.[StmtSettPrice] " +
                                      ",dbo.PrimeOpenPositions.[MktVal] " +
                                      ",dbo.PrimeOpenPositions.[Currency] " +
                                      ",dbo.PrimeOpenPositions.[Month] " +
                                      ",dbo.PrimeOpenPositions.[Prompt] " +
                                      ",dbo.PrimeOpenPositions.[ShortDesc] " +
                                      ",dbo.PrimeOpenPositions.[BBSymbol] " +
                                      ",dbo.PrimeOpenPositions.[AccountID] " +
                                "FROM dbo.PrimeOpenPositions " +
                                "INNER JOIN dbo.Mapping_ClientAccount ON dbo.Mapping_ClientAccount.AccountID = dbo.PrimeOpenPositions.AccountID " +
                                "INNER JOIN dbo.Clientstbl ON dbo.ClientsTbl.ClientID = dbo.Mapping_ClientAccount.ClientID " +
                                "INNER JOIN dbo.AccountsTbl ON dbo.AccountsTbl.AccountID = dbo.PrimeOpenPositions.AccountID " +
                                "AND dbo.Clientstbl.ClientID = '" + clientID + "'";
            using (SqlCommand command = new SqlCommand(queryString, con))
            {
                con.Open();
                using (SqlDataAdapter da = new SqlDataAdapter(command))
                {
                    da.Fill(datatablePrimeOpenPositions);
                }
                con.Close();
            }
        }

        //return datatablePrimeOpenPositions;
        return "You requested product " + datatablePrimeOpenPositions.Rows[1][1].ToString();
    }




Aucun commentaire:

Enregistrer un commentaire