Asp.net 如何使用json执行sql server存储过程?
我正在Asp.net中为销售点再培训管理系统创建表单,我有两个部分,第一部分是 类别,如有按钮,如饮料、比萨饼等,第二部分是项目,是类别的子菜单,如单击饮料,则应打开sql server表中的子菜单百事可乐、可口可乐、美琳达等 我已经创建了类别的运行时按钮,现在我想使用json来执行存储过程 单击“类别”时在项目中显示子菜单的步骤 按钮。请解释连接过程 使用sql server并执行存储过程 通过使用json。提前感谢。Asp.net 如何使用json执行sql server存储过程?,asp.net,sql,jquery,Asp.net,Sql,Jquery,我正在Asp.net中为销售点再培训管理系统创建表单,我有两个部分,第一部分是 类别,如有按钮,如饮料、比萨饼等,第二部分是项目,是类别的子菜单,如单击饮料,则应打开sql server表中的子菜单百事可乐、可口可乐、美琳达等 我已经创建了类别的运行时按钮,现在我想使用json来执行存储过程 单击“类别”时在项目中显示子菜单的步骤 按钮。请解释连接过程 使用sql server并执行存储过程 通过使用json。提前感谢。 关于saqib,JSON不会执行命令。它仅仅是一种以基于文本的形式存储数据
关于saqib,JSON不会执行命令。它仅仅是一种以基于文本的形式存储数据的方法,开发人员很容易阅读。如果要使用
JSON
存储下拉列表的值或将数据返回到下拉列表中,则需要创建WebMethod
。下面的代码显示了如何创建Web服务并使用JSON
从数据库返回结果。您可以使用DataContractJsonSerializer
类来读取JSON
。这个例子来自
到目前为止你都试了些什么?顺便说一句,不是杰森。我不确定如何使用文本格式来表示数据来执行存储过程。也许您想将JSON与其他东西结合使用?如果有帮助,请标记为答案。:)
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;
}