1using System; 2using System.Collections.Generic; 3using System.IO; 4using System.Linq; 5using System.Net; 6using System.Text; 7using System.Threading.Tasks; 8 9namespace ZSWinForm 10{ 11 public class ApiHelper 12 { 13 /// <summary> 14 /// 调用api返回json 15 /// </summary> 16 /// <param name="url">api地址</param> 17 /// <param name="context">接收参数</param> 18 /// <returns></returns> 19 public static string HttpApi(string uri, string context) 20 { 21 //ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 22 23 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 24 request.Proxy = null; 25 request.Method = "POST"; 26 27 //request.Accept = "*/*"; 28 request.ContentType = "application/x-www-form-urlencoded"; 29 //request.ContentType = "text/html;charset=gbk"; 30 byte[] buffer = Encoding.Default.GetBytes(context); 31 request.ContentLength = buffer.Length; 32 //WebRequest.GetSystemWebProxy(); 33 request.GetRequestStream().Write(buffer, 0, buffer.Length); 34 35 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 36 if (response == null) return null; 37 StreamReader sr = new StreamReader(response.GetResponseStream()); 38 return sr.ReadToEnd().Trim(); 39 } 40 } 41} 42 43using System; 44using System.Collections.Generic; 45using System.ComponentModel; 46using System.Data; 47using System.Drawing; 48using System.Linq; 49using System.Net; 50using System.Text; 51using System.Threading.Tasks; 52using System.Windows.Forms; 53 54namespace ZSWinForm 55{ 56 public partial class Form1 : Form 57 { 58 public Form1() 59 { 60 InitializeComponent(); 61 } 62 63 private void Form1_Load(object sender, EventArgs e) 64 { 65 66 67 } 68 69 private void btn_submit_Click(object sender, EventArgs e) 70 { 71 string ROW_ID = txt_ROW_ID.Text; 72 string DEALER_ID = txt_DEALER_ID.Text; 73 string AMOINT_SUMMARY = txt_AMOUNT_SUMMARY.Text; 74 string COLLECT_NUMBER = txt_COLLECT_NUMBER.Text; 75 string CREATE_DT = txt_CREATE_DT.Text; 76 string LASTUPDATE_DT = txt_LASTUPDATE_DT.Text; 77 string CREATOR_ID = txt_CREATOR_ID.Text; 78 string DELIVERY_WAREHOUSE = txt_DELIVERY_WAREHOUSE.Text; 79 string INDENT_FLAG = txt_INDENT_FLAG.Text; 80 string INDENT_NUMBER = txt_INDENT_NUMBER.Text; 81 string INDENT_REMARK = txt_INDENT_REMARK.Text; 82 string LASTUPDATOR_ID = txt_LASTUPDATOR_ID.Text; 83 string SHIPPING_ADDRESS = txt_SHIPPING_ADDRESS.Text; 84 string SUPPLIER_ID = txt_SUPPLIER_ID.Text; 85 string ORDERTYPE = txt_ORDERTYPE.Text; 86 string OU_TYPE = txt_OU_TYPE.Text; 87 88 string ROW_IDS = txt_ROW_IDS.Text; 89 string INDENT_APPLY_ID = txt_INDENT_APPLY_ID.Text; 90 string PRODUCT_ID = txt_PRODUCT_ID.Text; 91 string QUANTITY = txt_QUANTITY.Text; 92 string ROW_MONEY = txt_ROW_MONEY.Text; 93 string MEASURE = txt_MEASURE.Text; 94 string PRODUCT_NO = txt_PRODUCT_NO.Text; 95 string BASE_PRICE = txt_BASE_PRICE.Text; 96 string QUANTITYSEND = txt_QUANTITYSEND.Text; 97 string QUANTITYRECEIVED = txt_QUANTITYRECEIVED.Text; 98 string CREATE_DTS = txt_CREATE_DTS.Text; 99 100 string ROW_IDS2 = txt_ROW_IDS2.Text; 101 string INDENT_APPLY_ID2 = txt_INDENT_APPLY_ID2.Text; 102 string PRODUCT_ID2 = txt_PRODUCT_ID2.Text; 103 string QUANTITY2 = txt_QUANTITY2.Text; 104 string ROW_MONEY2 = txt_ROW_MONEY2.Text; 105 string MEASURE2 = txt_MEASURE2.Text; 106 string PRODUCT_NO2 = txt_PRODUCT_NO2.Text; 107 string BASE_PRICE2 = txt_BASE_PRICE2.Text; 108 string QUANTITYSEND2 = txt_QUANTITYSEND2.Text; 109 string QUANTITYRECEIVED2 = txt_QUANTITYRECEIVED2.Text; 110 string CREATE_DTS2 = txt_CREATE_DTS2.Text; 111 //主要是下面这几行 112 string uri = "http://localhost:5706//api/DBinfo/UpPDAData"; 113 string context = "requestContent={\"ROW_ID\":\"" + ROW_ID + "\",\"DEALER_ID\":\"" + DEALER_ID + "\",\"AMOUNT_SUMMARY\":" + AMOINT_SUMMARY + ",\"COLLECT_NUMBER\":\"" + COLLECT_NUMBER + "\",\"CREATE_DT\":\"" + CREATE_DT + "\",\"LASTUPDATE_DT\":\"" + LASTUPDATE_DT + "\",\"CREATOR_ID\":\"" + CREATOR_ID + "\",\"DELIVERY_WAREHOUSE\":\"" + DELIVERY_WAREHOUSE + "\",\"INDENT_FLAG\":\"" + INDENT_FLAG + "\",\"INDENT_NUMBER\":\"" + INDENT_NUMBER + "\",\"INDENT_REMARK\":\"" + INDENT_REMARK + "\",\"LASTUPDATOR_ID\":\"" + LASTUPDATOR_ID + "\",\"SHIPPING_ADDRESS\":\"" + SHIPPING_ADDRESS + "\",\"SUPPLIER_ID\":\"" + SUPPLIER_ID + "\",\"ORDERTYPE\":\"" + ORDERTYPE + "\",\"OU_TYPE\":\"" + OU_TYPE + "\",\"ZS_DBMXInfos\":[{\"ROW_ID\":\"" + ROW_IDS + "\",\"INDENT_APPLY_ID\":\"" + INDENT_APPLY_ID + "\",\"PRODUCT_ID\":\"" + PRODUCT_ID + "\",\"QUANTITY\":\"" + QUANTITY + "\",\"ROW_MONEY\":\"" + ROW_MONEY + "\",\"MEASURE\":\"" + MEASURE + "\",\"PRODUCT_NO\":\"" + PRODUCT_NO + "\",\"BASE_PRICE\":\"" + BASE_PRICE + "\",\"QUANTITYSEND\":\"" + QUANTITYSEND + "\",\"QUANTITYRECEIVED\":\"" + QUANTITYRECEIVED + "\",\"CREATE_DT\":\"" + CREATE_DTS + "\"},{\"ROW_ID\":\"" + ROW_IDS2 + "\",\"INDENT_APPLY_ID\":\"" + INDENT_APPLY_ID2 + "\",\"PRODUCT_ID\":\"" + PRODUCT_ID2 + "\",\"QUANTITY\":\"" + QUANTITY2 + "\",\"ROW_MONEY\":\""+ ROW_MONEY2 + "\",\"MEASURE\":\""+ MEASURE2 + "\",\"PRODUCT_NO\":\""+ PRODUCT_NO2 + "\",\"BASE_PRICE\":\""+ BASE_PRICE2 + "\",\"QUANTITYSEND\":\""+ QUANTITYSEND2 + "\",\"QUANTITYRECEIVED\":\""+ QUANTITYRECEIVED2 + "\",\"CREATE_DT\":\""+ CREATE_DTS2 + "\"}]}"; 114 var str = ApiHelper.HttpApi(uri,context); 115 txt_result.Text = str; 116 117 } 118 119 120 } 121} 122 123using System; 124using System.Net; 125using System.Net.Http; 126using System.Web.Http; 127using Newtonsoft.Json; 128using System.Configuration; 129using System.Web; 130using System.Data.SqlClient; 131using System.Data; 132 133namespace ZKT.Mobile.Web.Controllers 134{ 135 public class DBInfoController : BaseController 136 { 137 string connstr = ConfigurationManager.ConnectionStrings["ZKT"].ConnectionString; 138 [HttpPost] 139 public HttpResponseMessage UpPDAData() 140 { 141 string rev = HttpContext.Current.Request.Params["requestContent"]; 142 ZS_DBInfo i = JsonConvert.DeserializeObject<ZS_DBInfo>(rev); 143 BaseResponse response = new BaseResponse(); 144 SqlConnection conn = new SqlConnection(connstr); 145 SqlCommand cmd = new SqlCommand(); 146 cmd.Connection = conn; 147 conn.Open(); 148 SqlTransaction trans = conn.BeginTransaction(); 149 cmd.Transaction = trans; 150 try 151 { 152 cmd.CommandText = @"INSERT INTO [dbo].[ZS_DBInfo] 153 ([ROW_ID] 154 ,[DEALER_ID] 155 ,[AMOUNT_SUMMARY] 156 ,[COLLECT_NUMBER] 157 ,[CREATE_DT] 158 ,[LASTUPDATE_DT] 159 ,[CREATOR_ID] 160 ,[DELIVERY_WAREHOUSE] 161 ,[INDENT_FLAG] 162 ,[INDENT_NUMBER] 163 ,[INDENT_REMARK] 164 ,[LASTUPDATOR_ID] 165 ,[SHIPPING_ADDRESS] 166 ,[SUPPLIER_ID] 167 ,[ORDERTYPE] 168 ,[OU_TYPE]) 169 VALUES 170 (@ROW_ID 171 ,@DEALER_ID 172 ,@AMOUNT_SUMMARY 173 ,@COLLECT_NUMBER 174 ,@CREATE_DT 175 ,@LASTUPDATE_DT 176 ,@CREATOR_ID 177 ,@DELIVERY_WAREHOUSE 178 ,@INDENT_FLAG 179 ,@INDENT_NUMBER 180 ,@INDENT_REMARK 181 ,@LASTUPDATOR_ID 182 ,@SHIPPING_ADDRESS 183 ,@SUPPLIER_ID 184 ,@ORDERTYPE 185 ,@OU_TYPE); 186 select @@IDENTITY As ID;"; 187 cmd.CommandType = CommandType.Text; 188 SqlParameter[] parp = new SqlParameter[]{ 189 new SqlParameter("@ROW_ID",i.ROW_ID), 190 new SqlParameter("@DEALER_ID",i.DEALER_ID), 191 new SqlParameter("@AMOUNT_SUMMARY",i.AMOUNT_SUMMARY), 192 new SqlParameter("@COLLECT_NUMBER",i.COLLECT_NUMBER), 193 new SqlParameter("@CREATE_DT",i.CREATE_DT), 194 new SqlParameter("@LASTUPDATE_DT",i.LASTUPDATE_DT), 195 new SqlParameter("@CREATOR_ID",i.CREATOR_ID), 196 new SqlParameter("@DELIVERY_WAREHOUSE",i.DELIVERY_WAREHOUSE), 197 new SqlParameter("@INDENT_FLAG",i.INDENT_FLAG), 198 new SqlParameter("@INDENT_NUMBER",i.INDENT_NUMBER), 199 new SqlParameter("@INDENT_REMARK",i.INDENT_REMARK), 200 new SqlParameter("@LASTUPDATOR_ID",i.LASTUPDATOR_ID), 201 new SqlParameter("@SHIPPING_ADDRESS",i.SHIPPING_ADDRESS), 202 new SqlParameter("@SUPPLIER_ID",i.SUPPLIER_ID), 203 new SqlParameter("@ORDERTYPE",i.ORDERTYPE), 204 new SqlParameter("@OU_TYPE",i.OU_TYPE) 205 }; 206 cmd.Parameters.AddRange(parp); 207 int autono = Convert.ToInt32(cmd.ExecuteScalar()); 208 foreach (var a in i.ZS_DBMXInfos) 209 { 210 cmd.Parameters.Clear(); 211 cmd.CommandText = @"INSERT INTO [dbo].[ZS_DBMXInfo] 212 ([ROW_ID] 213 ,[INDENT_APPLY_ID] 214 ,[PRODUCT_ID] 215 ,[QUANTITY] 216 ,[ROW_MONEY] 217 ,[MEASURE] 218 ,[PRODUCT_NO] 219 ,[BASE_PRICE] 220 ,[QUANTITYSEND] 221 ,[QUANTITYRECEIVED] 222 ,[CREATE_DT] 223 ,[Autono]) 224 VALUES 225 (@ROW_ID 226 ,@INDENT_APPLY_ID 227 ,@PRODUCT_ID 228 ,@QUANTITY 229 ,@ROW_MONEY 230 ,@MEASURE 231 ,@PRODUCT_NO 232 ,@BASE_PRICE 233 ,@QUANTITYSEND 234 ,@QUANTITYRECEIVED 235 ,@CREATE_DT 236 ,@Autono)"; 237 cmd.CommandType = CommandType.Text; 238 SqlParameter[] paras2 = new SqlParameter[]{ 239 new SqlParameter("@ROW_ID",a.ROW_ID), 240 new SqlParameter("@INDENT_APPLY_ID",a.INDENT_APPLY_ID), 241 new SqlParameter("@PRODUCT_ID",a.PRODUCT_ID), 242 new SqlParameter("@QUANTITY",a.QUANTITY), 243 new SqlParameter("@ROW_MONEY",a.ROW_MONEY), 244 new SqlParameter("@MEASURE",a.MEASURE), 245 new SqlParameter("@PRODUCT_NO",a.PRODUCT_NO), 246 new SqlParameter("@BASE_PRICE",a.BASE_PRICE), 247 new SqlParameter("@QUANTITYSEND",a.QUANTITYSEND), 248 new SqlParameter("@QUANTITYRECEIVED",a.QUANTITYRECEIVED), 249 new SqlParameter("@CREATE_DT",a.CREATE_DT), 250 new SqlParameter("@Autono",autono) 251 }; 252 cmd.Parameters.AddRange(paras2); 253 cmd.ExecuteNonQuery(); 254 } 255 256 trans.Commit(); 257 response.Status = 1; 258 response.Message = "添加成功!"; 259 } 260 catch (Exception e) 261 { 262 trans.Rollback(); 263 response.Status = 0; 264 response.Message = "添加失败!"; 265 } 266 finally 267 { 268 conn.Close(); 269 trans.Dispose(); 270 conn.Dispose(); 271 } 272 return Request.CreateResponse(HttpStatusCode.OK, response); 273 } 274 } 275}
C#后台调用webapi
Stella981
2021-10-11
1239 0 0
点赞
收藏
评论区
加载中...