using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ZSWinForm
{
public class ApiHelper
{
/// <summary>
/// 调用api返回json
/// </summary>
/// <param name="url">api地址</param>
/// <param name="context">接收参数</param>
/// <returns></returns>
public static string HttpApi(string uri, string context)
{
//ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Proxy = null;
request.Method = "POST";
//request.Accept = "*/*";
request.ContentType = "application/x-www-form-urlencoded";
//request.ContentType = "text/html;charset=gbk";
byte[] buffer = Encoding.Default.GetBytes(context);
request.ContentLength = buffer.Length;
//WebRequest.GetSystemWebProxy();
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response == null) return null;
StreamReader sr = new StreamReader(response.GetResponseStream());
return sr.ReadToEnd().Trim();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ZSWinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn_submit_Click(object sender, EventArgs e)
{
string ROW_ID = txt_ROW_ID.Text;
string DEALER_ID = txt_DEALER_ID.Text;
string AMOINT_SUMMARY = txt_AMOUNT_SUMMARY.Text;
string COLLECT_NUMBER = txt_COLLECT_NUMBER.Text;
string CREATE_DT = txt_CREATE_DT.Text;
string LASTUPDATE_DT = txt_LASTUPDATE_DT.Text;
string CREATOR_ID = txt_CREATOR_ID.Text;
string DELIVERY_WAREHOUSE = txt_DELIVERY_WAREHOUSE.Text;
string INDENT_FLAG = txt_INDENT_FLAG.Text;
string INDENT_NUMBER = txt_INDENT_NUMBER.Text;
string INDENT_REMARK = txt_INDENT_REMARK.Text;
string LASTUPDATOR_ID = txt_LASTUPDATOR_ID.Text;
string SHIPPING_ADDRESS = txt_SHIPPING_ADDRESS.Text;
string SUPPLIER_ID = txt_SUPPLIER_ID.Text;
string ORDERTYPE = txt_ORDERTYPE.Text;
string OU_TYPE = txt_OU_TYPE.Text;
string ROW_IDS = txt_ROW_IDS.Text;
string INDENT_APPLY_ID = txt_INDENT_APPLY_ID.Text;
string PRODUCT_ID = txt_PRODUCT_ID.Text;
string QUANTITY = txt_QUANTITY.Text;
string ROW_MONEY = txt_ROW_MONEY.Text;
string MEASURE = txt_MEASURE.Text;
string PRODUCT_NO = txt_PRODUCT_NO.Text;
string BASE_PRICE = txt_BASE_PRICE.Text;
string QUANTITYSEND = txt_QUANTITYSEND.Text;
string QUANTITYRECEIVED = txt_QUANTITYRECEIVED.Text;
string CREATE_DTS = txt_CREATE_DTS.Text;
string ROW_IDS2 = txt_ROW_IDS2.Text;
string INDENT_APPLY_ID2 = txt_INDENT_APPLY_ID2.Text;
string PRODUCT_ID2 = txt_PRODUCT_ID2.Text;
string QUANTITY2 = txt_QUANTITY2.Text;
string ROW_MONEY2 = txt_ROW_MONEY2.Text;
string MEASURE2 = txt_MEASURE2.Text;
string PRODUCT_NO2 = txt_PRODUCT_NO2.Text;
string BASE_PRICE2 = txt_BASE_PRICE2.Text;
string QUANTITYSEND2 = txt_QUANTITYSEND2.Text;
string QUANTITYRECEIVED2 = txt_QUANTITYRECEIVED2.Text;
string CREATE_DTS2 = txt_CREATE_DTS2.Text;
//主要是下面这几行
string uri = "http://localhost:5706//api/DBinfo/UpPDAData";
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 + "\"}]}";
var str = ApiHelper.HttpApi(uri,context);
txt_result.Text = str;
}
}
}
using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace ZKT.Mobile.Web.Controllers
{
public class DBInfoController : BaseController
{
string connstr = ConfigurationManager.ConnectionStrings["ZKT"].ConnectionString;
[HttpPost]
public HttpResponseMessage UpPDAData()
{
string rev = HttpContext.Current.Request.Params["requestContent"];
ZS_DBInfo i = JsonConvert.DeserializeObject<ZS_DBInfo>(rev);
BaseResponse response = new BaseResponse();
SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
SqlTransaction trans = conn.BeginTransaction();
cmd.Transaction = trans;
try
{
cmd.CommandText = @"INSERT INTO [dbo].[ZS_DBInfo]
([ROW_ID]
,[DEALER_ID]
,[AMOUNT_SUMMARY]
,[COLLECT_NUMBER]
,[CREATE_DT]
,[LASTUPDATE_DT]
,[CREATOR_ID]
,[DELIVERY_WAREHOUSE]
,[INDENT_FLAG]
,[INDENT_NUMBER]
,[INDENT_REMARK]
,[LASTUPDATOR_ID]
,[SHIPPING_ADDRESS]
,[SUPPLIER_ID]
,[ORDERTYPE]
,[OU_TYPE])
VALUES
(@ROW_ID
,@DEALER_ID
,@AMOUNT_SUMMARY
,@COLLECT_NUMBER
,@CREATE_DT
,@LASTUPDATE_DT
,@CREATOR_ID
,@DELIVERY_WAREHOUSE
,@INDENT_FLAG
,@INDENT_NUMBER
,@INDENT_REMARK
,@LASTUPDATOR_ID
,@SHIPPING_ADDRESS
,@SUPPLIER_ID
,@ORDERTYPE
,@OU_TYPE);
select @@IDENTITY As ID;";
cmd.CommandType = CommandType.Text;
SqlParameter[] parp = new SqlParameter[]{
new SqlParameter("@ROW_ID",i.ROW_ID),
new SqlParameter("@DEALER_ID",i.DEALER_ID),
new SqlParameter("@AMOUNT_SUMMARY",i.AMOUNT_SUMMARY),
new SqlParameter("@COLLECT_NUMBER",i.COLLECT_NUMBER),
new SqlParameter("@CREATE_DT",i.CREATE_DT),
new SqlParameter("@LASTUPDATE_DT",i.LASTUPDATE_DT),
new SqlParameter("@CREATOR_ID",i.CREATOR_ID),
new SqlParameter("@DELIVERY_WAREHOUSE",i.DELIVERY_WAREHOUSE),
new SqlParameter("@INDENT_FLAG",i.INDENT_FLAG),
new SqlParameter("@INDENT_NUMBER",i.INDENT_NUMBER),
new SqlParameter("@INDENT_REMARK",i.INDENT_REMARK),
new SqlParameter("@LASTUPDATOR_ID",i.LASTUPDATOR_ID),
new SqlParameter("@SHIPPING_ADDRESS",i.SHIPPING_ADDRESS),
new SqlParameter("@SUPPLIER_ID",i.SUPPLIER_ID),
new SqlParameter("@ORDERTYPE",i.ORDERTYPE),
new SqlParameter("@OU_TYPE",i.OU_TYPE)
};
cmd.Parameters.AddRange(parp);
int autono = Convert.ToInt32(cmd.ExecuteScalar());
foreach (var a in i.ZS_DBMXInfos)
{
cmd.Parameters.Clear();
cmd.CommandText = @"INSERT INTO [dbo].[ZS_DBMXInfo]
([ROW_ID]
,[INDENT_APPLY_ID]
,[PRODUCT_ID]
,[QUANTITY]
,[ROW_MONEY]
,[MEASURE]
,[PRODUCT_NO]
,[BASE_PRICE]
,[QUANTITYSEND]
,[QUANTITYRECEIVED]
,[CREATE_DT]
,[Autono])
VALUES
(@ROW_ID
,@INDENT_APPLY_ID
,@PRODUCT_ID
,@QUANTITY
,@ROW_MONEY
,@MEASURE
,@PRODUCT_NO
,@BASE_PRICE
,@QUANTITYSEND
,@QUANTITYRECEIVED
,@CREATE_DT
,@Autono)";
cmd.CommandType = CommandType.Text;
SqlParameter[] paras2 = new SqlParameter[]{
new SqlParameter("@ROW_ID",a.ROW_ID),
new SqlParameter("@INDENT_APPLY_ID",a.INDENT_APPLY_ID),
new SqlParameter("@PRODUCT_ID",a.PRODUCT_ID),
new SqlParameter("@QUANTITY",a.QUANTITY),
new SqlParameter("@ROW_MONEY",a.ROW_MONEY),
new SqlParameter("@MEASURE",a.MEASURE),
new SqlParameter("@PRODUCT_NO",a.PRODUCT_NO),
new SqlParameter("@BASE_PRICE",a.BASE_PRICE),
new SqlParameter("@QUANTITYSEND",a.QUANTITYSEND),
new SqlParameter("@QUANTITYRECEIVED",a.QUANTITYRECEIVED),
new SqlParameter("@CREATE_DT",a.CREATE_DT),
new SqlParameter("@Autono",autono)
};
cmd.Parameters.AddRange(paras2);
cmd.ExecuteNonQuery();
}
trans.Commit();
response.Status = 1;
response.Message = "添加成功!";
}
catch (Exception e)
{
trans.Rollback();
response.Status = 0;
response.Message = "添加失败!";
}
finally
{
conn.Close();
trans.Dispose();
conn.Dispose();
}
return Request.CreateResponse(HttpStatusCode.OK, response);
}
}
}
C#后台调用webapi
点赞
收藏