基于C#和ACCESS数据库实现的水电管理信息系统

SMASHBOX

发布日期: 2018-11-05 15:46:06 浏览量: 1811
评分:
star star star star star star star star star_border star_border
*转载请注明来自write-bug.com

1 需求和规格说明

设计一个水电管理信息系统,能够对高校的水电费用进行管理,包括了登记费用,查询费用,以及住户信息管理等。在设计时要考虑到学生和教工在用水电时的不同,学生可以免费使用一定额度的水电,超过这个额度的随便以后必须自费使用,且自费部分水电费的价格标准要高于教工的收费标准(主要是节约资源)。

  • 实现对用户信息的录入

  • 实现水电煤气数据的录入

  • 计算并查询用户应缴费用

  • 查询未缴纳费用名单

2 设计

2.1 用户登陆

  • 设定一个唯一的管理员用于管理数据

  • 密码使用md5加混合方式

  • 保存于databse/ PWD.mdb

2.2 用户的录入和查询

  • 所有用户的信息放置于数据库databse/ data.mdb

  • 数据库密码zxcasdqwe

  • 应缴费用等详细显示在主界面

  • 默认的配置config.ini

  • 后台服务设置都可以再这里实现

  • 历史信息以文件的形式放入userdata

2.3 考虑到用户可能需要远程查询数据,加入网络模块

  • 考虑到可以让用户用浏览器浏览,所以协议使用tcp/ip

  • 实际使用方面抛弃了原vb的activeX控件

  • 使用.net库中的Tcpclienter和Tcplistener

2.4 设计语言采用c#(有类的概念.类C语言系)

2.5 用户信息数据库采用access,oledb方式连接

  • 保存于databse/ data.mdb

2.6 附加程序

在不使用主程序的时候可使用此程序建立后台服务,供远程用户查询数据。

类名 属性/方法 访问级别 说明
sck public 网络通信模块
listen(int ThreadNum,int port) public 使用多线程.监听本地端口
ls() private 由线程控制的函数 用于处理连接的quest并返回数据
server public 结构体
text public 用于在启动窗口和后台线程间传递服务器状态消息
ReadFile public 读取和设置文件
Existen (string path) public 判断文件是否存在
GetConfig(string path,string name) public 读取配置文件 返回设定的值
SetPwd (string path,string username,string userpwd) public 设置管理员密码
AddDetail(string path, string detail) public 添加历史记录
SetPwd(string path,string username,string userpwd) public 设置用户密码
Launcher 启动窗体
string GetMd6STR(string ConvertString) public 给字符编码(md5+混合…)
Mainform 主窗体
LoadData() public 载入数据
setdefault() public 默认值的载入
updatemoney() public 更新应缴金额
AboutBox 版权声明窗体

2.7 系统类图

