简体   繁体   中英

How to execute sql server stored procedure by using json?

Iam creating form for point of sale for resturaunt management system in Asp.net,I have two section one is Category like which have buttons like Drinks,Pizza etc and 2nd section is of Item which are sub menus of Category like if you click on Drink it should open sub menus Pepsi,Coke,Mirinda etc from sql server table.

i have created run time buttons of category,now i want to use json to execute stored procedure to show sub menus in item on the click of category Buttons.please explain the process of connectivity with sql server and to execute stored procedure by using json.Thanks in Advance. Regards saqib

JSON won't execute a command. It is merely a way of storing data in a text-based form easily readable for developers. You will need to create a WebMethod if you want to use JSON to store the values of the DropDown s or return the data into DropDown s. The following code shows how to create a Web Service and use JSON in order to return results from the database. You would use the DataContractJsonSerializer class to also read JSON . This example came from Using JSON with ASP.NET 3.5

using System;
using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Data.SqlClient;
using System.Runtime.Serialization.Json;
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ProductService
{   
 [OperationContract]    
 public string GetProductDetailsByProductID(int productID)
 {
   Product prod = new Product();        
   string connectionString = 
     "server=localhost;uid=sa;pwd=thiru;database=AdventureWorks;";
   using (SqlConnection connection = new SqlConnection(connectionString))
   {
     string sql = "Select Name, ProductNumber from Production.Product " +
       " Where ProductID = " + productID.ToString();
     connection.Open();
     SqlCommand command = new SqlCommand(sql, connection); 
     SqlDataReader reader = command.ExecuteReader();
     while (reader.Read())
     {
       prod.Name = reader["Name"].ToString();
       prod.ProductNumber = reader["ProductNumber"].ToString();
       prod.ProductID = productID;
     }
    }
    MemoryStream stream = new MemoryStream();
    DataContractJsonSerializer serializer = new 
      DataContractJsonSerializer(typeof(Product));
    serializer.WriteObject(stream, prod);
    stream.Position = 0;
    StreamReader streamReader = new StreamReader(stream);
    return streamReader.ReadToEnd();        
  } 
}
[DataContract]
public class Product
{
    [DataMember]
    public int ProductID;
    [DataMember]
    public string Name;
    [DataMember]
    public string ProductNumber;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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