首页 > 代码库 > MYSQL Socket 增删改差之增
MYSQL Socket 增删改差之增
1.像服务器数据库发送消息,添加数据
//WPF端的代码:
void SaveData()//房体是wpf上传的
{
if (fileSaveName == null || fileSaveName == "")
{
saveName savename = new saveName();
bool? saveState = savename.ShowDialog();
if (saveState == true)
{
fileSaveName = savename.text;
if (fileSaveName == null || fileSaveName == "")
{
PopSure popsure = new PopSure();
popsure.Label_Tip.Content = "请输入名字";
popsure.ShowDialog();
SaveData();
return;
}
}
else
{
saveDataState = false;
return;
}
}
var json = edit.GetLayoutJson();//房体的信息
UserData.Data["loadLayout"] = json;
if (System.IO.Directory.Exists("tempfile") == false)
{
System.IO.Directory.CreateDirectory("tempfile");
}
var bs = System.Text.Encoding.UTF8.GetBytes(json.ToString());
var hash = Sha1.CalcSHA1String(bs);
string filename = "tempfile\\" + hash + ".layout.txt";
System.IO.File.WriteAllBytes(filename, bs);
UserData.Data["HouseLayoutIndex"] = new MyJson.JsonNode_ValueString(hash);
System.Collections.Specialized.NameValueCollection dict = new System.Collections.Specialized.NameValueCollection();
dict["token"] = UserData.Data.asDict()["token"].AsString();
dict["userName"] = UserData.Data["userName"].AsString();
string url = UserData.BaseUrl + "homedecor/useruploadfile.aspx";//用户上传的接口
//上传2D房体资源
var taskLayout = HttpMgr.Task.Create(url, HttpMgr.Method.POST_File, HttpMgr.ReturnType.TEXT, dict,
filename,
(s, e) =>
{
if (e is Exception)
{
System.Windows.Forms.MessageBox.Show((e as Exception).Message + s.url);
}
else if (e is string)
{
//上传成功不用管
}
}
);
HttpMgr.Instance.GetLayer(0).QueueTask(taskLayout);
//上传2D房体缩略图
string[] picfile = this.edit.SaveJpeg();
if (picfile == null)
{
return;
}
filename = picfile[0];
UserData.Data["HouseLayoutPic"] = new MyJson.JsonNode_ValueString(picfile[1]);//图片哈希
taskLayout = HttpMgr.Task.Create(url, HttpMgr.Method.POST_File, HttpMgr.ReturnType.TEXT, dict,
filename,
(s, e) =>
{
if (e is Exception)
{
System.Windows.Forms.MessageBox.Show((e as Exception).Message);
}
else if (e is string)
{
//上传成功不用管
}
}
);
HttpMgr.Instance.GetLayer(0).QueueTask(taskLayout);
//添加数据库记录
dict = new System.Collections.Specialized.NameValueCollection();
dict["token"] = UserData.Data.asDict()["token"].ToString();
dict["id"] = UserData.Data.asDict()["HouseLayoutID"].ToString();
dict["name"] = fileSaveName;
dict["index"] = UserData.Data.asDict()["HouseLayoutIndex"].ToString() + ".layout.txt";
dict["pic"] = UserData.Data.asDict()["HouseLayoutPic"].ToString();
dict["layers"] = UserData.Data.asDict()["layers"].ToString();
if (editType == EditType.insert)
{
url = UserData.BaseUrl + "homedecor/houseLayoutAdd.aspx";
}
else if (editType == EditType.update)
{
url = UserData.BaseUrl + "homedecor/houselayoutupdate.aspx";
}
var task = HttpMgr.Task.Create(url,
HttpMgr.Method.GET,//提交类型
HttpMgr.ReturnType.TEXT,//返回类型
dict,//参数
null,//文件名,仅postfile 模式才有用
SaveDataCallBack//回调
);
HttpMgr.Instance.GetLayer(0).QueueTask(task);
}
void SaveDataCallBack(HttpMgr.Task task, object data)
{
if (data is Exception)
{
System.Windows.MessageBox.Show(data.ToString());
return;
}
try
{
var jsonData = http://www.mamicode.com/MyJson.Parse(data as string);
var state = jsonData.asDict()["state"].ToString();
if (state == "1")
{
if (editType == EditType.insert)
{
editType = EditType.update;
UserData.Data.asDict()["HouseLayoutID"] = jsonData.asDict()["id"];
}
suffixName = 0;
}
else
{
var message = jsonData.asDict()["msg"].ToString();
if(message=="name repeat") //重名自动后缀保存
{
string name = "";
name = fileSaveName + "__" + suffixName.ToString();
task.paramlist["name"] = name;
HttpMgr.Instance.GetLayer(0).QueueTask(task);
suffixName++;
}
else
{
MessageBox.Show("保存失败!详细:" + data.ToString());
}
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
//服务器端的代码
using System;
using MySql.Data.MySqlClient;
namespace homeDecor
{
public partial class houseLayoutAdd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
string token = Request.Params["token"];
string name = Request.Params["name"];
string index = Request.Params["index"];
string layers = Request.Params["layers"];
string pic = Request.Params["pic"];
if (token != null && name != null && index != null && pic != null && layers != null)
{
userStatusCheck userstatuscheck = new userStatusCheck(token);
int userid = userstatuscheck.userState[0];
int isAdmin = userstatuscheck.userState[1];
if (userid != 0)
{
string sqlcmdRepeat = "select count(*) as num from houseLayout where name = ‘" + name + "‘ and uid = " + userid;
using (MySqlConnection myconn = dbhelper.getMySqlCon())
{
MySqlCommand mySqlCommand = dbhelper.getSqlCommand(sqlcmdRepeat, myconn);
myconn.Open();
MySqlDataReader read = mySqlCommand.ExecuteReader();
if (read.Read())
{
int num = int.Parse(read["num"].ToString());
if (num > 0)
{
//重命名了
json.SetDictValue("state", -1);
json.SetDictValue("msg", "name repeat");
Response.Write(json.ToString());
return;
}
}
}
string sql = "insert into houseLayout(`createtime`,`name`,`index`,`uid`,`pic`,`layers`,`flg`) values(now(),‘" + name + "‘,‘" + index + "‘,‘" + userid + "‘,‘" + pic + "‘,‘" + layers + "‘,0)";
using (MySqlConnection myconn = dbhelper.getMySqlCon())
{
int id = -1;
MySqlCommand mySqlCommand = dbhelper.getSqlCommand(sql, myconn);
myconn.Open();
if (dbhelper.getUpdate(mySqlCommand) > 0)
{
mySqlCommand = dbhelper.getSqlCommand("SELECT LAST_INSERT_ID()", myconn);
MySqlDataReader reader = mySqlCommand.ExecuteReader();
while (reader.Read())
{
if (reader.HasRows)
{
id = int.Parse(reader["LAST_INSERT_ID()"].ToString());
}
}
json.asDict()["state"] = new MyJson.JsonNode_ValueNumber(1);
json.asDict()["id"] = new MyJson.JsonNode_ValueNumber(id);
}
else
{
json.asDict()["state"] = new MyJson.JsonNode_ValueNumber(-1);
json.asDict()["msg"] = new MyJson.JsonNode_ValueString("houselayout add error");
}
}
}
else
{
json.asDict()["state"] = new MyJson.JsonNode_ValueNumber(-1);
json.asDict()["msg"] = new MyJson.JsonNode_ValueString("user or password error");
}
}
else
{
json.asDict()["state"] = new MyJson.JsonNode_ValueNumber(-1);
json.asDict()["msg"] = new MyJson.JsonNode_ValueString("need token,name,index");
}
Response.Write(json.ToString());
}
}
}
MYSQL Socket 增删改差之增