3 用户手册

  • 如果无法启动,请先安装.NET framework 2.0

  • 默认账户admin,密码icyfire,请妥善保管您的密码

  • 您可以自由发布此程序,但请注明作者白忠魏(princehaku)

  • 在未获得作者本人同意下请不要尝试破解,修改此软件

  • 配置文件在config.ini;默认端口为80,线程数为10

  • BackgroundServise使用说明;此附加程序为后台服务程序,主程序已带有此功能

  • 在不使用主程序的时候可使用此程序建立后台服务,供远程用户查询数据

  • 程序接口p 端口 t线程数

  1. 例:
  2. BackgroundServise.exe /p 80 /t 10
  3. ->将在本地开启10个线程监听80端口
  4. 超过此线程用户将无法连接
  • 访问方式;浏览器输入服务器ip:服务器端口
  1. 例:
  2. 服务器::
  3. cmd{
  4. c:/>ipconfig
  5. +---------------------------------------------+
  6. IP Address. . . . . . . . . . . . : 125.70.161.182
  7. +---------------------------------------------+
  8. c:/>BackgroundServise.exe /p 80 /t 10
  9. 远程用户::
  10. 打开浏览器.
  11. 输入地址
  12. http://125.70.161.182:80
  13. 回车即可
  14. ps:服务器自己可使用http://localhost:80

4 调试及测试

4.1 本地测试

登陆系统

新增数据

保存数据

删除数据

详细记录

修改

查询

4.2 网络测试

浏览器查看

测试id为123123的用户(不存在此用户)

命令行结果

  1. GET /?id=123&button=%E6%9F%A5%E8%AF%A2 HTTP/1.1
  2. ./UserData/wegDbfile-123.rdb123无此数据

测试id为200403080326的用户(存在此用户)

命令行结果

  1. GET /?id=200403080326&button=%E6%9F%A5%E8%AF%A2 HTTP/1.1
  2. 200403080326数据发送成功

5 关键代码

5.1 主界面代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Security.Cryptography;
  9. using Readfile;
  10. namespace My_project
  11. {
  12. public partial class MainForm : Form
  13. {
  14. private bool Edit;
  15. Readfile.Readfile rf = new Readfile.Readfile();
  16. public MainForm()
  17. {
  18. InitializeComponent();
  19. }
  20. public struct cell
  21. {
  22. public static int ColumnIndex;
  23. public static int RowIndex;
  24. public static string usertype;
  25. }
  26. private void userBindingNavigatorSaveItem_Click(object sender, EventArgs e)
  27. {
  28. //保存并重置数据单元
  29. this.Validate();
  30. this.userBindingSource.EndEdit();
  31. this.tableAdapterManager.UpdateAll(this.dataDataSet);
  32. this.LoadData();
  33. this.Edit = false;
  34. }
  35. //数据加密
  36. public static string GetMd6Str(string ConvertString)
  37. {
  38. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  39. string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8).Replace("-", "B");
  40. t2 += BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8).Replace("-", "Z");
  41. t2 += BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8).Replace("-", "W");
  42. return t2;
  43. }
  44. //填充数据库
  45. public void LoadData()
  46. {
  47. this.userTableAdapter.Fill (this.dataDataSet.user);
  48. }
  49. private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
  50. {
  51. foreach (DataGridViewCell c in userDataGridView.SelectedCells)
  52. {
  53. if (c.RowIndex>=0) { this.userDataGridView.Rows.RemoveAt(c.RowIndex); }
  54. }
  55. }
  56. //设定当前单元格
  57. private void userDataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
  58. {
  59. if (userDataGridView.SelectedCells.Count == 1 & e.ColumnIndex >= 0 & e.RowIndex >= 0 & e.Button==MouseButtons .Right)
  60. {
  61. userDataGridView.CurrentCell = userDataGridView[e.ColumnIndex, e.RowIndex];
  62. }
  63. }
  64. private void 关于本程序ToolStripMenuItem_Click(object sender, EventArgs e)
  65. {
  66. AboutBox newabout= new AboutBox ();
  67. newabout.Show();
  68. }
  69. private void fwToolStripMenuItem_Click(object sender, EventArgs e)
  70. {
  71. groupBox3.Visible = true;
  72. }
  73. private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
  74. {
  75. if(this.Edit == true)
  76. {
  77. switch (MessageBox.Show("是否保存您作的修改?", "关闭程序", MessageBoxButtons.YesNo))
  78. {
  79. case DialogResult.Yes:
  80. this.Validate();
  81. this.userBindingSource.EndEdit();
  82. this.tableAdapterManager.UpdateAll(this.dataDataSet);
  83. break;
  84. }
  85. }
  86. //else
  87. {
  88. //终止所有线程并结束程序
  89. Application.Exit();
  90. }
  91. }
  92. private void hideall()
  93. {
  94. //隐藏所有可能出现的窗体
  95. groupBox2.Visible = false;
  96. button1.Visible = false;
  97. groupBox1.Visible = false;
  98. }
  99. private void userDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  100. {
  101. string[] data=new string[0];
  102. hideall();
  103. this.Edit = true;
  104. if (e.RowIndex >= 0)
  105. {
  106. try
  107. {
  108. switch (e.ColumnIndex)
  109. {
  110. //配额
  111. case 5:
  112. groupBox1.Text = "用户配额";
  113. //读取数据
  114. data = userDataGridView.CurrentCell.Value.ToString().Split('|');
  115. if (data[0] != "")
  116. {
  117. water.Text = data[0];
  118. elec.Text = data[1];
  119. gas.Text = data[2];
  120. }
  121. else
  122. {
  123. water.Text = "0";
  124. elec.Text = "0";
  125. gas.Text = "0";
  126. }
  127. cell.ColumnIndex = e.ColumnIndex;
  128. cell.RowIndex = e.RowIndex;
  129. groupBox1.Visible = true;
  130. button1.Visible = true;
  131. //用户类型
  132. cell.usertype = userDataGridView[e.ColumnIndex - 1, e.RowIndex].Value.ToString();
  133. break;
  134. //单价
  135. case 6:
  136. groupBox1.Text = "单价";
  137. //读取数据
  138. data = userDataGridView.CurrentCell.Value.ToString().Split('|');
  139. if (data[0] != "")
  140. {
  141. water.Text = data[0];
  142. elec.Text = data[1];
  143. gas.Text = data[2];
  144. }
  145. else
  146. {
  147. water.Text = "0";
  148. elec.Text = "0";
  149. gas.Text = "0";
  150. }
  151. cell.ColumnIndex = e.ColumnIndex;
  152. cell.RowIndex = e.RowIndex;
  153. groupBox1.Visible = true;
  154. button1.Visible = true;
  155. //用户类型
  156. cell.usertype = userDataGridView[e.ColumnIndex - 2, e.RowIndex].Value.ToString();
  157. break;
  158. //使用
  159. case 7:
  160. groupBox1.Text = "已经使用";
  161. //读取数据
  162. data = userDataGridView.CurrentCell.Value.ToString().Split('|');
  163. if (data[0] != "")
  164. {
  165. water.Text = data[0];
  166. elec.Text = data[1];
  167. gas.Text = data[2];
  168. }
  169. else
  170. {
  171. water.Text = "0";
  172. elec.Text = "0";
  173. gas.Text = "0";
  174. }
  175. cell.ColumnIndex = e.ColumnIndex;
  176. cell.RowIndex = e.RowIndex;
  177. groupBox1.Visible = true;
  178. //用户类型
  179. cell.usertype = userDataGridView[e.ColumnIndex - 3, e.RowIndex].Value.ToString();
  180. ; break;
  181. //缴费
  182. case 8:
  183. recent.Text = userDataGridView[9, e.RowIndex].Value.ToString();
  184. updatemoney();
  185. button1.Visible = true;
  186. groupBox2.Visible = true;
  187. //应交费的信息
  188. textBox4.Text = userDataGridView[e.ColumnIndex, e.RowIndex].Value.ToString();
  189. ; break;
  190. default: this.userDataGridView.BeginEdit(true);break;
  191. }
  192. handin.Text = "";
  193. }
  194. catch
  195. {
  196. groupBox1.Text = "";
  197. }
  198. }
  199. }
  200. private void 详情ToolStripMenuItem_Click(object sender, EventArgs e)
  201. {
  202. //详情载入
  203. //string type = userDataGridView.CurrentCellAddress.X();
  204. string userid = "wegDbfile-" + userDataGridView[1, userDataGridView.CurrentCell.RowIndex].Value.ToString();
  205. string path=Application.StartupPath + "/UserData/" + userid + ".rdb";
  206. detail de = new detail(path,userDataGridView[1, userDataGridView.CurrentCell.RowIndex].Value.ToString());
  207. de.Show();
  208. }
  209. private void 数据库导出ToolStripMenuItem_Click(object sender, EventArgs e)
  210. {
  211. saveFileDialog1.ShowDialog();
  212. if (saveFileDialog1.FileName.ToString() != "")
  213. {
  214. this.dataDataSet.WriteXml(saveFileDialog1.FileName.ToString());
  215. }
  216. }
  217. private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
  218. {
  219. toolStripSplitButton1.ShowDropDown();
  220. }
  221. private void toolStripButton1_Click(object sender, EventArgs e)
  222. {
  223. //this.Search_col.Text.Trim;
  224. //this.Search_con.Text.Trim;
  225. if (this.Search_col.Text.ToString() != "" & this.Search_con.Text.ToString() != "")
  226. {
  227. userTableAdapter.Adapter.SelectCommand.CommandText = "SELECT [user].* FROM [user] WHERE [" + this.Search_col.Text.Trim() + "] like '%" + this.Search_con.Text.Trim() + "%'";
  228. this.userTableAdapter.Fill(this.dataDataSet.user);
  229. }
  230. }
  231. private void toolStripButton2_Click(object sender, EventArgs e)
  232. {
  233. userTableAdapter.Adapter.SelectCommand.CommandText = "SELECT [user].* FROM [user]";
  234. this.userTableAdapter.Fill(this.dataDataSet.user);
  235. }
  236. private void MainForm_Load(object sender, EventArgs e)
  237. {
  238. }
  239. //限制输入数字
  240. private void water_KeyPress(object sender, KeyPressEventArgs e)
  241. {
  242. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)&& !(e.KeyChar == '.'))
  243. {
  244. e.Handled = true;
  245. }
  246. }
  247. private void elec_KeyPress(object sender, KeyPressEventArgs e)
  248. {
  249. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && !(e.KeyChar == '.'))
  250. {
  251. e.Handled = true;
  252. }
  253. }
  254. private void handin_KeyPress(object sender, KeyPressEventArgs e)
  255. {
  256. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && !(e.KeyChar == '.'))
  257. {
  258. e.Handled = true;
  259. }
  260. }
  261. private void gas_KeyPress(object sender, KeyPressEventArgs e)
  262. {
  263. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && !(e.KeyChar == '.'))
  264. {
  265. e.Handled = true;
  266. }
  267. }
  268. private void accept_Click(object sender, EventArgs e)
  269. {
  270. userDataGridView[cell.ColumnIndex, cell.RowIndex].Value = water.Text + '|' + elec.Text + "|" + gas.Text;
  271. if(groupBox1.Text=="已经使用")
  272. {
  273. //更新应交金额
  274. updatemoney();
  275. }
  276. //加入历史
  277. string userid = "wegDbfile-" + userDataGridView[1, userDataGridView.CurrentCell.RowIndex].Value.ToString();
  278. string detail = DateTime.Now.ToString() + "-----" + groupBox1.Text + "改变为" +getdata() +"<br />";
  279. rf.AddDetail(Application.StartupPath + "/UserData/" + userid + ".rdb", detail);
  280. groupBox1.Visible = false;
  281. }
  282. //得到详细改变的内容
  283. private string getdata()
  284. {
  285. switch (groupBox1.Text.ToString())
  286. {
  287. case "已经使用":
  288. return userDataGridView.CurrentCell.Value.ToString();
  289. case "用户配额":
  290. return userDataGridView.CurrentCell.Value.ToString();
  291. case "单价":
  292. return userDataGridView.CurrentCell.Value.ToString();
  293. }
  294. return "未知";
  295. }
  296. private void button1_Click(object sender, EventArgs e)
  297. {
  298. setdefault();
  299. }
  300. private void pictureBox1_Click(object sender, EventArgs e)
  301. {
  302. groupBox1.Visible = false;
  303. }
  304. //+++++++++++++++++++++
  305. private void setdefault()
  306. {
  307. //默认值的取得
  308. Readfile.Readfile rf = new Readfile.Readfile();
  309. if (groupBox1.Text == "用户配额")
  310. {
  311. if (cell.usertype == "教师")
  312. {
  313. water.Text = "0";
  314. elec.Text = "0";
  315. gas.Text = "0";
  316. }
  317. else
  318. {
  319. water.Text = rf.GetConfig(Application.StartupPath + "/config.ini", "waterlimit");
  320. elec.Text = rf.GetConfig(Application.StartupPath + "/config.ini", "eleclimit");
  321. gas.Text = rf.GetConfig(Application.StartupPath + "/config.ini", "gaslimit");
  322. }
  323. }
  324. if (groupBox1.Text == "单价")
  325. {
  326. if (cell.usertype == "教师")
  327. {
  328. water.Text = rf.GetConfig(Application.StartupPath + "/config.ini", "watertc");
  329. elec.Text = rf.GetConfig(Application.StartupPath + "/config.ini", "electc");
  330. gas.Text = rf.GetConfig(Application.StartupPath + "/config.ini", "gastc");
  331. }
  332. else
  333. {
  334. water.Text = rf.GetConfig(Application.StartupPath + "/config.ini", "waterst");
  335. elec.Text = rf.GetConfig(Application.StartupPath + "/config.ini", "elecst");
  336. gas.Text = rf.GetConfig(Application.StartupPath + "/config.ini", "gasst");
  337. }
  338. }
  339. //rf.GetConfig();
  340. /*water.Text = data[0];
  341. elec.Text = data[1];
  342. gas.Text = data[2];
  343. * */
  344. }
  345. private void pictureBox2_Click(object sender, EventArgs e)
  346. {
  347. groupBox2.Visible = false;
  348. }
  349. private void button2_Click(object sender, EventArgs e)
  350. {
  351. if (handin.Text != "")
  352. {
  353. //修改余额
  354. userDataGridView[9, userDataGridView.CurrentCell.RowIndex].Value = double.Parse(recent.Text.ToString()) + double.Parse(handin.Text.ToString()) - double.Parse(textBox4.Text.ToString());
  355. recent.Text=userDataGridView[9, userDataGridView.CurrentCell.RowIndex].Value.ToString();
  356. //加入历史
  357. string userid = "wegDbfile-" + userDataGridView[1, userDataGridView.CurrentCell.RowIndex].Value.ToString();
  358. string detail = DateTime.Now.ToString() + "-----应交" + textBox4.Text.ToString() + "元" +
  359. "<br />" + "----------------------缴入" + handin.Text.ToString() + "元" +
  360. "<br />" + "----------------------剩余" + recent.Text.ToString() + "元"
  361. ;
  362. rf.AddDetail(Application.StartupPath + "/UserData/" + userid + ".rdb", detail);
  363. //清空已经使用
  364. userDataGridView[userDataGridView.CurrentCell.ColumnIndex - 1, userDataGridView.CurrentCell.RowIndex].Value = "0|0|0";
  365. updatemoney();
  366. groupBox2.Visible = false;
  367. textBox4.Text = "0";
  368. }
  369. }
  370. private void updatemoney()
  371. {
  372. string[] used;
  373. string[] limit;
  374. string[] per;
  375. double money=0.0;
  376. double me = 0.0;
  377. //已经使用
  378. used=userDataGridView[7, userDataGridView.CurrentCell.RowIndex].Value.ToString().Split('|');
  379. //单价
  380. per = userDataGridView[6, userDataGridView.CurrentCell.RowIndex].Value.ToString().Split('|');
  381. //配额
  382. limit = userDataGridView[5, userDataGridView.CurrentCell.RowIndex].Value.ToString().Split('|');
  383. //用户类型
  384. try
  385. {
  386. if (cell.usertype == "教师")
  387. for (int i = 0; i < 3; i++)
  388. {
  389. money += double.Parse(used[i]) * double.Parse(per[i]);
  390. }
  391. else
  392. {
  393. for (int i = 0; i < 3; i++)
  394. {
  395. //如果没超过使用限额算0元
  396. if (double.Parse(limit[i]) - double.Parse(used[i]) >= 0)
  397. {
  398. me = 0; }
  399. else {
  400. me=(double.Parse(used[i]) - double.Parse(limit[i])) * double.Parse(per[i]);
  401. }
  402. money += me;
  403. }
  404. }
  405. if (recent.Text=="") {
  406. recent.Text = "0";
  407. }
  408. //money = money - double.Parse(recent.Text.ToString());
  409. userDataGridView[8, userDataGridView.CurrentCell.RowIndex].Value = money.ToString();
  410. }
  411. catch
  412. {
  413. userDataGridView[8, userDataGridView.CurrentCell.RowIndex].Value = "数据格式有误..请检查";
  414. }
  415. }
  416. private void pictureBox3_Click(object sender, EventArgs e)
  417. {
  418. groupBox3.Visible = false;
  419. }
  420. private void button3_Click(object sender, EventArgs e)
  421. {
  422. string user = rf.GetConfig(Application.StartupPath + "/Database/PWD.mdb", "username");
  423. string pwd = rf.GetConfig(Application.StartupPath + "/Database/PWD.mdb", "userpassword");
  424. if (curpwd.Text == "" || newpwd.Text == "" || newpwd2.Text == "") { MessageBox.Show("密码不能为空"); return; }
  425. if (newpwd.Text != newpwd2.Text) { MessageBox.Show("两次输入不同"); return; }
  426. if (GetMd6Str(curpwd.Text) == pwd)
  427. {
  428. //写入密码
  429. rf.SetPwd(Application.StartupPath + "/Database/PWD.mdb", GetMd6Str("admin"), GetMd6Str(newpwd.Text));
  430. MessageBox.Show("设置成功");
  431. }
  432. else
  433. { MessageBox.Show("原密码错误"); }
  434. //隐藏并清空
  435. groupBox3.Visible = false;
  436. curpwd.Text = ""; newpwd.Text = ""; newpwd2.Text = "";
  437. }
  438. private void 复制单元格ToolStripMenuItem_Click(object sender, EventArgs e)
  439. {
  440. Clipboard.SetText(userDataGridView.CurrentCell.Value.ToString());
  441. }
  442. private void 粘贴单元格ToolStripMenuItem_Click(object sender, EventArgs e)
  443. {
  444. userDataGridView.CurrentCell.Value = Clipboard.GetText();
  445. }
  446. private void dToolStripMenuItem_Click(object sender, EventArgs e)
  447. {
  448. }
  449. }
  450. }

