24.11.19
C/S结构用户界面设计
【实验编号】
10003809547j 图形用户界面设计
【实验学时】
8学时
【实验环境】
所需硬件环境为微机;
所需软件环境为Microsoft Visual Studio 2013
【实验内容】
- 主界面增加基础信息
核心代码
namespace CS
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(71, 35);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(143, 70);
this.button1.TabIndex = 0;
this.button1.Text = "基础信息";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(71, 144);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(143, 70);
this.button2.TabIndex = 1;
this.button2.Text = "入库信息";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(71, 258);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(143, 70);
this.button3.TabIndex = 2;
this.button3.Text = "出库信息 ";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(71, 368);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(143, 70);
this.button4.TabIndex = 3;
this.button4.Text = "用户 ";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(71, 481);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(143, 70);
this.button5.TabIndex = 4;
this.button5.Text = "库存";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 593);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "物料系统";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
}
}
- 入库查询及修改
核心代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CS
{
public partial class RuKu : Form
{
Class1 类 = new Class1();
public RuKu()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
//数据库链接参数
SqlConnection con = new SqlConnection(类.SQL链接地址());
//数据库打开
con.Open();
//数据库语句
string sql = "select MingCeng as 名称 ,BianMa as 编码 ,GuiGe as 规格 from JiChuXinXi where (MingCeng like '%" + textBox1.Text + "%' or BianMa like '%" + textBox1.Text + "%' ) ";
//SqlDataAdapter来接收 数据库参数与语句
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sql, con);
// DataSet 是数据容器
DataSet dataSet1 = new DataSet();
//sqlDataAdapter1.Fill把数据填充给DataSet
sqlDataAdapter1.Fill(dataSet1);
//数据库链接关闭
con.Close();
//dataGridView1这个控件显示的数据 来源于DataSet的 Tables表
dataGridView1.DataSource = dataSet1.Tables[0];
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
}
private void RuKu_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string 单号 = DateTime.Now.ToString("yyyyMMddhhmmss");
//MessageBox.Show(单号);
try
{
SqlConnection conn = new SqlConnection(类.SQL链接地址());
conn.Open();
string sql = "insert into RCK values ('入库','" + 单号 + "','" + textBox2.Text + "','" + textBox3.Text + "','','',GETDATE())";
SqlCommand cmd = new SqlCommand(sql, conn);
int count = cmd.ExecuteNonQuery();
conn.Close();
if (count == 1)
{
MessageBox.Show("保存成功");
}
else
{
MessageBox.Show("保存失败");
}
}
catch (Exception ex)
{
MessageBox.Show("保存失败" + ex.ToString());
}
}
}
}
- 库存查询
核心代码
namespace CS
{
partial class Form2
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 77);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.Size = new System.Drawing.Size(566, 488);
this.dataGridView1.TabIndex = 0;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(86, 26);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(149, 21);
this.textBox2.TabIndex = 4;
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
//
// button2
//
this.button2.Location = new System.Drawing.Point(503, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 5;
this.button2.Text = "全部查询";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(330, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "索引查询";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(609, 592);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form2";
this.Text = "库存查询";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
}
}
4. 提交出库
核心代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CS
{
public partial class ChuKu : Form
{
public decimal 当前库存;
Class1 类 = new Class1();
public ChuKu()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//数据库链接参数
SqlConnection con = new SqlConnection(类.SQL链接地址());
//数据库打开
con.Open();
//数据库语句
string sql = "select BianMa AS 编码,MingCeng as 规格 ,dbo.f_get_当前库存(BianMa) as 库存 from JiChuXinXi where (BianMa like '%" + textBox3.Text + "%' or MingCeng like '%" + textBox3.Text + "%' ) ";
//SqlDataAdapter来接收 数据库参数与语句
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sql, con);
// DataSet 是数据容器
DataSet dataSet1 = new DataSet();
//sqlDataAdapter1.Fill把数据填充给DataSet
sqlDataAdapter1.Fill(dataSet1);
//数据库链接关闭
con.Close();
//dataGridView1这个控件显示的数据 来源于DataSet的 Tables表
dataGridView1.DataSource = dataSet1.Tables[0];
}
private void button2_Click(object sender, EventArgs e)
{
Decimal 出库数量 = Convert.ToDecimal(textBox2.Text.ToString());
decimal shuliang = Convert.ToDecimal(textBox2.Text.ToString())*-1;
string 单号 = DateTime.Now.ToString("yyyyMMddhhmmss");
if (出库数量 > 当前库存)
{
MessageBox.Show("库存数不够!");
}
else
{
try
{
SqlConnection conn = new SqlConnection(类.SQL链接地址());
conn.Open();
string sql = "insert into RCK values ('出库','" + 单号 + "','" + textBox1.Text + "','" + shuliang + "','','" + comboBox1.Text + "',GETDATE())";
SqlCommand cmd = new SqlCommand(sql, conn);
int count = cmd.ExecuteNonQuery();
conn.Close();
if (count == 1)
{
MessageBox.Show("保存成功");
}
else
{
MessageBox.Show("保存失败");
}
}
catch (Exception ex)
{
MessageBox.Show("保存失败" + ex.ToString());
}
}
}
private void ChuKu_Load(object sender, EventArgs e)
{
//数据库链接参数
SqlConnection con = new SqlConnection(类.SQL链接地址());
//数据库打开
con.Open();
//数据库语句
string sql = "SELECT CAST([ID] AS VARCHAR)+[NAME] AS kehu FROM [YongHu]";
//SqlDataAdapter来接收 数据库参数与语句
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sql, con);
// DataSet 是数据容器
DataSet dataSet1 = new DataSet();
//sqlDataAdapter1.Fill把数据填充给DataSet
sqlDataAdapter1.Fill(dataSet1);
//数据库链接关闭
con.Close();
this.comboBox1.DataSource = dataSet1.Tables[0].DefaultView;
this.comboBox1.DisplayMember = "kehu";
this.comboBox1.ValueMember = "kehu";
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//e.RowIndex e点击行RowIndex号
textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
//Rows[e.RowIndex]控件的第e.RowIndex行 Cells[0]第0列
textBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
当前库存 = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
- 用户管理
核心代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CS
{
public partial class YongHu : Form
{
Class1 类 = new Class1();
public YongHu()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
YongHuTianJia yonghutianjia = new YongHuTianJia();
yonghutianjia.Show();
}
private void YongHu_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//数据库链接参数
SqlConnection con = new SqlConnection(类.SQL链接地址());
//数据库打开
con.Open();
//数据库语句
string sql = "SELECT * FROM [cs].[dbo].[YongHu]";
//SqlDataAdapter来接收 数据库参数与语句
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sql, con);
// DataSet 是数据容器
DataSet dataSet1 = new DataSet();
//sqlDataAdapter1.Fill把数据填充给DataSet
sqlDataAdapter1.Fill(dataSet1);
//数据库链接关闭
con.Close();
//dataGridView1这个控件显示的数据 来源于DataSet的 Tables表
dataGridView1.DataSource = dataSet1.Tables[0];
}
}
}
【关键步骤】
(1) 主界面可进行基础信息的添加,添加编码规格备注等信息
(2) 根据添加的基础信息完成商品的入库
(3) 在库存可以看到已经入库的商品信息
(4) 在出库信息中可完成商品的出库
(5) 在用户中可查看管理用户信息
【实验体会】
这次实验中我使用C#完成了一个简单物料管理的CS程序,通过连接到虚拟机中的windows7sqlserver服务完成了基础的增删改查功能,页面设计的比较简陋,VS编写C#界面也是十分方便,就是有时会报莫名其妙的错误,但好在系统不算复杂都解决了,通过这次实验我认识到了CS架构与平常写的网页的不同,二者各有优缺点,各有各自的应用场景。