C# Web Service 接收数据

C# 2017

1.新建项目选择

C# Web Service 接收数据_第1张图片

2.选择空 点击确定

C# Web Service 接收数据_第2张图片

3.新建amsx文件 点击确定C# Web Service 接收数据_第3张图片

 

 在amsx文件中编写 接收XML数据

using System.Web.Services;
using System.Xml;

namespace TBEAWebService
{
    /// 
    /// TBEA 的摘要说明
    /// 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class TBEA : System.Web.Services.WebService
    {

        /// 
        /// 编码
        /// 
        /// 
        /// 
        [WebMethod(Description ="编码")]
        public XmlDocument CompanyCode(string xmlinfo)
        {
            
            NewLife.Log.XTrace.WriteLine(xml);
            return Business.CompanyCode.SyncCompanyCode(xml);
        }

    }
}

XML文件中 编写要接受的数据字段 



  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

增加类cs文件 下面row[" "]中字段要和XML中一样

循环新增和修改也要和XML 文件中MDMName一样 因为SQL太长删除了一些


        public static XmlDocument SyncCompanyCode(string xmlinfo)
        {
            //初始化返回值
            XmlDocument result = XMLHelper.ResultInit(true, "");
            try
            {

                Dictionary> dataRows = XMLHelper.ParamsToTable(xmlinfo);
                
                Hashtable MDM_PM = XMLHelper.MDMToPM(AppDomain.CurrentDomain.BaseDirectory + "/XmlInfo/CompanyCode.xml");
                foreach (string key in dataRows.Keys)
                {
                    //设置返回值的批次UUID
                    result = XMLHelper.ResultUUIDS(result, key);
                    List rows = dataRows[key];
                    //循环每行数据,插入或更新,以主编码为主键
                    foreach (Hashtable row in rows)
                    {
                        
                        string uuid = row["UUID"].ToString();
                        if (row["开户公司编码"] == null || row["开户公司编码"].ToString() == "")
                        {
                            NewLife.Log.XTrace.WriteLine("出现【开户公司编码】为空的数据,忽略");
                            continue;
                        }
                        string sql = "select Accountopen_code from PM_MDM_CompanyCode where Accountopen_code='" + row["开户公司编码"] + "'";
                        string Accountopen_code = DBHelper.QuerySqlHasDB(sql, "Accountopen_code");
                        
                        string ActionSql = "";
                        string success = "";
                        int a = 0;
                        if (Accountopen_code != "")
                        {
                            ActionSql = "update PM_MDM_CompanyCode set Accountopen_name='" + row["开户公司名称"].ToString() + "',Bankaccount='" + row["开户银行"].ToString() + "',Accounts='" + row["账号"].ToString() + "',Mainsubject='" + row["主科目说明"].ToString() + "',";
                            ActionSql += " where Accountopen_code = '" + row["开户公司编码"].ToString() + "' ";
                            Log.RecorderLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").ToString() + ":" + ActionSql);
                            a = DBHelper.ExecuteSql(ActionSql);
                            if (a>0) {
                                success = "修改成功";
                            }
                        }
                        else
                        {
                            ActionSql = "INSERT INTO PM_MDM_CompanyCode(Id,Accountopen_name,Accountopen_code,Bankaccount,Accounts,Mainsubject,";
                            ActionSql += "Payment_Attribute,Accountopen_date,SWIFT_Code,Freeze_date,Thaw_date,Cancellation_date,Is_signContract,AccountStatus,Onlineopen_status)";
                            ActionSql += "VALUES ('"+Guid.NewGuid()+"','" + row["开户公司名称"].ToString() + "','"+ row["开户公司编码"].ToString() + "','" + row["开户银行"].ToString()+"','" + row["账号"].ToString() + "','" + row["主科目说明"].ToString() + "','" + row["子科目编码"].ToString() + "'";
                            Log.RecorderLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").ToString() + ":" + ActionSql);
                            a = DBHelper.ExecuteSql(ActionSql);
                            if (a > 0)
                            {
                                success = "新增成功";
                            }
                        }
                        result = XMLHelper.ResultUUID(result, uuid, success, true);
                    }
                }

            }
            catch (Exception ex)
            {
                result = XMLHelper.ResultUUID(result, false, ex.Message);
                NewLife.Log.XTrace.WriteLine("更新CompanyCode失败:" + ex.Message);
            }
            return result;
        }

Web.config


  
    
    
    
    
  
  
    
    
    
  

连接数据库

            //链接数据库 
            DataBaseEntity dbEntity = new DataBaseEntity();
            dbEntity.DataBaseCode = "WebServiceDB";
            dbEntity.DataBaseType = DatabaseType.SqlServer;
            dbEntity.ConnectionString = String.Format("Server={0};Initial Catalog={1};User ID={2};Password={3}"
                  , System.Configuration.ConfigurationManager.AppSettings["SqlUrl"],
                  System.Configuration.ConfigurationManager.AppSettings["DataBase"],
                 System.Configuration.ConfigurationManager.AppSettings["UserName"],
                  System.Configuration.ConfigurationManager.AppSettings["Password"]);
            XCode.DataAccessLayer.DAL.SetConn(dbEntity.DataBaseCode, dbEntity);
            return dbEntity;
        }
       /// 
        /// 执行sql返回查询的DataTable
        /// 
        /// 
        /// 
        public static DataTable QuerySql(string sql)
        {
            DataBaseEntity dbEntity = DBEntity();
            DAL dal = DAL.Create(dbEntity.DataBaseCode);
            try
            {
                return dal.Session.Query(sql).Tables[0];
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                dal.Session.AutoClose();
            }
        }
        /// 
        /// 查询sql是否在数据中存在数据
        /// 
        /// 
        /// 
        public static string QuerySqlHasDB(string sql, string columnName)
        {
            DataTable dt = QuerySql(sql);
            if (dt != null && dt.Rows.Count > 0)
                return dt.Rows[0][columnName].ToString();
            else
                return "";
        }
        /// 
        /// 获取当前表最大行数+1
        /// 
        /// 
        /// 
        public static string QuerySqlMaxSequ(string TableName, bool isDefault = true)
        {
            string sql = "select max(Sequ)+1 Sequ from PB_Defaultfield where TableName='" + TableName + "'";
            if (isDefault == false)
                sql = "select max(Sequ)+1 Sequ from " + TableName;
            string Sequ = QuerySqlHasDB(sql, "Sequ");
            if (string.IsNullOrEmpty(Sequ))
                Sequ = "1";
            return Sequ;
        }

 

不知道怎么调试输出 就写了日志

public static void RecorderLog(string sendname)
        {
            FileStream fileStream = new FileStream("E:\\Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter streamWriter = new StreamWriter(fileStream);
            streamWriter.BaseStream.Seek(0L, SeekOrigin.End);
            streamWriter.WriteLine(sendname);
            streamWriter.Flush();
            streamWriter.Close();
            fileStream.Close();
        }

 

你可能感兴趣的:(WebService,C#)