5.2 网络模块代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8. using System.IO;
  9. using Readfile;
  10. namespace My_project
  11. {
  12. class sck
  13. {
  14. //创建新tcplistenr监听端口
  15. TcpListener listener = new TcpListener(IPAddress.Any, 8080);
  16. public void listen(int ThreadNum,int port)
  17. {
  18. //改变端口
  19. listener = new TcpListener(IPAddress.Any, port);
  20. try
  21. {
  22. listener.Start();
  23. server.text = "后台服务器启动成功";
  24. //MessageBox.Show (listener.LocalEndpoint.ToString());
  25. //创建线程
  26. for (int i = 1; i <= ThreadNum; i++)
  27. {
  28. Thread thrd = new Thread(new ThreadStart(this.ls));
  29. thrd.IsBackground = true;
  30. thrd.Start();
  31. }
  32. }
  33. catch
  34. {
  35. server.text = "socket错误,未能启动";
  36. }
  37. }
  38. private void ls()
  39. {
  40. string GET,data;
  41. string[] result;
  42. TcpClient client = listener.AcceptTcpClient();
  43. //创建网络流
  44. NetworkStream netStream = client.GetStream();
  45. StreamReader sr = new StreamReader(client.GetStream());
  46. //读取第一行的东西然后关闭sr
  47. GET=sr.ReadLine().ToString();
  48. StreamWriter sw = new StreamWriter(client.GetStream());
  49. Readfile.Readfile rf = new Readfile.Readfile();
  50. result=GET.Split(' ');
  51. //sw.Write("HTTP/1.1 200 OK\r\n");
  52. if (result[1] == "") { ; }
  53. if (result[1] == "/"|result[1] == "/index.html")
  54. {
  55. //显示首页
  56. //
  57. StreamReader fsr = new StreamReader(Application.StartupPath + "/UserData/default.dba");
  58. while (fsr.EndOfStream == false)
  59. {
  60. sw.Write(fsr.ReadLine().ToString());
  61. }
  62. fsr.Close();
  63. sw.Close();
  64. sr.Close();
  65. netStream.Close();
  66. }
  67. else
  68. {
  69. try
  70. {//分离&
  71. string[] fs = result[1].Split('&');
  72. //分离=
  73. string[] fr = fs[0].Split('=');
  74. if (fr[1].ToString() != "")
  75. {
  76. if (rf.Existen(Application.StartupPath + "/UserData/wegDbfile-"
  77. + fr[1].ToString() + ".rdb"))
  78. {
  79. StreamReader fsr = new StreamReader(Application.StartupPath + "/UserData/wegDbfile-"
  80. + fr[1].ToString() + ".rdb");
  81. while (fsr.EndOfStream == false)
  82. {
  83. sw.Write(fsr.ReadLine().ToString());
  84. }
  85. fsr.Close();
  86. sw.Close();
  87. sr.Close();
  88. netStream.Close();
  89. }
  90. else
  91. {
  92. data = "<html><head><meta http-equiv=content-type content=\"text/html; charset=UTF-8\">数据不存在";
  93. sw.Write(data);
  94. sw.Close();
  95. sr.Close();
  96. netStream.Close();
  97. }
  98. }
  99. else
  100. {
  101. data = "<html><head><meta http-equiv=content-type content=\"text/html; charset=UTF-8\">数据不存在";
  102. sw.Write(data);
  103. sw.Close();
  104. sr.Close();
  105. netStream.Close();
  106. }
  107. }
  108. catch
  109. {
  110. data = "<html><head><meta http-equiv=content-type content=\"text/html; charset=UTF-8\">数据不存在";
  111. sw.Write(data);
  112. sw.Close();
  113. sr.Close();
  114. netStream.Close();
  115. }
  116. //MessageBox.Show(data);
  117. }
  118. //此线程退出时创建新线程
  119. Thread thrd = new Thread(new ThreadStart(this.ls));
  120. thrd.IsBackground = true;
  121. thrd.Start();
  122. }
  123. }
  124. }

