6 程序源代码说明书
6.1 程序源代码 6.1.1 主界面代码:
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using System.Data.SqlClient;
namespace LostQuery {
public partial class Main : Form {
public Main() {
InitializeComponent(); }
// “登记/修改” 按钮的响应
private void btn_input_Click(object sender, EventArgs e) {
Login login = new Login(); login.Show(); }
//“查询”按钮的响应
private void btn_query_Click(object sender, EventArgs e) {
Query query = new Query(); query.Show(); } } }
6.1.2 登录 页面代码:
using System;
11
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms;
namespace LostQuery {
public partial class Login : Form {
public Login() {
InitializeComponent(); }
//“登录”按钮添加响应
private void btn_login_Click(object sender, EventArgs e) {
//判断是否输入为空
if (this.tb_account.Text == \ || this.tb_password.Text == \) {
MessageBox.Show(\请输入用户名和密码\, \提示\, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.tb_account.Focus(); this.tb_password.Focus(); } else {
//用户名和密码输入正确
if (this.tb_account.Text == \ && this.tb_password.Text == \) {
Input input = new Input(); input.Show(); this.Visible = false; }
//用户名或密码不正确 else {
MessageBox.Show(\对不起,你输入的用户名或密码不正确,不能进行登记\, \提示\, MessageBoxButtons.OK, MessageBoxIcon.Information);
12
this.tb_account.Focus(); this.tb_password.Focus(); } } }
//“清除”按钮添加响应
private void btn_clear_Click(object sender, EventArgs e) {
tb_account.Clear(); tb_password.Clear(); } } }
6.1.3 登记/修改 页面代码:
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace LostQuery {
public partial class Input : Form {
public Input() {
InitializeComponent(); }
/*****************************************************************************/ /* 登记新的失物信息模块 */ /*****************************************************************************/
//给确定按钮添加响应
13
private void btnOk_Click(object sender, EventArgs e) {
//失物类型不允许为空 if (this.tbType.Text == \)
MessageBox.Show(\请输入失物类型\, \提示\, MessageBoxButtons.OK, MessageBoxIcon.Information); else {
//把失物信息写入数据库 string connString =
\Security=True\;//新建connection对象
SqlConnection connection = new SqlConnection(connString);
string sqlString = \
listLost(type,feature,lostLocation,date,status) values('\ + tbType.Text.Trim() + \ + tbFeature.Text.Trim() + \ + tbAddress.Text.Trim() + \ +
dateTimePicker1.Value.ToShortDateString() + \未领取')\;//登记的物品状态都设置为未领取 try {
connection.Open();
MessageBox.Show(\打开数据库连接成功\);
SqlCommand command = new SqlCommand(sqlString, connection); int result = command.ExecuteNonQuery(); MessageBox.Show(\添加成功\); }
catch (Exception) {
MessageBox.Show(\打开失败\); } finally {
connection.Close();
MessageBox.Show(\关闭数据库连接成功\); } } }
//“清除”按钮添加响应
private void btnClear_Click(object sender, EventArgs e) {
tbType.Clear();
14
tbFeature.Clear(); tbAddress.Clear(); }
/*****************************************************************************/ /* 修改状态模块 */ /*****************************************************************************/
//“确定”按钮添加响应
private void buttonOK_Click(object sender, EventArgs e) {
//输入为空
if (this.tbGet.Text == \)
MessageBox.Show(\请输入领取编号!\, \提示\, MessageBoxButtons.OK, MessageBoxIcon.Information); else {
//判断输入的是不是数字
string isNum = this.tbGet.Text;
Regex reg = new Regex(\); //判断是不是数据,要不是就表示没有选择,则从隐藏域里读出来
Match ma = reg.Match(isNum); if (ma.Success) {
int num = Convert.ToInt32(isNum); //是数字
string connString =
\Security=True\;//新建connection对象
SqlConnection connection = new SqlConnection(connString);
string sqlString = \已领取' where id='\+num+\;
try {
connection.Open();
MessageBox.Show(\打开数据库连接成功\);
SqlCommand command = new SqlCommand(sqlString, connection); int result = command.ExecuteNonQuery(); MessageBox.Show(\状态更改成功\); }
15