将本地数据库存储到服务器数据库

最近几天一直在学习将本地数据库上传到服务器数据库上。

作为萌新的我,终于完成了。

在学习的开始

现在程序里面将本地数据库通过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.

猜你喜欢

转载自www.cnblogs.com/xiehaha123/p/11842453.html