public tableinfo Post([FromBody] tablename req) { tableinfo resp = new tableinfo(); try { //从配置文件中读取字符串 Json_File _Json_File = new Json_File(); var configuration = _Json_File.Read_Json_File(); string connString = configuration["conn"]; MySqlConnection conn = new MySqlConnection(connString); using (MySqlCommand cmd = new MySqlCommand())//创建查询命令 { string sql = "select * from " + req.table; cmd.Connection = conn; cmd.CommandText = sql; //cmd.Parameters.Add(new MySqlParameter("@tablename",req.table));表名不能进行参数化查询 MySqlDataAdapter reader = new MySqlDataAdapter(cmd); //创建一个执行命令的适配器对象 DataSet ds = new DataSet(); //相当于建立一个基于前台的虚拟数据库 DataTable dtable; //相当于数据库中的一张数据表 DataRowCollection coldrow; //相当于表中行的集合 DataRow drow; //相当于一行中列的数据集合 reader.Fill(ds, "ds"); //将查询的结果存储到虚拟数据库ds的虚拟表student中 dtable = ds.Tables["ds"]; //将数据表student的数据复制到DataTable对象(取库中的一张数据表) coldrow = dtable.Rows; //获取数据表中的所有行 int len = coldrow[0].ItemArray.Length; //获取一行有多少字段 resp.countofcolumns = len; //数据表列数 resp.countofrows = coldrow.Count; //数据表行数 resp.tableheader = new string[0]; for (int i = 0; i < dtable.Columns.Count; i++) { List <string> b = resp.tableheader.ToList(); b.Add(dtable.Columns[i].ColumnName); resp.tableheader = b.ToArray(); } resp.tableinformation = new List <List <string> >(); for (int inti = 0; inti < coldrow.Count; inti++) { drow = coldrow[inti]; List <string> temp = new List <string>(); for (int j = 0; j < len; j++) { temp.Add(drow[j].ToString()); } resp.tableinformation.Add(temp); } } } catch (Exception ex) { Console.WriteLine(ex); } return(resp); }
//public List<booking> GetDayBooking(DateTime bookingDateRequest) //{ // var response = new List<booking>(); // response.Add(new booking() { BookingId = "1", FirstName = "Vikas", LastName = "Sethia", BookingDate = DateTime.Now, PhoneNumber = "0789456123", NumberOfGuests = 2, StartTime = new TimeSpan(12, 30, 00), bookedtables = getTables(new List<int>() { 1 }) }); // response.Add(new booking() { BookingId = "1", FirstName = "Martin", LastName = "Karlsson", BookingDate = DateTime.Now, PhoneNumber = "0789456123", NumberOfGuests = 2, StartTime = new TimeSpan(12, 00, 00), bookedtables = getTables(new List<int>() { 2 }) }); // response.Add(new booking() { BookingId = "1", FirstName = "Jonas", LastName = "Wu", BookingDate = DateTime.Now, PhoneNumber = "0789456123", NumberOfGuests = 4, StartTime = new TimeSpan(12, 30, 00), bookedtables = getTables(new List<int>() { 3, 4 }) }); // return response; //} //private List<bookedtable> getTables(List<int> tableNumbers) //{ // var response = new List<bookedtable>(); // foreach (var item in tableNumbers) // { // response.Add(new bookedtable() { TableNumber = item, BookingId = "1" }); // } // return response; //} public void AddUpdateTable(TableInfoEntity tableRequest) { var newTableDetails = new tableinfo() { IsDeleted = false, Capacity = tableRequest.Capacity, IsBookable = tableRequest.IsBookable, Xposition = tableRequest.Xposition, Yposition = tableRequest.Yposition, TableNumber = tableRequest.TableNumber, TableName = tableRequest.TableName, ShapeId = tableRequest.ShapeId, CustomerId = _customerId }; _dataAccess.AddUpdateTable(newTableDetails); }
public void AddUpdateTable(tableinfo tableRequest) { var table = _context.tableinfoes.FirstOrDefault(t => t.TableNumber == tableRequest.TableNumber && t.CustomerId.Equals(_customerId)); if (table == null) { _context.tableinfoes.Add(tableRequest); } else { table.TableName = tableRequest.TableName ?? table.TableName; table.Xposition = tableRequest.Xposition ?? table.Xposition; table.Yposition = tableRequest.Yposition ?? table.Yposition; table.ShapeId = tableRequest.ShapeId ?? table.ShapeId; table.IsBookable = tableRequest.IsBookable; table.Capacity = tableRequest.Capacity; table.IsDeleted = false; table.CustomerId = _customerId; } _context.SaveChanges(); }
public tableinfo Get(string table) { tableinfo resp = new tableinfo(); try { string ip = Request.Headers["X-Forwarded-For"].FirstOrDefault(); if (string.IsNullOrEmpty(ip)) { ip = Request.HttpContext.Connection.RemoteIpAddress.ToString(); } Console.WriteLine("============================="); //使用postman调用接口,使用部署后的网站的页面调用接口,看看请求方的IP是不是同一个 //是的话可以在网站全部部署后阻止跨域ajax请求,提高安全性 Console.WriteLine(ip); Console.WriteLine("============================="); //从配置文件中读取字符串 Json_File _Json_File = new Json_File(); var configuration = _Json_File.Read_Json_File(); string connString = configuration["conn"]; MySqlConnection conn = new MySqlConnection(connString); using (MySqlCommand cmd = new MySqlCommand())//创建查询命令 { string sql = "select * from " + table; cmd.Connection = conn; cmd.CommandText = sql; //cmd.Parameters.Add(new MySqlParameter("@tablename",req.table));表名不能进行参数化查询 MySqlDataAdapter reader = new MySqlDataAdapter(cmd); //创建一个执行命令的适配器对象 DataSet ds = new DataSet(); //相当于建立一个基于前台的虚拟数据库 DataTable dtable; //相当于数据库中的一张数据表 DataRowCollection coldrow; //相当于表中行的集合 DataRow drow; //相当于一行中列的数据集合 reader.Fill(ds, "ds"); //将查询的结果存储到虚拟数据库ds的虚拟表student中 dtable = ds.Tables["ds"]; //将数据表student的数据复制到DataTable对象(取库中的一张数据表) coldrow = dtable.Rows; //获取数据表中的所有行 int len = coldrow[0].ItemArray.Length; //获取一行有多少字段 resp.countofcolumns = len; //数据表列数 resp.countofrows = coldrow.Count; //数据表行数 resp.tableheader = new string[0]; for (int i = 0; i < dtable.Columns.Count; i++) { List <string> b = resp.tableheader.ToList(); b.Add(dtable.Columns[i].ColumnName); resp.tableheader = b.ToArray(); } resp.tableinformation = new List <List <string> >(); for (int inti = 0; inti < coldrow.Count; inti++) { drow = coldrow[inti]; List <string> temp = new List <string>(); for (int j = 0; j < len; j++) { temp.Add(drow[j].ToString()); } resp.tableinformation.Add(temp); } } } catch (Exception ex) { Console.WriteLine(ex); } return(resp); }