简体   繁体   English

如何使用json执行SQL Server存储过程?

[英]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. 我在Asp.net中为餐厅管理系统创建销售点表格,我有两个部分,一个是“类别”,其中有诸如Drinks,Pizza等按钮,第二个部分是“ Item”,这是类别的子菜单,例如,单击“ Drink”它应该从sql server表中打开子菜单Pepsi,Coke,Mirinda等。

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. 我已经创建了类别的运行时按钮,现在我想使用json执行存储过程以在单击Buttons类别时在项目中显示子菜单。请说明与sql server的连接过程以及使用json执行存储过程。提前致谢。 Regards saqib 关于萨奇布

JSON won't execute a command. JSON不会执行命令。 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. 如果要使用JSON存储DropDown的值或将数据返回到DropDown则需要创建一个WebMethod The following code shows how to create a Web Service and use JSON in order to return results from the database. 以下代码显示了如何创建Web服务并使用JSON以便从数据库返回结果。 You would use the DataContractJsonSerializer class to also read JSON . 您将使用DataContractJsonSerializer类还读取JSON This example came from Using JSON with ASP.NET 3.5 本示例来自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;
}

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

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