首页 > 代码库 > 简单的后台数据和前台数据交互.net
简单的后台数据和前台数据交互.net
最近忙着做POS项目,心血来来潮写了点小项目。
更具要求是随机显示数据并且产生的数据是可以控制的。前台交互显示能够倒叙,切每次只显示一条,页面不能超过20条超过的部分做删除。
我先展示一下前台的代码,比较简单适合初学者。
因为时间问题,这里采用的是Dbfirst
因为不能让加载页面的时候出现空数据所以在加载的时候会初始化数据到页面上
然后是ajax调用的json数据
页面的ajax
前台大概就是这样
至于后台用的是winform
我直接贴出初始化数据库的代码
using PCDD.Service; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace PCDD { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { string sql = string.Empty; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); sql = "create database if not exists pcdd"; MySqlHelper.exc(sql); string path = AppDomain.CurrentDomain.BaseDirectory + @"Script\"; sql = "select count(1) from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA=‘pcdd‘"; var obj = MySqlHelper.ExNone(sql); #region 判断数据是否存在 不存在初始化 if (obj.ToString().Equals("0")) { var i = MySqlHelper.InitData(path); Login lo = new Login(); lo.ShowDialog(); if (lo.DialogResult == DialogResult.OK) { Application.Run(new CRM()); } else { return; } } else { Login lo = new Login(); lo.ShowDialog(); if (lo.DialogResult == DialogResult.OK) { Application.Run(new CRM()); } else { return; } } #endregion } } }
MySqlhelper类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MySql.Data.MySqlClient; using System.Configuration; using System.Data; using System.IO; namespace PCDD.Service { public class MySqlHelper { #region field private static readonly string constr = ConfigurationManager.ConnectionStrings["mysql"].ToString(); private static readonly string cstr = ConfigurationManager.ConnectionStrings["mysql1"].ToString(); private static MySqlConnection con = new MySqlConnection(constr); private static MySqlCommand cmd = new MySqlCommand(); #endregion #region PCDD /// <summary> /// 查询Datatable /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataTable Query(string sql) { Open(); State(sql); DataTable dt = new DataTable(); MySqlDataAdapter da = new MySqlDataAdapter(cmd); da.Fill(dt); Close(); return dt; } /// <summary> /// 返回None /// </summary> /// <param name="sql"></param> /// <returns></returns> public static object ExNone(string sql) { Open(); State(sql); var x = cmd.ExecuteScalar(); Close(); return x; } /// <summary> /// 执行删除 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int ExDelete(string sql) { Open(); State(sql); var x = cmd.ExecuteNonQuery(); Close(); return x; } public static void State(string sql) { cmd.Connection = con; cmd.CommandText = sql; } public static void Open() { con.Open(); } public static void Close() { con.Close(); } /// <summary> /// 初始化数据 /// </summary> /// <param name="path"></param> /// <returns></returns> public static int InitData(string path) { try { int i; DirectoryInfo di = new DirectoryInfo(path); foreach (FileInfo fo in di.GetFiles()) { if (fo.Name.ToUpper().EndsWith("SQL")) { var st = fo.OpenText(); var sql = st.ReadToEnd(); st.Close(); Open(); State(sql); cmd.ExecuteNonQuery(); var filename = (fo.Name).Split(‘.‘)[0]; State(filename); i = cmd.ExecuteNonQuery(); Close(); return i; } } return -1; } catch (Exception) { throw; } } #endregion #region 数据库不存在 public static int exc(string sql) { using (MySqlConnection mcon = new MySqlConnection(cstr)) { mcon.Open(); MySqlCommand com = new MySqlCommand(sql,mcon); int i = com.ExecuteNonQuery(); mcon.Close(); return i; } } #endregion } }
后台管理的代码如下:
using PCDD.Service; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace PCDD { public partial class CRM : Form { private string sql = string.Empty; private int no = 0; #region 初始化 public CRM() { InitializeComponent(); } private void CRM_Load(object sender, EventArgs e) { l13.Text = "当前用户:" + pojo.UserName; } #endregion #region 自动模式 /// <summary> /// 定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tm_Tick(object sender, EventArgs e) { sql = "INSERT INTO pc_module(Non,Num1,Num2,Num3,Snum,SmallSingle,BigSingle,SmallBoth,BigBoth,ModifyTime)VALUES(‘{0}‘,{1},{2},{3},{4},‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘) "; var x = GenerateRandom(10, 4); t1.Text = x[1].ToString(); t2.Text = x[2].ToString(); t3.Text = x[3].ToString(); t4.Text = (x[1] + x[2] + x[3]).ToString(); var sum = x[1] + x[2] + x[3]; if (sum % 2 == 0) { if (sum >= 14) { t5.Text = string.Empty; t6.Text = string.Empty; t7.Text = string.Empty; t8.Text = string.Empty; t8.Text = "大双"; } else { t5.Text = string.Empty; t6.Text = string.Empty; t7.Text = string.Empty; t8.Text = string.Empty; t7.Text = "小双"; } } else { if (sum >= 15) { t5.Text = string.Empty; t6.Text = string.Empty; t7.Text = string.Empty; t8.Text = string.Empty; t6.Text = "大单"; } else { t5.Text = string.Empty; t6.Text = string.Empty; t7.Text = string.Empty; t8.Text = string.Empty; t5.Text = "小单"; } } if (no == 0) { no = 819000; no++; } else { no++; } sql = string.Format(sql, no, x[1].ToString(), x[2].ToString(), x[3].ToString(), sum.ToString(), t5.Text.ToString(), t6.Text.ToString(), t7.Text.ToString(), t8.Text.ToString(), Convert.ToDateTime(DateTime.Now)); MySqlHelper.ExNone(sql); } /// <summary> /// 产生随机数 /// </summary> /// <param name="iMax">最大范围</param> /// <param name="iNum">产生的个数</param> /// <returns></returns> List<int> GenerateRandom(int iMax, int iNum) { long lTick = DateTime.Now.Ticks; List<int> lstRet = new List<int>(); for (int i = 0; i < iNum; i++) { Random ran = new Random((int)lTick * i); int iTmp = ran.Next(iMax); lstRet.Add(iTmp); lTick += (new Random((int)lTick).Next(10)); } return lstRet; } /// <summary> /// 设置时间间隔 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void b1_Click(object sender, EventArgs e) { var bl = IsFloat(t9.Text); if (t9.Text == null && t9.Text == "") { //t9.Text = "1000";//1秒 t9.Text = "240000";//4分钟 tm.Interval = int.Parse(t9.Text); if (tm.Enabled == false) { tm.Enabled = true; l12.Text = "自动模式成功开启"; } } else if (bl == false) { t9.Text = string.Empty; } else if (Convert.ToDouble(t9.Text) != 0) { var s = Convert.ToDouble(t9.Text) * 1000; var ins = (int)s; //测试专用代码 #region 测试用 //tm.Interval = ins; //if (tm.Enabled == false) //{ // tm.Enabled = true; // l12.Text = "自动模式成功开启"; //} //else //{ // l12.Text = "自动模式成功开启"; //} #endregion #region 发布版本代码 if (ins >= 240000) { tm.Interval = ins; if (tm.Enabled == false) { tm.Enabled = true; l12.Text = "自动模式成功开启"; } else { l12.Text = "自动模式成功开启"; } } else { tm.Interval = 240000; if (tm.Enabled == false) { tm.Enabled = true; l12.Text = "对不起!你输入的值小于240(单位:秒)默认时间间隔为4分钟"; } else { l12.Text = "对不起!你输入的值小于240(单位:秒)默认时间间隔为4分钟"; } } #endregion } else { tm.Enabled = false; l12.Text = "自动模式成功关闭"; } MessageBox.Show("设置成功!"); Thread.Sleep(3000); l12.Text = ""; } #endregion #region 手动模式 /// <summary> /// 手动模式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void b2_Click(object sender, EventArgs e) { sql = "UPDATE pc_module set Num1 ={0},Num2={1},Num3 ={2},Snum ={3},SmallSingle=‘{4}‘,BigSingle=‘{5}‘,SmallBoth=‘{6}‘,BigBoth=‘{7}‘ WHERE 1 ORDER BY ModifyTime DESC LIMIT 1"; tm.Enabled = false; var s0 = t10.Text; var s1 = t11.Text; var s2 = t12.Text; if ((IsFloat(s0) == false) || (IsFloat(s1) == false) || (IsFloat(s2) == false)) { ShowMsg(); } else { if ((Convert.ToDouble(s0) >= 0 && Convert.ToDouble(s0) <= 9) && (Convert.ToDouble(s1) >= 0 && Convert.ToDouble(s1) <= 9) && (Convert.ToDouble(s2) >= 0 && Convert.ToDouble(s2) <= 9)) { var sum = Convert.ToDouble(s0) + Convert.ToDouble(s1) + Convert.ToDouble(s2); l24.Text = sum.ToString(); l25.Text = string.Empty; l26.Text = string.Empty; l27.Text = string.Empty; l28.Text = string.Empty; if (sum % 2 == 0) { if (sum >= 14) { l25.Text = string.Empty; l26.Text = string.Empty; l27.Text = string.Empty; l28.Text = string.Empty; l28.Text = "大双"; } else { l25.Text = string.Empty; l26.Text = string.Empty; l27.Text = string.Empty; l28.Text = string.Empty; l27.Text = "小双"; } } else { if (sum >= 15) { l25.Text = string.Empty; l26.Text = string.Empty; l27.Text = string.Empty; l28.Text = string.Empty; l26.Text = "大单"; } else { l25.Text = string.Empty; l26.Text = string.Empty; l27.Text = string.Empty; l28.Text = string.Empty; l25.Text = "小单"; } } sql = string.Format(sql, s0.ToString(), s1.ToString(), s2.ToString(), sum.ToString(), l25.Text.ToString(), l26.Text.ToString(), l27.Text.ToString(), l28.Text.ToString()); MySqlHelper.ExNone(sql); } else { MessageBox.Show("请输入0-9之间的数字"); } } } #endregion #region 模式切换 //自动 private void b3_Click(object sender, EventArgs e) { tm.Enabled = true; tm.Interval = 240000; //tm.Interval = 1000; MessageBox.Show("随机模式开启"); } //手动 private void b4_Click(object sender, EventArgs e) { tm.Enabled = false; //清空手动数据 l24.Text = string.Empty; l25.Text = string.Empty; l26.Text = string.Empty; l27.Text = string.Empty; l28.Text = string.Empty; //清空自动数据 t1.Text = string.Empty; t2.Text = string.Empty; t3.Text = string.Empty; t4.Text = string.Empty; t5.Text = string.Empty; t6.Text = string.Empty; t7.Text = string.Empty; t8.Text = string.Empty; t9.Text = string.Empty; t10.Text = "0"; t11.Text = "0"; t12.Text = "0"; l24.Text = "0"; l27.Text = "小双"; MessageBox.Show("手动模式开启"); } #endregion #region 基础方法 /// <summary> /// 判断是否输入为数字 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsFloat(string str) { string regextext = @"^(-?\d+)(\.\d+)?$"; Regex regex = new Regex(regextext, RegexOptions.None); return regex.IsMatch(str.Trim()); } public void ShowMsg() { MessageBox.Show("请检查输入的三个随机数是否非法!"); } #endregion #region 超级方法 private void b5_Click(object sender, EventArgs e) { sql = "delete from pc_module"; var obj = MySqlHelper.ExDelete(sql); if (obj != 0) { MessageBox.Show("已成功清空所有数据!"); } } private void b6_Click(object sender, EventArgs e) { sql = "delete from Pc_Module where ModifyTime BETWEEN ‘{0}‘ AND ‘{1}‘"; var time1 = Convert.ToDateTime(DateTime.Now.AddHours(-15)); var time2 = Convert.ToDateTime(DateTime.Now); sql = string.Format(sql, time1, time2); var res = MySqlHelper.ExDelete(sql); if (res != 0) { MessageBox.Show("已经删除当天数据!"); } } private void Non_Click(object sender, EventArgs e) { if (IsFloat(Non_text.Text)) { no = Convert.ToInt32(Non_text.Text); } else { no = 819000;//默认值 } } #endregion } }
以上仅供学习使用。目前博主正在研究DDD,欢迎大神做DDD的技术分享。
简单的后台数据和前台数据交互.net
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。