5.3 启动窗体代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Security.Cryptography;
  9. using Readfile;
  10. namespace My_project
  11. {
  12. //公共结构..用于传递后台服务器状况
  13. public struct server {
  14. public static string text;
  15. }
  16. public partial class Launcher : Form
  17. {
  18. //public int i;
  19. MainForm mainform = new MainForm();
  20. public Launcher()
  21. {
  22. InitializeComponent();
  23. }
  24. private void timer1_Tick(object sender, EventArgs e)
  25. {
  26. if (progressBar1.Value == 100)
  27. {
  28. progressBar1.Value = 0;
  29. }
  30. progressBar1.Value += 1;
  31. //如果线程结束则清空资源进入主窗口
  32. switch (progressBar1.Value)
  33. {
  34. case 1: label3.Text = "载入配置文件"; break;
  35. case 20: label3.Text = "载入数据库"; break;
  36. case 40: label3.Text = "设置后台线程"; break;
  37. case 60: label3.Text = server.text; break;
  38. case 80: label3.Text = "载入界面"; break;
  39. }
  40. if (!backgroundWorker1.IsBusy & progressBar1.Value >= 90)
  41. {
  42. //释放线程资源
  43. backgroundWorker1.CancelAsync();
  44. backgroundWorker1.Dispose();
  45. this.Hide();
  46. this.ShowInTaskbar = false;
  47. mainform.Show();
  48. timer1.Enabled = false;
  49. }
  50. }
  51. private void Launcher_Load(object sender, EventArgs e)
  52. {
  53. }
  54. public static string GetMd6Str(string ConvertString)
  55. {
  56. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  57. string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8).Replace("-", "B");
  58. t2 += BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8).Replace("-","Z");
  59. t2 += BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8).Replace("-", "W");
  60. return t2;
  61. }
  62. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  63. {
  64. //在此载入数据库
  65. mainform.LoadData();
  66. }
  67. private void button1_Click(object sender, EventArgs e)
  68. {
  69. groupBox1.Enabled =false ;
  70. //配置文件读取类
  71. Readfile.Readfile cfg = new Readfile.Readfile();
  72. button1.Enabled = false;
  73. //检验用户名和密码
  74. string user=cfg.GetConfig(Application.StartupPath + "/Database/PWD.mdb", "username");
  75. string pwd = cfg.GetConfig(Application.StartupPath + "/Database/PWD.mdb", "userpassword");
  76. if (GetMd6Str(textBox1.Text) == user && GetMd6Str(textBox2.Text) == pwd)
  77. {
  78. this.Focus();
  79. //改变鼠标形状
  80. this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
  81. progressBar1.Visible = true;
  82. button1.Visible = false;
  83. timer1.Enabled = true;
  84. backgroundWorker1.RunWorkerAsync();
  85. //启用socket
  86. if(cfg.GetConfig(Application.StartupPath + "/config.ini", "enable")=="true")
  87. {
  88. sck sock = new sck();
  89. //读取线程数量
  90. sock.listen(int.Parse(cfg.GetConfig(Application.StartupPath + "/config.ini", "ThreadNum")),
  91. int.Parse(cfg.GetConfig(Application.StartupPath + "/config.ini", "port")));
  92. }
  93. else
  94. {
  95. server.text="未开启后台服务";}
  96. }
  97. else
  98. {
  99. label3.Text = "用户名或密码错误";
  100. timer2.Enabled = true;
  101. }
  102. }
  103. private void timer2_Tick(object sender, EventArgs e)
  104. {
  105. groupBox1.Enabled = true;
  106. button1.Enabled = true;
  107. label3.Text = "";
  108. timer2.Enabled = false;
  109. }
  110. private void pictureBox2_Click(object sender, EventArgs e)
  111. {
  112. Application.Exit();
  113. }
  114. private void Launcher_Paint(object sender, PaintEventArgs e)
  115. {
  116. //设定交点
  117. if (textBox1.Text=="") { textBox1.Focus(); }
  118. }
  119. }
  120. }

