webservice与mysql数据库_WebService实现数据库操作

1.数据库连接:

[WebMethod]public string InsertPoint(stringdata)

{//解决跨域问题//Context.Response.AddHeader("Access-Control-Allow-Origin", "*");

string database = "Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=password1633";//string data = HttpContext.Current.Request["data"];

try{if (data.Equals("") || data == null)

{return "data is null";

}else{

OverlaysPoint opoint= JsonConvert.DeserializeObject(data);// 将接收的参数data实例化为一个对象(方便操作),注意该类的变量名要和json的key值对应/*foreach (Data_Opoint dtpoint in opoint.overlays)

{

sql = "insert into Map_Overlays(wf_pictureid,wf_picturename,wf_railid,wf_name,wf_location,wf_createtime,wf_pid,wf_lng,wf_lat) values(‘" + "123456789" + "‘,‘" + opoint.picturename + "‘,‘" + opoint.id + "‘,‘" + opoint.name + "‘,‘" + opoint.location + "‘,‘" + opoint.createime + "‘,‘" + dtpoint.pid + "‘,‘" + dtpoint.lng.ToString() + "‘,‘" + dtpoint.lat.ToString() + "‘)";

ExecuteUpdate(sql, database);

}*/

string[] xyArray = opoint.position.Split(new string[] { "(", ",", ")"}, StringSplitOptions.RemoveEmptyEntries); //处理json字符串

//执行sql语句string sql = "insert into map_img_points(miid,pointtype,pointid, pointcode, positionx, positiony, pointinfo) values (‘地图id‘, ‘" + opoint.point_type + "‘, ‘" + opoint.point_bm + "‘,‘" + opoint.point_code + "‘,‘" + xyArray[0] + "‘,‘" + xyArray[1] + "‘, ‘" + opoint.point_info + "‘)";

executeUpdate(sql, database);return "success";

}

}catch(NullReferenceException e)

{returne.StackTrace;

}

}

Data Source :  一个点默认为本地数据库,其他数据库填写相应ip ;

Initial Catelog:是目标数据库名称

我们接收一个json字符串,将其value填入一个实例化的类对象,通过这个类的实例化对象来使用它

OverlaysPoint opoint = JsonConvert.DeserializeObject(data);

OverPoint类:

public classOverlaysPoint

{public stringpoint_type;public stringpoint_bm;public stringpoint_code;public stringposition;public stringpoint_info;public stringcon_bussiness;public stringcon_entity;public stringcon_fields;public stringshowpic;public stringshowtext;public stringshowdata;public stringcreate_time;public stringdata_refresh_rate;

}

executeQuery和executeUpdate方法:

//对数据库操作//用于查询

private DataTable executeQuery(string sqlStr, stringsqlCon)

{

SqlConnection con= newSqlConnection(@sqlCon);

con.Open();

SqlCommand cmd= newSqlCommand();

cmd.Connection=con;

cmd.CommandType=CommandType.Text;

cmd.CommandText=sqlStr;

DataTable dt= new DataTable(); //实例化数据表

SqlDataAdapter msda;

msda= new SqlDataAdapter(cmd); //实例化适配器

msda.Fill(dt); //保存数据

con.Close();returndt;

}//用于增删改;

private int executeUpdate(string sqlStr, stringsqlCon)

{

SqlConnection con= newSqlConnection(@sqlCon);

con.Open();

SqlCommand cmd= newSqlCommand();

cmd.Connection=con;

cmd.CommandType=CommandType.Text;

cmd.CommandText=sqlStr;int iud = 0;

iud=cmd.ExecuteNonQuery();

con.Close();returniud;

}

解决跨域问题:

b5a0b377e8d3ebd6022f4f71ea3b733f.png

2.ajax调用:

var jsonOverlays = JSON.stringify(data.field).replace(/\"/g,"‘");// 将双引号替换为单引号

$.ajax({

type:"POST",

contentType:"application/json; charset=utf-8",

url:"http://192.168.21.1:7777/WebService1.asmx/test",

data:"{\"data\":\"" + jsonOverlays + "\"}", //这是post请求要传递的参数

dataType: ‘json‘, //预期返回类型

success: function(result){ //回调函数

layer.msg(‘ajax调用成功‘ +result.d);

console.log(result.d);

},

error: function (data) {//200的响应有可能被认为error, responseText中没有Message部分

alert(‘error‘);

},

complete: function (data) {

;//after success ot error

}

});

注意2处:1.双引号替换为单引号  2.构造data的参数

2019-1-22测试无误:

fdf5b3eb18ff1ff97b85e8b8e9fb022d.png

如果出错,注意看浏览器端控制台报错(ajax出错)和vs输出窗口(c#方法出错)

原文:https://www.cnblogs.com/toheart/p/10306386.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值