劉德軍
摘 要: Webservice,又名Web服務,它是一種Web應用程序,是自包含、自描述、模塊化的應用,可以在網絡中被描述、發(fā)布、查找以及通過Web來調用,它是基于網絡的、分布式的模塊化組件,它執(zhí)行特定的任務,遵守具體的技術規(guī)范,這些規(guī)范使得WebService能與其他兼容的組件進行互操作。
Webservice具有平臺無關性,編程語言無關性等特性。它可以被.net,java,delphi,powerbuilder,vc,c++,jbuilder等開發(fā)工具或語言所調用。本文通過C#調用WebService成功實現(xiàn)區(qū)域衛(wèi)生平臺數(shù)據(jù)接口上傳。
關鍵詞: WebService;區(qū)域衛(wèi)生平臺;接口;上傳
前提條件
1、接口文檔:本例中使用的是根據(jù)《江蘇省健康信息平臺共享數(shù)據(jù)集(醫(yī)療+公共衛(wèi)生部分)2018版V2.02》修訂的接口文檔。
2、網絡連通:要求醫(yī)療單位保證數(shù)據(jù)上傳程序所在機器與數(shù)據(jù)中心機房網絡的暢通。
需求
要求接口完成如下需求:1、每日產生的數(shù)據(jù)上傳到區(qū)域衛(wèi)生平臺;2、自動定時執(zhí)行;3、記錄傳送入?yún)⒑头祷亟Y果到日志文件中。
接口文件內容說明
1、服務名稱:VKT_UploadService
2、接口地址:http://192.168.21.4:87/VKT_UploadService.asmx?wsdl
3、入?yún)⒓俺鰠ⅲ裕?/p>
過程分析
1、完成數(shù)據(jù)集的對照,即完成藥品、診療以及其他數(shù)據(jù)與本院數(shù)據(jù)的對照工作;
2、引用WebService服務;
3、創(chuàng)建每個業(yè)務所需的SQL語句,并構建入?yún)ⅲ?/p>
4、執(zhí)行上傳操作。
詳細步驟
1、建立C#應用程序
2、添加WebService服務:在“項目”菜單中選擇“添加服務引用(S)…”,彈出“添加服務引用”對話框,在“地址”中填入接口地址,點擊“前往”,系統(tǒng)會在服務欄加載對應的項目,并在備注中顯示“在地址“http://192.168.21.4:87/VKT_UploadService.asmx?wsdl”處找到1個服務”,命名空間設置為:ServiceReference1,點“確定”完成設置。如圖1示:
圖1
圖2
3、數(shù)據(jù)對照(略)。
4、界面控件定義
建立界面如圖2,控件參數(shù)見下表。
表1
5、詳細代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 區(qū)域衛(wèi)生平臺接口.ServiceReference1;
using System.IO;
//變量定量
public string LogStr = "";//日志變量
public Int32 DTransSuccess = 0;//上傳成功
public Int32 DTransFail = 0;//上傳失敗
string Lstr1="";//格式一頭
string Lstr2="";//格式一尾
string Lstr3="";//格式二外頭
string Lstr4="";//格式二外中
string Lstr5="";//格式二內頭
string Lstr6="";//格式二內尾
string Lstr7="";//格式二內部一
string Lstr8="";//格式二內部二
區(qū)域衛(wèi)生平臺接口.ServiceReference1.VKT_UpBusinessDataFromHosSoapClient myService = null;
//格式一: [適用于無主從關系的記錄] StrInit(string Vid, string Vstr)
//格式二: [適用于主從關系的記錄] StrInitSub(string Vid, string Vstr0),略
//定義門急診掛號信息函數(shù)UP_registerrecord(),共25個入?yún)ⅲ?/p>
//執(zhí)行上傳,并將相關內容保存到日志文件中
private void button1_Click(object sender, EventArgs e)
{
string jsonStr;
jsonStr = this.textBox1.Text;
LogStr=DateTime.Now.ToString() + ",上傳區(qū)域衛(wèi)生平臺數(shù)據(jù)內容如下:"+this.textBox1.Text+"。\r\n";
string Tmpstr;
Tmpstr = myService.VKT_UploadService(jsonStr);//執(zhí)行上傳
LogStr = LogStr + DateTime.Now.ToString() + ",返回結果如下:" + Tmpstr + "。\r\n";
Tmpstr =Tmpstr.Replace("\\r\\n", "\r\n").Replace("\\", "").Replace("\"", "");
if (Tmpstr.Contains("success: 1"))
{
DTransSuccess++;//判斷成功則計數(shù)加1
}
else
{
DTransFail ++; //判斷失敗則計數(shù)加1
}
this.richTextBox1.Text= Tmpstr;
WriteLog(LogStr, (this.dtTpStart.Value.Date == this.dtTpEnd.Value.Date) ? this.dtTpStart.Value.ToShortDateString() + "_" + DateTime.Today.ToShortDateString().ToString() + ".log" : this.dtTpStart.Value.ToShortDateString() + "_" + this.dtTpEnd.Value.ToShortDateString() + "_" + DateTime.Today.ToShortDateString().ToString() + ".log");//寫日志
LogStr = "";
}
//寫日志文件
private void WriteLog(string Wstr,string strFilePath)
{
string Current;
Current = Directory.GetCurrentDirectory();//獲取當前根目錄
strFilePath = Current + "\\QYWSPT" +strFilePath;
System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
sw.WriteLine( Wstr);
sw.Close();
fs.Close();
}
//生成門急診掛號信息并執(zhí)行上傳
private void button2_Click(object sender, EventArgs e)
{
DTransSuccess = 0;
DTransFail = 0;
OleDbConnection connORCL = new OleDbConnection(Properties.Settings.Default.connectionORCLString);//調Settings.settings 中的connectionORCLString對應的連接字符串
string mySelectQuery = Properties.Settings.Default.Str0201;//調Settings.settings 中的Str0201對應的SQL語句
mySelectQuery = String.Format(@mySelectQuery, this.dtTpStart.Value.ToShortDateString(), this.dtTpEnd.Value.ToShortDateString());
OleDbConnection myConnection = new OleDbConnection(Properties.Settings.Default.connectionORCLString);
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
myConnection.Open();
OleDbCommand oledbcom = new OleDbCommand(mySelectQuery, myConnection);
DataSet ds1 = new DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter(oledbcom);
adp.Fill(ds1, "srctable");
if (ds1.Tables["srctable"].Rows.Count > 0) //如果查詢記錄數(shù)>0
{
this.toolStripProgressBar1.Value = 0;
StrInit("0201", "registerrecord");
int x = 1;
toolStripStatusLabel1.Text = "共" + ds1.Tables[0].Rows.Count.ToString().Trim() + "條記錄。";
this.toolStripProgressBar1.Minimum = 0;
this.toolStripProgressBar1.Maximum = ds1.Tables[0].Rows.Count;
this.toolStripProgressBar1.Step = 1;
foreach (DataRow dw in ds1.Tables[0].Rows) //遍歷每一行數(shù)據(jù)
{
int Cnum = ds1.Tables[0].Columns.Count;//取查詢總列數(shù)
string[] Vstr = new string[Cnum];
//取當前行中的每一列數(shù)據(jù)存到Vstr數(shù)組中
for (Int32 i = 0; i <= Cnum - 1; i++)
{
Vstr[i] = dw.ItemArray[i].ToString();
}
}
//構建字符串,并調用UP_registerrecord函數(shù)
this.textBox1.Text = Lstr1 + "\r\n{\r\n";
this.textBox1.Text = this.textBox1.Text + UP_registerrecord(Vstr[0], Vstr[1], Vstr[2], Vstr[3], Vstr[4], Vstr[5], Vstr[6], Vstr[7], Vstr[8], Vstr[9], Vstr[10], Vstr[11], Vstr[12], Vstr[13], Vstr[14], Vstr[15], Vstr[16], Vstr[17], Vstr[18], Vstr[19], Vstr[20], Vstr[21], Vstr[22], Vstr[23], Vstr[24]);
this.textBox1.Text = this.textBox1.Text + "\r\n}\r\n" + Lstr2;
button1.PerformClick(); //調用上傳按鈕事件執(zhí)行上傳
this.toolStripProgressBar1.Value++;
toolStripStatusLabel1.Text = "共" + ds1.Tables[0].Rows.Count.ToString().Trim() + "條記錄。正在上傳第" + x.ToString().Trim() + "個患者記錄,成功:" + DTransSuccess.ToString().Trim() + "個,失?。? + DTransFail.ToString().Trim() + "個。";
x++;
this.Refresh();
}
}
else
{
toolStripStatusLabel1.Text = "沒有記錄。";
}
myConnection.Close();//關閉數(shù)據(jù)庫連接
myConnection.Dispose();
DTransSuccess = 0;
DTransFail = 0;
}
2、生成可執(zhí)行文件
在C#編譯生成可執(zhí)行文件,該文件位于當前工程目錄bin\Debug下,若要定時執(zhí)行,可在“任務計劃”中添加任務計劃,定時執(zhí)行即可(注意執(zhí)行完成后要將程序退出)。
總結
本文中采用WebService實現(xiàn)區(qū)域衛(wèi)生平臺數(shù)據(jù)接口上傳功能,在實際開發(fā)過程中要注意以下幾點:1、需要増加try…catch…異常捕獲,以免程序異常報錯;2、對于有主從結構的數(shù)據(jù),可以按照先遍歷主表,再根據(jù)主表的主鍵值,遍歷從表的數(shù)據(jù),最終生成主從結構的上傳字符串,本例中不再贅述;3、為方便用戶進行后期修改,本例中將SQL和連接字符串放在Settings.setting中作為可修改內容,具體文件為對應Debug目錄中的“區(qū)域衛(wèi)生平臺接口.exe.config ”文件中;4、本系統(tǒng)在Windows XP SP3 + Microsoft Visual Studio 2010+ORACLE 10g環(huán)境下調試通過。
參考文獻
[1]宋智軍 邱仲潘,Visual C#2010從入門到精通,電子工業(yè)出版社,2011.1.
[2]蔡月茹 柳西玲,Web Service基礎教程,清華大學出版社,2005..6.