5.4 Readfile代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. namespace Readfile
  6. {
  7. public class Readfile
  8. {
  9. public string GetConfig(string path,string name)
  10. {
  11. StreamReader sr = new StreamReader(path);
  12. //初始化数组
  13. string[] config = "1=1".ToString().Split('=');
  14. //循环读取所需设置
  15. while (config[0] != name & sr.EndOfStream==false )
  16. {
  17. config = sr.ReadLine().ToString().Split('=');
  18. }
  19. sr.Close();
  20. try
  21. {
  22. return config[1];
  23. }
  24. catch
  25. {
  26. return "error";
  27. }
  28. }
  29. public bool SetPwd(string path,string username,string userpwd)
  30. {
  31. FileStream sm = new FileStream(path, FileMode.Create);
  32. StreamWriter sw = new StreamWriter(sm);
  33. sw.Write("[管理员信息]");
  34. sw.Write("\r\n");
  35. sw.Write("\r\n");
  36. sw.Write("-------请勿改动以下内容------");
  37. sw.Write("\r\n");
  38. sw.Write("username="+username);
  39. sw.Write("\r\n");
  40. sw.Write("userpassword="+userpwd);
  41. sw.Write("\r\n");
  42. sw.Write("-------请勿改动以上内容------");
  43. sw.Close();
  44. sm.Close();
  45. return true;
  46. }
  47. //历史记录加入
  48. public void AddDetail(string path, string detail)
  49. {
  50. //如果文件不存在则新建
  51. FileStream File = new FileStream(path, FileMode.OpenOrCreate);
  52. //如果是新建的文件。。。则写入meta属性
  53. if (File.Length == 0)
  54. {
  55. StreamWriter sw1 = new StreamWriter(File);
  56. sw1.Write("<html><head><meta http-equiv=content-type content=\"text/html; charset=UTF-8\"></head><body>");
  57. sw1.Close();
  58. }
  59. File.Close();
  60. FileStream append = new FileStream(path, FileMode.Append);
  61. StreamWriter sw = new StreamWriter(append);
  62. sw.Write(detail);
  63. //写入换行符
  64. sw.Write("<br />");
  65. sw.Close();
  66. }
  67. public bool Existen(string path)
  68. {
  69. try
  70. {
  71. FileStream File = new FileStream(path, FileMode.Open);
  72. File.Close();
  73. return true;
  74. }
  75. catch
  76. {
  77. return false;
  78. }
  79. }
  80. }
  81. }

