¶Ô±ÈÐÂÎļþ |
| | |
| | | using JiangSu.cs; |
| | | using JiangSu.Models; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Net; |
| | | using System.Net.Http; |
| | | using System.Web.Http; |
| | | |
| | | namespace JiangSu.Controllers |
| | | { |
| | | public class ImgController : ApiController |
| | | { |
| | | [HttpGet] |
| | | public List<Img> SelectByPage(string name, int pageSize = 10, int pageIndex = 1) |
| | | { |
| | | try |
| | | { |
| | | return ImgDAL.SelectByPage(name, pageSize, pageIndex); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | [HttpGet] |
| | | public Img SelectById(long id) |
| | | { |
| | | try |
| | | { |
| | | return ImgDAL.SelectById(id); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | [HttpGet] |
| | | public int DeleteByIds([FromUri] List<int> ids) |
| | | { |
| | | try |
| | | { |
| | | if (null == ids || ids.Count == 0) return 0; |
| | | |
| | | return ImgDAL.DeleteByIds(ids); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | [HttpPost] |
| | | public int Insert([FromBody] Img img) |
| | | { |
| | | try |
| | | { |
| | | if (null == img) return 0; |
| | | |
| | | return ImgDAL.Insert(img); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | [HttpPost] |
| | | public int UpdateById([FromBody] Img img) |
| | | { |
| | | try |
| | | { |
| | | if (null == img) return 0; |
| | | |
| | | return ImgDAL.UpdateById(img); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | return 0; |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | <Compile Include="App_Start\RouteConfig.cs" /> |
| | | <Compile Include="App_Start\WebApiConfig.cs" /> |
| | | <Compile Include="Controllers\HomeController.cs" /> |
| | | <Compile Include="Controllers\ImgController.cs" /> |
| | | <Compile Include="Controllers\OpController.cs" /> |
| | | <Compile Include="Controllers\ValuesController.cs" /> |
| | | <Compile Include="Controllers\GridController.cs" /> |
| | | <Compile Include="cs\GridDAL.cs" /> |
| | | <Compile Include="cs\ImgDAL.cs" /> |
| | | <Compile Include="cs\LogOut.cs" /> |
| | | <Compile Include="cs\ModelDAL.cs" /> |
| | | <Compile Include="cs\ModelHandler.cs" /> |
| | | <Compile Include="cs\PostgreHelper.cs" /> |
| | | <Compile Include="cs\SQLiteHelper.cs" /> |
| | | <Compile Include="cs\Tools.cs" /> |
| | | <Compile Include="Global.asax.cs"> |
| | | <DependentUpon>Global.asax</DependentUpon> |
| | | </Compile> |
| | | <Compile Include="Models\Grid.cs" /> |
| | | <Compile Include="Models\Img.cs" /> |
| | | <Compile Include="Models\Model.cs" /> |
| | | <Compile Include="Properties\AssemblyInfo.cs" /> |
| | | </ItemGroup> |
| | |
| | | |
| | | public string path { set; get; } |
| | | |
| | | public string json { set; get; } |
| | | |
| | | public string geom { set; get; } |
| | | } |
| | | } |
| | |
| | | <?xml version="1.0" encoding="utf-8"?> |
| | | <configuration> |
| | | <appSettings> |
| | | <add key="pgConn" value="Server=127.0.0.1;Port=5433;Database=etown;User Id=postgres;Password=postgres;"/> |
| | | </appSettings> |
| | | <configSections> |
| | | <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> |
| | | </configSections> |
| | |
| | | <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet-JiangSu-20231222105949.mdf;Initial Catalog=aspnet-JiangSu-20231222105949;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" /> |
| | | </connectionStrings> |
| | | <appSettings> |
| | | <add key="pgConn" value="Server=127.0.0.1;Port=5433;Database=etown;User Id=postgres;Password=postgres;"/> |
| | | <add key="webpages:Version" value="2.0.0.0" /> |
| | | <add key="webpages:Enabled" value="false" /> |
| | | <add key="PreserveLoginUrl" value="true" /> |
| | |
| | | public static int Insert(Grid grid) |
| | | { |
| | | string sql = "insert into grid (json) values (@json);select last_insert_rowid();"; |
| | | SQLiteParameter[] sqlParams = ModelDAL.GetParams<Grid>(sql, grid); |
| | | SQLiteParameter[] sqlParams = Tools.GetSQLiteParams<Grid>(sql, grid); |
| | | |
| | | object obj = SQLiteHelper.ExecuteScalar(sql, sqlParams); |
| | | return null == obj ? 0 : Convert.ToInt32(obj); |
| | |
| | | public static int UpdateById(Grid grid) |
| | | { |
| | | string sql = "update grid set json = @json where id = @id"; |
| | | SQLiteParameter[] sqlParams = ModelDAL.GetParams<Grid>(sql, grid); |
| | | SQLiteParameter[] sqlParams = Tools.GetSQLiteParams<Grid>(sql, grid); |
| | | |
| | | return SQLiteHelper.ExecuteNonQuery(sql, sqlParams); |
| | | } |
| | |
| | | using System; |
| | | using JiangSu.Models; |
| | | using Npgsql; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Data; |
| | | using System.Data.Common; |
| | | using System.Linq; |
| | | using System.Web; |
| | | |
| | |
| | | { |
| | | public class ImgDAL |
| | | { |
| | | public static List<Img> SelectByPage(string name, int pageSize = 10, int pageIndex = 1) |
| | | { |
| | | bool flag = string.IsNullOrWhiteSpace(name); |
| | | string sql = string.Format("select id, name, path, json, st_astext(geom) geom from sd.img {0} order by id limit {1} offset {2}", flag ? "" : "where upper(name) like @name", pageSize, pageSize * (pageIndex - 1)); |
| | | |
| | | DataTable dt = flag ? Tools.PGHelper.GetDataTable(sql) : Tools.PGHelper.GetDataTable(sql, new NpgsqlParameter("@name", "%" + name.Trim().ToUpper() + "%")); |
| | | if (null == dt || dt.Rows.Count == 0) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | List<Img> list = ModelHandler.FillModel<Img>(dt); |
| | | |
| | | return list; |
| | | } |
| | | |
| | | public static Img SelectById(long id) |
| | | { |
| | | string sql = "select id, name, path, json, st_astext(geom) geom from sd.img where id = @id"; |
| | | |
| | | DbParameter param = new NpgsqlParameter("@id", id); |
| | | |
| | | DataTable dt = Tools.PGHelper.GetDataTable(sql, param); |
| | | if (null == dt || dt.Rows.Count == 0) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | List<Img> list = ModelHandler.FillModel<Img>(dt); |
| | | |
| | | return null == list || list.Count == 0 ? null : list[0]; |
| | | } |
| | | |
| | | public static int DeleteByIds(List<int> ids) |
| | | { |
| | | string str = string.Join(",", ids.ToArray()); |
| | | string sql = string.Format("delete from sd.img where id in ({0})", str); |
| | | |
| | | return Tools.PGHelper.ExecuteNonQuery(sql); |
| | | } |
| | | |
| | | public static int Insert(Img img) |
| | | { |
| | | string geom = string.IsNullOrWhiteSpace(img.geom) ? "null" : string.Format("ST_GeomFromText('{0}')", img.geom); |
| | | string sql = string.Format("insert into sd.img (name, path, json, geom) values (@name, @path, @json, {0}) returning id", geom); |
| | | DbParameter[] sqlParams = Tools.GetPGParams<Img>(sql, img); |
| | | |
| | | object obj = Tools.PGHelper.GetScalar(sql, sqlParams); |
| | | |
| | | return null == obj ? 0 : Convert.ToInt32(obj); |
| | | } |
| | | |
| | | public static int UpdateById(Img img) |
| | | { |
| | | string geom = string.IsNullOrWhiteSpace(img.geom) ? "" : string.Format(", geom = ST_GeomFromText('{0}')", img.geom); |
| | | string sql = string.Format("update sd.img set name = @name, path = @path, json = @json{0} where id = @id", geom); |
| | | DbParameter[] sqlParams = Tools.GetPGParams<Img>(sql, img); |
| | | |
| | | return Tools.PGHelper.ExecuteNonQuery(sql, sqlParams); |
| | | } |
| | | } |
| | | } |
| | |
| | | { |
| | | public class ModelDAL |
| | | { |
| | | /// <summary> |
| | | /// è·ååæ°å表 |
| | | /// </summary> |
| | | public static SQLiteParameter[] GetParams<T>(string sql, T t) |
| | | { |
| | | List<SQLiteParameter> list = new List<SQLiteParameter>(); |
| | | Type tType = typeof(T); |
| | | BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance; |
| | | |
| | | int start = sql.IndexOf("@"); |
| | | while (start != -1) |
| | | { |
| | | int end = sql.IndexOf(",", start); |
| | | if (end == -1) end = sql.IndexOf(")", start); |
| | | if (end == -1) end = sql.IndexOf(" ", start); |
| | | if (end == -1) end = sql.Length; |
| | | |
| | | string name = sql.Substring(start + 1, end - start - 1); |
| | | PropertyInfo pi = tType.GetProperty(name, flags); |
| | | if (pi != null) |
| | | { |
| | | object value = pi.GetValue(t, null); |
| | | SQLiteParameter dp = new SQLiteParameter("@" + name, value); |
| | | list.Add(dp); |
| | | } |
| | | |
| | | start = sql.IndexOf("@", end); |
| | | } |
| | | |
| | | return list.ToArray(); |
| | | } |
| | | |
| | | public static List<Model> SelectByPage(string name, int pageSize = 10, int pageIndex = 1) |
| | | { |
| | | bool flag = string.IsNullOrWhiteSpace(name); |
| | |
| | | public static int Insert(Model model) |
| | | { |
| | | string sql = "insert into model (name, json) values (@name, @json);select last_insert_rowid();"; |
| | | SQLiteParameter[] sqlParams = GetParams<Model>(sql, model); |
| | | SQLiteParameter[] sqlParams = Tools.GetSQLiteParams<Model>(sql, model); |
| | | |
| | | //return SQLiteHelper.ExecuteNonQuery(sql, sqlParams); |
| | | |
| | |
| | | public static int UpdateById(Model model) |
| | | { |
| | | string sql = "update model set name = @name, json = @json where id = @id"; |
| | | SQLiteParameter[] sqlParams = GetParams<Model>(sql, model); |
| | | SQLiteParameter[] sqlParams = Tools.GetSQLiteParams<Model>(sql, model); |
| | | |
| | | return SQLiteHelper.ExecuteNonQuery(sql, sqlParams); |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | using Npgsql; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Data.Common; |
| | | using System.Data.SQLite; |
| | | using System.Linq; |
| | | using System.Reflection; |
| | | using System.Web; |
| | | |
| | | namespace JiangSu.cs |
| | | { |
| | | public class Tools |
| | | { |
| | | private static PostgreHelper _pgHelper; |
| | | |
| | | /// <summary> |
| | | /// DB帮å©ç±» |
| | | /// </summary> |
| | | public static PostgreHelper PGHelper |
| | | { |
| | | get |
| | | { |
| | | if (null == _pgHelper) |
| | | { |
| | | _pgHelper = new PostgreHelper(); |
| | | } |
| | | |
| | | return _pgHelper; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// è·ååæ°å表 |
| | | /// </summary> |
| | | public static DbParameter[] GetPGParams<T>(string sql, T t) |
| | | { |
| | | List<DbParameter> list = new List<DbParameter>(); |
| | | Type tType = typeof(T); |
| | | BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance; |
| | | |
| | | int start = sql.IndexOf("@"); |
| | | while (start != -1) |
| | | { |
| | | int end = sql.IndexOf(",", start); |
| | | if (end == -1) end = sql.IndexOf(")", start); |
| | | if (end == -1) end = sql.IndexOf(" ", start); |
| | | if (end == -1) end = sql.Length; |
| | | |
| | | string name = sql.Substring(start + 1, end - start - 1); |
| | | PropertyInfo pi = tType.GetProperty(name, flags); |
| | | if (pi != null) |
| | | { |
| | | object value = pi.GetValue(t, null); |
| | | NpgsqlParameter dp = new NpgsqlParameter("@" + name, value); |
| | | list.Add(dp); |
| | | } |
| | | |
| | | start = sql.IndexOf("@", end); |
| | | } |
| | | |
| | | return list.ToArray(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// è·ååæ°å表 |
| | | /// </summary> |
| | | public static SQLiteParameter[] GetSQLiteParams<T>(string sql, T t) |
| | | { |
| | | List<SQLiteParameter> list = new List<SQLiteParameter>(); |
| | | Type tType = typeof(T); |
| | | BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance; |
| | | |
| | | int start = sql.IndexOf("@"); |
| | | while (start != -1) |
| | | { |
| | | int end = sql.IndexOf(",", start); |
| | | if (end == -1) end = sql.IndexOf(")", start); |
| | | if (end == -1) end = sql.IndexOf(" ", start); |
| | | if (end == -1) end = sql.Length; |
| | | |
| | | string name = sql.Substring(start + 1, end - start - 1); |
| | | PropertyInfo pi = tType.GetProperty(name, flags); |
| | | if (pi != null) |
| | | { |
| | | object value = pi.GetValue(t, null); |
| | | SQLiteParameter dp = new SQLiteParameter("@" + name, value); |
| | | list.Add(dp); |
| | | } |
| | | |
| | | start = sql.IndexOf("@", end); |
| | | } |
| | | |
| | | return list.ToArray(); |
| | | } |
| | | } |
| | | } |
| | |
| | | return JSON.stringify(model); |
| | | } |
| | | |
| | | function InsertImg() { |
| | | var data = getImg(); |
| | | |
| | | ajax("Img/Insert", "POST", data, null, null, function (rs) { |
| | | console.log(rs); |
| | | alert(rs); |
| | | }); |
| | | } |
| | | |
| | | function UpdateImgById() { |
| | | var data = getImg(); |
| | | |
| | | ajax("Img/UpdateById", "POST", data, null, null, function (rs) { |
| | | console.log(rs); |
| | | alert(rs); |
| | | }); |
| | | } |
| | | |
| | | function getImg() { |
| | | var model = { |
| | | id: $("#iid").val(), |
| | | name: $("#iname").val(), |
| | | path: $("#ipath").val(), |
| | | json: $("#ijson").val() |
| | | }; |
| | | |
| | | return JSON.stringify(model); |
| | | } |
| | | |
| | | function ajax(url, type, data, dataType, contentType, fn) { |
| | | $.ajax({ |
| | | url: url, |
| | |
| | | json: <textarea id="gjson" style="width: 1024px;height: 70px;">{ "type": "Feature", "properties": { "ID": "12", "NAME": "Area8", "REMARK": "" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 120.24179180542139, 32.005615085853492, 13.770 ], [ 120.24175240823769, 32.005610442630996, 13.770 ], [ 120.24173428512847, 32.005722106674966, 13.770 ], [ 120.24177368235908, 32.005726749902777, 13.770 ], [ 120.24179180542139, 32.005615085853492, 13.770 ] ] ] } }</textarea> <br /> |
| | | <input type="button" value="InsertGrid" onclick="InsertGrid();" /> |
| | | <input type="button" value="UpdateGridById" onclick="UpdateGridById();" /><br /> <br /> |
| | | |
| | | |
| | | å¾çï¼<a href="http://localhost/JiangSu/Img/SelectByPage?pageSize=10&pageIndex=1&name=">SelectByPage?pageSize=10&pageIndex=1&name=</a> <span style="width: 50px;"> </span> |
| | | <a href="http://localhost/JiangSu/Img/SelectById?id=1">SelectById?id=1</a> <span style="width: 50px;"> </span> |
| | | <a href="http://localhost/JiangSu/Img/DeleteByIds?ids=3&ids=0">DeleteByIds?ids=3&ids=0</a> <br /> |
| | | |
| | | id: <input id="iid" value="1" /> <br /> |
| | | name: <input id="iname" value="1.jpg"/> <br /> |
| | | path: <input id="ipath" value="Images\1.jpg"/> <br /> |
| | | json: <input id="ijson" value="{}"/> <br /> |
| | | <input type="button" value="InsertImg" onclick="InsertImg();" /> |
| | | <input type="button" value="UpdateImgById" onclick="UpdateImgById();" /><br /> <br /> |
| | | </form> |
| | | </body> |
| | | </html> |