码迷,mamicode.com
首页 > Windows程序 > 详细

winform基础——实现简易赈灾物资发放登记系统

时间:2015-05-08 12:28:19      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

技术分享技术分享技术分享技术分享技术分享

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication10
{
class Program
{
public const string CONNSTRING = "server=.;database=zhenzai;uid=sa;pwd=123";
static void Main(string[] args)
{
//标题栏
Console.WriteLine("****************赈灾物资发放登记系统*******************");
Console.WriteLine("1.物资库存维护");
Console.WriteLine("2.物资库存列表");
Console.WriteLine("3.物资发放");
Console.WriteLine("4.物资发放统计");
Console.WriteLine("5.退出系统");
Console.WriteLine("*******************************************************");
//进入循环 可以循环输入数字进入系统
while (true)
{
string a = Console.ReadLine();
switch (a)
{
case "1":
ChaInfo(); //输入1 进入库存管理函数
break;
case "2":
ShowKc(); //输入2 进入显示库存函数
break;
case "3":
fafang(); //输入3 进入物资发放函数
break;
case "4":
FaTongji(); // 输入4 进入显示发放物资函数
break;

default:
break;
}
if (a=="5")
{
break; // 输入5 跳出循环
}
}
}
/// <summary>
/// 库存管理函数(库存输入)
/// </summary>
public static void ChaInfo()
{
Console.Write("请输入物资名称:");
string name = Console.ReadLine();
Console.Write("请输入库存数量:");
string num = Console.ReadLine();
Console.Write("请输入计量单位:");
string danwei = Console.ReadLine();
Console.Write("请输入物资单价:");
string jiage = Console.ReadLine();
SqlConnection conn = new SqlConnection(CONNSTRING);
//为了防止添加出错链接关不掉,使用try-catch-finally
try
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into kc values(‘" + name + "‘,‘" + num + "‘,‘" + danwei + "‘,‘" + jiage + "‘)";
cmd.ExecuteNonQuery();
Console.WriteLine("添加成功!");
}
catch
{
Console.WriteLine("添加出错");
}
finally
{
conn.Close();
}
Console.WriteLine("*******************************************************");
}
/// <summary>
/// 显示库存函数
/// </summary>
public static void ShowKc()
{
Console.WriteLine("物资库存列表如下:");
Console.WriteLine("**********************************************************");
Console.WriteLine("物资名称\t库存数量\t单价(元)");
SqlConnection conn = new SqlConnection(CONNSTRING);
try
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from kc";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Console.WriteLine(dr[0].ToString() + "\t\t" + dr[1].ToString() + dr[2].ToString() + "\t\t" + dr[3].ToString() + "\n");
}
}
finally
{
conn.Close();
}
Console.WriteLine("*******************************************************");
}
/// <summary>
/// 发放物资函数
/// </summary>
public static void fafang()
{
Console.Write("请输入发放物资:");
string ss = Console.ReadLine();
SqlConnection conn = new SqlConnection(CONNSTRING);
try
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from kc where WzName=‘" + ss + "‘";
SqlDataReader dr = cmd.ExecuteReader();
string a, e;
int b;
while (dr.Read())
{
a = dr[3].ToString();
Console.WriteLine("当前单价:" + a);
b = Convert.ToInt32(dr[1]);
e = dr[2].ToString(); //记录单位
Console.WriteLine("当前库存:" + b + e);


Console.Write("请输入发放数量:");
int c = Convert.ToInt32(Console.ReadLine());
Console.Write("接收人:");
string d = Console.ReadLine();
if (c > b) //判断如果发放数量大于库存数量
{
Console.WriteLine("库存数量不足!");
break;
}
else //否则执行以下两个函数
{
InsertFa(ss, a, c, d, e); //向fafang表中插入数据
JianKc(ss, b, c); // 更新库存表中相应库存数量
Console.WriteLine("发放成功!");
}
}
}
finally
{
conn.Close();
}
Console.WriteLine("*******************************************************");
}
/// <summary>
/// 更新fafang表
/// </summary>
/// <param name="ss">物资名称</param>
/// <param name="a">当前单价</param>
/// <param name="c">发放数量</param>
/// <param name="d">接收人</param>
/// <param name="e">单位</param>
public static void InsertFa(string ss,string a,int c,string d,string e)
{
DateTime dt = DateTime.Now;
string aa = dt.ToString("yyyy年MM月dd日");
SqlConnection conn = new SqlConnection(CONNSTRING);
try
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into fafang values(‘" + ss + "‘," + c + ",‘" + e + "‘," + a + ",‘" + aa + "‘,‘" + d + "‘)";
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
/// <summary>
/// 库存更新函数
/// </summary>
/// <param name="ss">物资名称</param>
/// <param name="b">原来库存数量</param>
/// <param name="c">发放数量</param>
public static void JianKc(string ss,int b,int c)
{
SqlConnection conn = new SqlConnection(CONNSTRING);
try
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "update kc set num=" + (b - c) + "where wzname=‘" + ss + "‘";
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
/// <summary>
/// 显示fafang表中的数据
/// </summary>
public static void FaTongji()
{
SqlConnection conn = new SqlConnection(CONNSTRING);
try
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from fafang";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Console.WriteLine(dr[1].ToString() + "\t" + dr[2].ToString() + dr[3].ToString() + "\t" + dr[4].ToString() + "\t" + dr[5].ToString() + "\t" + dr[6].ToString() + "\n");
}
}
finally
{
conn.Close();
}
Console.WriteLine("*******************************************************");
}
}
}

winform基础——实现简易赈灾物资发放登记系统

标签:

原文地址:http://www.cnblogs.com/William-1234/p/4487249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!