5.5 BackgroundServise代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.IO;
  8. using Readfile;
  9. namespace BackgroundServise
  10. {
  11. class Program
  12. {
  13. //创建新tcplistenr监听端口
  14. static TcpListener listener = new TcpListener(IPAddress.Any,0);
  15. static void Main(string[] args)
  16. {
  17. Console.Clear();
  18. int ThreadNum; int port;
  19. //初始值
  20. ThreadNum = -1;
  21. port = -1;
  22. try
  23. {
  24. //改变端口和线程数
  25. for (int i = 0; i < args.Length; i++)
  26. {
  27. string[] result = args[i].Split('=');
  28. if (result[0] == "p") { port = int.Parse(result[1]); }
  29. if (result[0] == "t") { ThreadNum = int.Parse(result[1]); }
  30. }
  31. if (ThreadNum == -1 || port == -1)
  32. {
  33. Console.Beep();
  34. Console.Write("用法错误 \r\n程序接口p 端口 t线程数\r\n例 BackgroundServise.exe p=8080 t=10" +
  35. "\r\n将在本地开启10个线程监听8080端口\r\n超过此线程用户将无法连接\r\n");
  36. return;
  37. }
  38. }
  39. catch
  40. {
  41. //错误捕获
  42. Console.Beep();
  43. Console.Write("用法错误 \r\n程序接口p 端口 t线程数\r\n例 BackgroundServise.exe p=8080 t=10" +
  44. "\r\n将在本地开启10个线程监听8080端口\r\n超过此线程用户将无法连接\r\n");
  45. return;
  46. }
  47. Console.Write("水电管理系统后台服务器\r\n版权所有:白忠魏\r\n");
  48. Console.Write("线程数" + ThreadNum.ToString());
  49. Console.Write("\r\n");
  50. Console.Write("端口" + port.ToString()+"\r\n");
  51. //改变端口
  52. listener = new TcpListener(IPAddress.Any, port);
  53. try
  54. {
  55. listener.Start();
  56. Console.Write("开始监听");
  57. //MessageBox.Show (listener.LocalEndpoint.ToString());
  58. //创建线程
  59. for (int i = 1; i <= ThreadNum; i++)
  60. {
  61. Thread thrd = new Thread(new ThreadStart(ls));
  62. thrd.IsBackground = false;
  63. thrd.Start();
  64. }
  65. }
  66. catch
  67. {
  68. Console.Beep();
  69. Console.Write("socket错误,启动失败");
  70. return;
  71. }
  72. }
  73. static private void ls()
  74. {
  75. string GET,data;
  76. string[] result;
  77. TcpClient client = listener.AcceptTcpClient();
  78. //创建网络流
  79. NetworkStream netStream = client.GetStream();
  80. StreamReader sr = new StreamReader(client.GetStream());
  81. //读取第一行的东西然后关闭sr
  82. GET=sr.ReadLine().ToString();
  83. Console.Write("\r\n");
  84. Console.Write(GET);
  85. StreamWriter sw = new StreamWriter(client.GetStream());
  86. Readfile.Readfile rf = new Readfile.Readfile();
  87. result=GET.Split(' ');
  88. //sw.Write("HTTP/1.1 200 OK\r\n");
  89. if (result[1] == "") { ; }
  90. if (result[1] == "/"|result[1] == "/index.html")
  91. {
  92. //显示首页
  93. //
  94. StreamReader fsr = new StreamReader("./UserData/default.dba");
  95. while (fsr.EndOfStream == false)
  96. {
  97. sw.Write(fsr.ReadLine().ToString());
  98. }
  99. fsr.Close();
  100. sw.Close();
  101. sr.Close();
  102. netStream.Close();
  103. }
  104. else
  105. {
  106. try
  107. {//分离&
  108. string[] fs = result[1].Split('&');
  109. //分离=
  110. string[] fr = fs[0].Split('=');
  111. if (fr[1].ToString() != "")
  112. {
  113. if (rf.Existen("/UserData/wegDbfile-"
  114. + fr[1].ToString() + ".rdb"))
  115. {
  116. StreamReader fsr = new StreamReader("./UserData/wegDbfile-"
  117. + fr[1].ToString() + ".rdb");
  118. while (fsr.EndOfStream == false)
  119. {
  120. sw.Write(fsr.ReadLine().ToString());
  121. }
  122. Console.Write("\r\n");
  123. Console.Write(fr[1].ToString()+"数据发送成功");
  124. fsr.Close();
  125. sw.Close();
  126. sr.Close();
  127. netStream.Close();
  128. }
  129. else
  130. {
  131. data = "<html><head><meta http-equiv=content-type content=\"text/html; charset=UTF-8\">数据不存在";
  132. sw.Write(data);
  133. Console.Write("\r\n");
  134. Console.Write(fr[1].ToString() + "无此数据");
  135. sw.Close();
  136. sr.Close();
  137. netStream.Close();
  138. }
  139. }
  140. else
  141. {
  142. data = "<html><head><meta http-equiv=content-type content=\"text/html; charset=UTF-8\">数据不存在";
  143. sw.Write(data);
  144. Console.Write("\r\n");
  145. Console.Write(fr[1].ToString() + "数据发送失败");
  146. sw.Close();
  147. sr.Close();
  148. netStream.Close();
  149. }
  150. }
  151. catch
  152. {
  153. data = "<html><head><meta http-equiv=content-type content=\"text/html; charset=UTF-8\">数据不存在";
  154. sw.Write(data);
  155. sw.Close();
  156. sr.Close();
  157. netStream.Close();
  158. }
  159. //MessageBox.Show(data);
  160. }
  161. //此线程退出时创建新线程
  162. Thread thrd = new Thread(new ThreadStart(ls));
  163. thrd.IsBackground = false;
  164. thrd.Start();
  165. }
  166. }
  167. }
上传的附件 cloud_download 水电管理信息系统.7z ( 15.86mb, 68次下载 )
error_outline 下载需要11点积分

keyboard_arrow_left上一篇 : 基于C#和Sql Server的图书管理系统 基于Java的简单web服务器设计与实现 : 下一篇keyboard_arrow_right



SMASHBOX
2018-11-05 15:48:05
基于C#和ACCESS数据库实现的水电管理信息系统
1275322230
2020-10-25 15:04:34
积分不够下载不了
sddzxw
2021-01-28 14:05:20
积分不够
zhangjianhu
2021-05-04 15:26:17
非常好的源码

发送私信

我这种人,别爱别信别打动

9
文章数
7
评论数
最近文章
eject