?
WebService.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.Common;
using System.Data;
using System.Collections.Specialized;
using DataHelper;
namespace myWebService
{
///
/// WebService 的摘要说明
///
[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 WebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
string uid = GetGuid();
string test = "n7,5,20150701,10,20150701|n8,5,20150701,10,20150701|n9,5,20150701,9,20150701";
string[] rows=test.Split('|');
int rowsCount=rows.Length;
int colsCount=rows[0].Length;
string cmd = string.Empty;
string floorno=string.Empty;
Nameva lueCollection nvc=new Nameva lueCollection();
string result = string.Empty;
DataHelper.DbHelper dh = DataHelper.DataFactory.GetHelper();
dh.Open();
dh.BeginTrans();
try
{
List sql = new List();
for (int i = 0; i < rowsCount; i++)
{
sql.Add("insert into TARGETHISTORY(EQNO,FLOORNO,RUNSTART,RUNTIME,UPDATETIME,UID) values(@EQNO,@FLOORNO,@RUNSTART,@RUNTIME,@UPDATETIME,@UID)");
nvc = new Nameva lueCollection();
nvc.Add("@EQNO", rows[i].Split(',')[0]);
nvc.Add("@FLOORNO", rows[i].Split(',')[1]);
nvc.Add("@RUNSTART", rows[i].Split(',')[2]);
nvc.Add("@RUNTIME", rows[i].Split(',')[3]);
nvc.Add("@UPDATETIME", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
nvc.Add("@UID", uid);
floorno = rows[i].Split(',')[1];
dh.ExecuteNonQuery(CommandType.Text, sql[i], nvc);
}
nvc = new Nameva lueCollection();
nvc.Add("@UID", uid);
nvc.Add("@FLOORNO", floorno);
dh.ExecuteNonQuery(CommandType.StoredProcedure, "DeviceShareLoad", nvc);
dh.CommitTrans();
result= "数据导入成功!";
}
catch (Exception ex)
{
dh.RollBackTrans();
dh.Close();
result = "数据导入失败!请联系 IT! 错误原因:"+ex.ToString();
}
finally
{
dh.Close();
}
return result;
}
private static string GetGuid()
{
System.Guid guid = new Guid();
guid = Guid.NewGuid();
return guid.ToString();
}
}
}
webconfig
?
存储过程
USE [test] GO /****** Object: StoredProcedure [dbo].[DeviceShareLoad] Script Date: 07/10/2015 02:49:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[DeviceShareLoad] @floorno nvarchar(50), @uid nvarchar(50) AS BEGIN merge into targettable as t using (select * from TARGETHISTORY where uid=@uid) as s on t.floorno=@floorno and s.floorno=@floorno and t.eqno=s.eqno when matched then update set t.eqno=s.eqno,t.floorno=s.floorno,t.runstart=s.runstart,t.runtime=s.runtime,t.updatetime=getdate() when not matched and s.floorno=@floorno then insert (eqno,floorno,runstart,runtime,updatetime) values(s.eqno,s.floorno,s.runstart,s.runtime,getdate()) when not matched by source and t.floorno=@floorno then delete; SET NOCOUNT ON; END