案例1:

一丶webservice执行存储过程 随笔 第1张
 1 public void GetClass(string name, string id)
 2         {
 3             try
 4             {
 5                 string connectString = System.Configuration.ConfigurationSettings.AppSettings["connStr"];
 6                 SqlConnection conn = new SqlConnection(connectString);
 7                 conn.Open();
 8                 SqlCommand comm = new SqlCommand();
 9                 comm.Connection = conn;
10                 comm.CommandText = "PROC_ChoicePersonnel";
11                 comm.CommandType = System.Data.CommandType.StoredProcedure;
12                 //传值以及赋值  
13                 SqlParameter[] sps = new SqlParameter[] {
14                     new SqlParameter("@ClassName",name),
15                     new SqlParameter("@id",id),
16                     new SqlParameter("@flag","class")
17                   };
18                 comm.Parameters.AddRange(sps);
19 
20                 SqlDataAdapter sdap = new SqlDataAdapter();
21                 sdap.SelectCommand = comm;
22                 DataTable dt = new DataTable();
23                 sdap.Fill(dt);
24                 conn.Close();
25                 StringBuilder sb = new StringBuilder();
26                 sb.Append("[");
27                 for (int i = 0; i < dt.Rows.Count; i++)
28                 {
29                     sb.Append("{");
30                     sb.Append("\"id\":\"" + dt.Rows[i]["id"].ToString() + "\",");
31                     sb.Append("\"name\":\"" + dt.Rows[i]["name"].ToString() + "\"");
32                     sb.Append("},");
33                 }
34                 sb.Remove(sb.ToString().LastIndexOf(','), 1);
35                 sb.Append("]");
36                 Result(sb);
37             }
38             catch (Exception ex)
39             {
40 
41             }
42         }    
43 
44       private void Result(StringBuilder sb)
45         {
46             Context.Response.Charset = "UTF-8"; //设置字符集类型  
47             Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
48             Context.Response.ContentType = "text/plain";
49             Context.Response.Write(sb.ToString());
50             Context.Response.End();
51         }
View Code

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