將本地數據庫存儲到服務器數據庫


最近幾天一直在學習將本地數據庫上傳到服務器數據庫上。

作為萌新的我,終於完成了。

在學習的開始

現在程序里面將本地數據庫通過post方式上傳到網頁調用接口,服務器端進行驗證接收並存儲數據。有的地方由於項目具有安全問題,不能完全展示,但是有問題可以問我。

萌新的我很樂意答復你們。(*^▽^*)

上傳查詢比較簡單,本地數據庫上傳代碼。

public string Upload(string jId)
{

try
{
string[] sourceTableName = new string[4];
sourceTableName[0] = "0009_OnDuty";
sourceTableName[1] = "0009_PowerSavingRecord";
sourceTableName[2] = "0009_ShutDownTime";
sourceTableName[3] = "0009_SolarPanelErrInfo";

string[] targetTableName = new string[4];
targetTableName[0] = "0009_OnDuty";
targetTableName[1] = "0009_PowerSavingRecord";
targetTableName[2] = "0009_ShutDownTime";
targetTableName[3] = "0009_SolarPanelErrInfo";

for (int i = 0; i < sourceTableName.Length; i++)
{
//connection = new SQLiteConnection(str);
//connection.Open();
//string sql = "select * from " + sourceTableName[i] + " where needupload=\'1\' and Id<>'' limit 0,30";
//SQLiteCommand command = new SQLiteCommand(sql, connection);
//SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, connection);
//DataTable table = new DataTable();
//adapter.Fill(table);
//connection.Close();
string sql = null;
if (sourceTableName[i] == "xxx)
{
sql = "select xxx from " + sourceTableName[i] + " where needupload=\'1\'";
}
DataTable dt = QueryDT(sql);
if (dt != null && dt.Rows.Count > 0)
{
UploadSingle(jId, dt, sourceTableName[i], targetTableName[i]);
}
}
return "";
}
catch (Exception ex)
{
ErrorTraceListener.WriteException(ex.ToString());
return "";
}
}

public void UploadSingle(string jId, DataTable t, string sourceTableName, string targetTableName)
{
try
{
string r = JsonConvert.SerializeObject(t);
if (r == "[]")
return;
r = HttpUtility.UrlEncode(r, Encoding.Unicode);
DataBlock datablock = new DataBlock(jId, targetTableName, r);
datablock.jId = jId;
datablock.TableName = targetTableName;

INIFile INI_Param = new INIFile(Environment.CurrentDirectory + @"\Param.ini");
string url = INI_Param.Read("url", "url");
//url = "http://localhost:33478/WebClientUpLoadHandler.ashx";
Uri _baseAddress = new Uri(url);
//_baseAddress = new Uri("http://localhost:38000/WebClientUpLoadHandler.ashx");

string ss = JsonConvert.SerializeObject(datablock);
WebClient webClient = new WebClient();
webClient.Headers["Accept"] = "application/json";
webClient.Encoding = Encoding.UTF8;
webClient.UploadStringCompleted += (send, es) =>
{
try
{
if ((es.Result != null) && (es.Result != ""))
{
//connection = new SQLiteConnection(str);
//connection.Open();
DataAccess dataAccess = new DataAccess();
string sql = "update " + sourceTableName + " set needupload=0 where ID in(" + es.Result + ")";
//SQLiteCommand command = new SQLiteCommand(sql, connection);
//command.ExecuteNonQuery();
//connection.Close();
dataAccess.ExecuteSql(sql);
ActionLog.WriteLog("發送成功,編號為" + es.Result,"","","","");
}
else
{
ErrorTraceListener.WriteException("上傳失敗,服務器返回數據為空");
}
}
catch (Exception ex)
{
ErrorTraceListener.WriteException("上傳數據后本地更新錯誤," + ex.Message.ToString());
}
};
webClient.UploadStringAsync(_baseAddress, "POST", ss);
}
catch (Exception ex)
{
ErrorTraceListener.WriteException(ex.ToString());
}
}

注意url我寫在了配置文檔里面讀取的。

 

服務器端的代碼

的線建立一個網站,網站的物理地址指向新建的接口。

通過接口去實現存儲數據。

接口里面的代碼

string value = "";
try
{
//獲取從Silverlight客戶端傳來的信息
//int length = context.Request.ContentLength;
//byte[] bytes = context.Request.BinaryRead(length);
//string txtContent = Encoding.Default.GetString(bytes);

string rel = "";
byte[] byts = new byte[System.Web.HttpContext.Current.Request.InputStream.Length];
//rel = "hello world123";

System.Web.HttpContext.Current.Request.InputStream.Read(byts, 0, byts.Length);
value = Encoding.UTF8.GetString(byts);

ActionLog.WriteLog(value, "", "", "", "");
if (value != null && value.Length > 0)
{
DataBlock datablock = (DataBlock)JsonConvert.DeserializeObject(value, typeof(DataBlock));
datablock.Data = HttpUtility.UrlDecode(datablock.Data, Encoding.Unicode);
if (datablock.Data != null)
{
ActionLog.WriteLog(datablock.Data, "", "", "", "");

DataBase db = new DataBase();
rel = db.Save(datablock);
}
else
{

ErrorLog.WriteLog("服務器接受數據失敗:Data數據為NULL");
}
}
else
{
rel = "hello world";
//ErrorLog.WriteLog("服務器接受數據失敗:Request數據為NULL");
}
//從服務器端返回信息
context.Response.ContentType = "text/plain";
context.Response.Write(rel);
}
catch (Exception e)
{
ErrorLog.WriteLog("服務器接受數據失敗:" + e.ToString());
}
}

public bool IsReusable {
get {
return false;
}
}

里面的save方法主要是對數據進行存儲的操作。一般服務器數據庫里面都會多一條數據去存儲本地服務器的id.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM