using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication8
{
class Program
{
public const string CONNECTIONSTRING = "server=MA-PC;database=zhenzai;uid=sa;pwd=111111";
//向数据库中添加数据
public static void AddInfo(string wzname, string num, string unit, string price)
{
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into kc values('" + wzname + "','" + num + "','" + unit + "','" + price + "')";
cmd.ExecuteNonQuery();
conn.Close();
}
//输入数据
public static void ChaRu()
{
Console.Write("请输入物资名称:");
string wzname = Console.ReadLine();
Console.Write("请输入计量单位:");
string num = Console.ReadLine();
Console.Write("请输入库存数量:");
string unit = Console.ReadLine();
Console.Write("请输入物资单价:");
string price = Console.ReadLine();
AddInfo(wzname, num, unit, price);
Console.WriteLine("添加成功");
}
//显示物资库存列表
public static void Show2()
{
Console.WriteLine("物资名称"+" "+"库存数量"+" "+"单价(元)");
//显示
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from kc";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string wzname = dr["WzName"].ToString();
string num = dr["Num"].ToString();
string unit = dr["Unit"].ToString();
string price = dr["Price"].ToString();
Console.WriteLine(wzname +" "+ " " + num + unit + " " +" "+ price);
}
conn.Close();
}
//向物资发放列表添加
public static void addkc(string name, string unit, string num, string price)
{
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();
SqlCommand scmd = conn.CreateCommand();
scmd.CommandText = "insert into kc values('" + name + "','" + num + "','" + unit + "','" + price + "')";
scmd.ExecuteNonQuery();
Console.WriteLine("添加成功!");
conn.Close();
}
//物资发放显示
public static void insert()
{
string name,num, receiver;
Console.WriteLine("****************************物质库存维护****************************");
do{
Console.Write("请输入发放物资名称:");
name = Console.ReadLine();
}
while(!hasWuzi(name));
do{
Console.Write("请输入发放数量:");
num = Console.ReadLine();
}
while(!has(num,name));
Console.Write("接收人:");
receiver = Console.ReadLine();
addfa(name,num,receiver);
Console.WriteLine("成功发放!");
}
//查询
public static void addfa(string name,string num,string receiver)
{
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();
SqlCommand scmd = conn.CreateCommand();
scmd.CommandText = "select * from kc where WzName='" + name + "'";
SqlDataReader re = scmd.ExecuteReader();
while (re.Read())
{
string price=re["Price"].ToString();
string unit = re["Unit"].ToString();
int n=Convert.ToInt32(num);
insertfa(name,n,unit,price,receiver);
}
conn.Close();
}
//向发放表中添加数据
public static void insertfa(string name,int num,string unit,string price,string receiver)
{
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();
SqlCommand scmd = conn.CreateCommand();
scmd.CommandText = "insert into fafang values('"+name+"','"+num+"','"+unit+"','"+price+"','"+DateTime.Now+"','"+receiver+"')";
scmd.ExecuteNonQuery();
conn.Close();
}
//检查发放物资的库存
public static bool has(string num,string name)
{
bool t = false;
int n = Convert.ToInt32(num);
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();
SqlCommand scmd = conn.CreateCommand();
scmd.CommandText = "select * from kc where WzName='" + name + "'";
SqlDataReader re = scmd.ExecuteReader();
re.Read();
if ((int)re["Num"] >= n)
{
t = true;
}
else
{
Console.WriteLine("库存不足!!!");
}
conn.Close();
return t;
}
//查询输出单价和库存
public static bool hasWuzi(string name)
{
bool t = false;
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();
SqlCommand scmd = conn.CreateCommand();
scmd.CommandText = "select * from kc where WzName='"+name+"'";
SqlDataReader re=scmd.ExecuteReader();
t = re.HasRows;
while (re.Read())
{
Console.WriteLine("当前单价:"+re["Price"].ToString()+"元
当前库存:"+re["Num"].ToString()+re["Unit"].ToString()+"
");
}
conn.Close();
return t;
}
//显示物资发放列表
public static void show4()
{
Console.WriteLine("物资名称"+" "+"发放数量"+" "+"单价(元)"+" "+"领取人");
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from fafang";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string wzname = dr["WzName"].ToString();
string fanum = dr["faNum"].ToString();
string unit = dr["Unit"].ToString();
string faprice = dr["faPrice"].ToString();
string fadate = ((DateTime )dr["fadate"]).ToString("yyyy年MM月dd日");
string receiver = dr["receiver"].ToString();
Console.WriteLine(wzname + " "+" " + fanum + unit + " " +" "+ faprice+" "+fadate +" "+receiver);
}
conn.Close();
}
static void Main(string[] args)
{
Console.WriteLine("赈灾物资发放登记表");
while (true)
{
Console.WriteLine("1、物资库存维护
2、物资库存列表
3、物资发放
4、物资发放统计
5、退出系统");
string tr=Console.ReadLine ();
switch (tr)
{
case "1":
Console.WriteLine("物资库存维护");
Console.WriteLine("*************************************************************");
ChaRu();
Console.WriteLine("*************************************************************");
break ;
case "2":
Console.WriteLine("物资库存表如下");
Console.WriteLine("*************************************************************");
Show2();
Console.WriteLine("*************************************************************");
break ;
case "3":
Console.WriteLine("物资发放");
insert();
break ;
case "4":
Console.WriteLine("物资发放统计");
Console.WriteLine("*************************************************************");
show4();
Console.WriteLine("*************************************************************");
break ;
}
if (tr=="5")
{
break;
}
Console.ReadLine();
}
}
}
}