webservice介面實現資料共享的實現的初步解決方案(更新、刪除)
一、伺服器A和伺服器B為對方設定專門的資料庫查詢介面,通過SOAP或者HTTP協議形成相互通訊的功能。
資料編輯操作實現伺服器相互通訊的功能示例(伺服器A和伺服器B):伺服器A為伺服器提B供web service專用服務介面,在使用者進行資料編輯(伺服器A端的訊息)操作動作訊息的時候,伺服器A先通過介面查詢伺服器B上的所字條所處的狀態(空閒或編輯狀態),同時查詢本伺服器上的編輯字條的所處的狀態。如果有一方處於編輯狀態,禁止本客戶端多字條的編輯,如果雙方查詢到的狀態都是處於閒狀態,則改變本伺服器上的資料中的字條狀態為編輯狀態進行鎖定,然後進入編輯,編輯完成後,對編輯字條的狀態進行初始化,防止多個客戶端對資料庫中的同條欄位進行編輯操作,造成資料的不同步和紊亂。
伺服器A和伺服器B進行通訊實現方式如圖1所示:
二、此方案需要保證基礎條件:
(1) 伺服器的高效能處理能力。
(2) 網路寬頻的高穩定性和異常情況的低概率。
(3) 使用者操作的及時性和保證不跨瀏覽器的情況下一次性的資料編輯的完成。
本方案設計的具體實現技術路線
以上的實現功能,可以通過流程圖提現如圖所示:
三、具體實現過程(實驗)
1 資料庫的設計
新建資料庫A(ceshi1) id的增長值初始值1,增長值2累加,用程式控制
create table customers
1、editstate為0此欄位為空閒狀態,1為在編輯忙碌狀態
2、editpersonalid為0此欄位沒有編輯人員在編輯狀態,此外記錄編輯人員的唯一標識
3、
4、useable為0此欄位可查詢,1此欄位為刪除不可用狀態
新建資料庫B(ceshi2) id的增長值初始值1,增長值2累加,用程式控制
create table customers(id int not null, name varchar(15),editstate int default 0,editpersonalid int default 0,state int default 0,useable int default 0);
1、editstate為0此欄位為空閒狀態,1為在編輯忙碌狀態
2、editpersonalid為0此欄位沒有編輯人員在編輯狀態,此外記錄編輯人員的唯一標識
3、state為0此欄位為空閒狀態,1為批量在編輯狀態
4、useable為0此欄位可查詢,1此欄位為刪除不可用狀態
2 資料庫web service介面設計(實現資料雙向同步:以資料插入資料同步實現為例)
2.1 資料庫的查詢方案設計
資料庫的查詢,使用者A和使用者B分別獨立查詢資料庫A和資料庫 B,獨立進行操作。
2.2 實現資料字條狀態查詢、狀態改變及字條資訊查詢的介面設計
資料庫A單字條編輯狀態介面設計:
[WebMethod(Description="根據需要編輯的字條ID查詢該字條的先在所處的狀態")]
public int DetTheMessageState(int MessageID) {
SqlConnection conn = new SqlConnection("server=.;database=ceshi1;integrated security=True");
conn.Open();
try
{
SqlDataAdapter sda = new SqlDataAdapter("select * from customers where id=" + MessageID, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
string state = dt.Rows[0][1].ToString();
if (state == null || state == "")
{ return 2; //資料庫中無此字條具體的資訊
}
return 1;
}
catch (Exception ex)
{
Console.Write(ex.Message);
return 0;
}
}
資料庫A判斷單字條編輯狀態介面設計:
[WebMethod(Description = "根據提供的字條key判斷是否已存在")]
public int judgeExist(string key) {
SqlConnection conn = new SqlConnection("server=.;database=ceshi1;integrated security=True");
conn.Open();
try
{
SqlDataAdapter sda = new SqlDataAdapter("select * from customers where name='" + key + "'", conn);
DataTable dt = new DataTable();
sda.Fill(dt);
string id=dt.Rows[0][0].ToString();
if (id == null || id == "")
{ return 0; }
else
return 1;
}
catch(Exception ex) {
Console.WriteLine(ex.Message);
return 0;
}
}
資料庫A單字條編輯狀態更改介面設計:
[WebMethod(Description = "根據需要編輯的字條ID查詢該字條的狀態並根據需要修改先在所處的狀態")]
public int AlertTheMessageState(int MessageID)
{
//資料庫的查連線
SqlConnection conn = new SqlConnection("server=.;database=ceshi1;integrated security=True");
conn.Open();
//資料庫的查詢 try
{
SqlDataAdapter sda = new SqlDataAdapter("select * from customers where id=" + MessageID, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
int state = (int)dt.Rows[0][2];
string name = dt.Rows[0][1].ToString();
conn.Close();
if (name != null && name != "")
{
if (state == 0)
{
SqlConnection conn1 = new SqlConnection("server=.;database=ceshi1;integrated security=True");
conn1.Open();
string strCmd = "UPDATE customers SET state = 1 WHERE id=" + MessageID;
try
{
SqlCommand sqlComm = new SqlCommand();
sqlComm.Connection = conn1;
sqlComm.CommandText = strCmd;
sqlComm.ExecuteNonQuery();
conn1.Close();
return 1;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
}
else
return 2;
}
else
return 3;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
}
資料庫A單字條插入和資料庫B同步實現介面設計:
[WebMethod(Description = "根據提供的資料字條進行資料的插入")]
public int insertDB(int MessageID,string Name) {
//資料庫的查連線
SqlConnection conn = new SqlConnection("server=.;database=ceshi1;integrated security=True");
conn.Open();
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO customers (id,name) VALUES(" + MessageID + ",'" + Name + "')";
cmd.ExecuteNonQuery();
conn.Close();
return 1;
}
catch(Exception ex) {
Console.Write(ex.Message);
return 0;
}
return 0;
}
資料庫A單字條查閱匹配:
[WebMethod(Description = "根據字條的欄位,判斷該字條是否存在")]
public int sendMessageArray(int MessageID,string Name) {
SqlConnection conn = new SqlConnection("server=.;database=ceshi1;integrated security=True");
conn.Open();
try
{
SqlDataAdapter sda = new SqlDataAdapter("select * from customers where id=" + MessageID+" and name='"+Name+"'", conn);
DataTable dt = new DataTable();
sda.Fill(dt);
string id = dt.Rows[0][0].ToString();
if (id == null || id == "")
{ return 0; }
return 1;
}
catch (Exception ex)
{
Console.Write(ex.Message);
return 0;
}
}
資料庫A單字條依照資料庫B單字條修改:
[WebMethod(Description = "根據字條的欄位進行修改本字條")]
public int updateMessageArray(int MessageID, string Name)
{
SqlConnection conn = new SqlConnection("server=.;database=ceshi1;integrated security=True");
conn.Open();
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "UPDATE customers SET name='"+Name+"' WHERE id="+MessageID;
cmd.ExecuteNonQuery();
conn.Close();
return 1;
}
catch (Exception ex)
{
Console.Write(ex.Message);
return 0;
}
}
Vs呼叫示例:
protected void sendName_Click(object sender,EventArgs e) {
SqlConnection myconn = new SqlConnection("server=.;database=ceshi2;integrated security=True");
myconn.Open();
int personalID=0;
try {
SqlCommand da = new SqlCommand("select max(id) as maxid from customers where id%2=0", myconn);
SqlDataReader ds = da.ExecuteReader();
ds.Read();
if(ds["maxid"]!=null)
{
personalID =Convert.ToInt32(ds["maxid"]);
}
else{
personalID = 0;
}
// if (persoanID> 0)
personalID = personalID + 2;
// Response.Write("<script>alert(" + Convert.ToInt32(personalID) + ");</script>"); 測試
}
catch(Exception ex)
{
personalID = personalID + 2;
Console.WriteLine(ex.Message);
// Response.Write("<script>alert(" + Convert.ToInt32(personalID) + ");</script>"); 測試
}
myconn.Close();
myconn.Open();
int comeback = 0;
string Name=aname.Value.ToString();
if (Name != null&&Name!="")
{ //查詢本資料庫本資訊是否在本地資料庫存在
int namenum = 0;
try
{
SqlDataAdapter sda = new SqlDataAdapter("select * from customers where name='" + Name + "'", myconn);
DataTable dt = new DataTable();
sda.Fill(dt);
string id=dt.Rows[0][0].ToString();
if (id != null && id != "")
{
namenum = 1;
}
else
namenum = 0;
}
catch(Exception ex){
Console.WriteLine(ex.Message);
namenum = 0;
}
localhost.Service service = new localhost.Service();
//查詢遠端伺服器資料庫 查詢此字條是否已經存在
int comeExist = service.judgeExist(Name);
if(comeExist==0&&namenum==0)
{
comeback = service.insertDB(Convert.ToInt32(personalID),Name.ToString());
string str = "INSERT INTO customers (id,name) VALUES(" + personalID + ",'" + Name + "')";
SqlCommand cmd = new SqlCommand(str, myconn);
cmd.ExecuteNonQuery();
myconn.Close();
if (comeback == 1)
{
Response.Write("<script>alert('插入資料庫的雙向同步成功!!!'); history.go(-1);</script>");
}
else
{
Response.Write("<script>alert('插入資料庫的雙向同步失敗!!!'); history.go(-1);</script>");
}
}
if (comeExist == 1 && namenum == 0)
{
Response.Write("<script>alert('遠端資料庫中資料已經存在!!!'); history.go(-1);</script>");
}
if (comeExist == 0 && namenum == 1)
{
Response.Write("<script>alert('本地資料庫中資料已經存在!!!'); history.go(-1);</script>");
}
if (comeExist == 1 && namenum == 1)
{
Response.Write("<script>alert('資料庫中資料已經存在!!!'); history.go(-1);</script>");
}
else
{
Response.Write("<script>alert('處理異常!!!'); history.go(-1);</script>");
}
}
else {
Response.Write("<script>alert('姓名不能為空!!!'); history.go(-1);</script>");
}
}
2.3 資料同步的手動API設計(RESET API設計)
手動實現資料的同步是對資料庫A和資料庫B實現特定時間的一次性更新,對資料編輯出現的異常情況進行統一處理,使伺服器不必為異常的情況出現時,做一些踏步等待或者停止去處理一下異常,留給伺服器更多的時間對資料的編輯進行處理。