//包含头文件
using System.Data.SqlClient;
//连接数据库
//连接sql数据库
public SqlConnection sql_open_wj()
{
string connString = "user id=sa;password=1234;Database=Z2WB;Connect Timeout=30;Server=10.172.24.119";
SqlConnection conn = new SqlConnection(connString);
conn.ConnectionString = connString;
conn.Open();
return conn;
}
//关闭sql数据库
public void sql_close_wj(SqlConnection conn)
{
conn.Close();
}
//查询sql数据库
public SqlDataReader sql_chaxun_wj(SqlConnection conn,String sql)
{
//"SELECT count(*) from Student"
string SQLstring = sql;
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = SQLstring;
SqlDataReader dataReader = command.ExecuteReader();
return dataReader;
//dataReader.Read
// int num = (int)command.ExecuteScalar();
}
//执行sql语句
public void sql_zhixing_wj(SqlConnection conn, String sql)
{
//"SELECT count(*) from Student"
string SQLstring = sql;
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = SQLstring;
command.ExecuteNonQuery();
}
//sql读取数字
public void sql_duqu_wj(SqlDataReader datareader)
{
while (datareader.Read())
{
//StuName = (string)dataReader[0];//读取第一单元的值
}
datareader.Close();
}
//demo
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 总二料板
{
public partial class Form1 : Form
{
int ruku = 0;
int chuku = 0;
int chaxunku = 0;
string cache = "";
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//获取扫码输入
int press_sj =(int) e.KeyChar;
//回车符输入
if (press_sj == 13)
{
ruku = 0;
cache = "";
//textBox2.AppendText(press_sj.ToString());
textBox2.Text = "";
textBox2.Text = textBox1.Text;
textBox1.Text = "";
SqlConnection sql_data = sql_open_wj();//连接数据库
//查询编码是否存在
SqlDataReader sql_jiluji=sql_chaxun_wj(sql_data,"SELECT * FROM kucun WHERE 产品编码='"+textBox2.Text+"'");
if (sql_jiluji.Read())
{
cache = "库位号:" + sql_jiluji[1] + "物料编码:" + sql_jiluji[6];
//继续判断是否已经入库
if (sql_jiluji[3].ToString()== "1")
{
//重复入库
textBox3.Text = cache+ "重复入库";
ruku = 2;
}
else
{
//可以入库
ruku = 1;
}
}
else
{
textBox3.Text = "不存在编码";
ruku = 2;
}
sql_jiluji.Close();
//执行入库
if (ruku == 1)
{
sql_zhixing_wj(sql_data, "UPDATE kucun SET 入库时间 = GETDATE(), 入库状态 = 1 WHERE 产品编码='" + textBox2.Text + "'");
textBox3.Text =cache+ "入库成功";
}
sql_close_wj(sql_data);
}
}
public SqlConnection sql_open_wj()
{
string connString = "user id=sa;password=1234;Database=Z2WB;Connect Timeout=30;Server=10.172.24.119";
SqlConnection conn = new SqlConnection(connString);
conn.ConnectionString = connString;
conn.Open();
return conn;
}
public void sql_close_wj(SqlConnection conn)
{
conn.Close();
}
public SqlDataReader sql_chaxun_wj(SqlConnection conn,String sql)
{
//"SELECT count(*) from Student"
string SQLstring = sql;
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = SQLstring;
SqlDataReader dataReader = command.ExecuteReader();
return dataReader;
//dataReader.Read
// int num = (int)command.ExecuteScalar();
}
public void sql_zhixing_wj(SqlConnection conn, String sql)
{
//"SELECT count(*) from Student"
string SQLstring = sql;
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = SQLstring;
command.ExecuteNonQuery();
}
public void sql_duqu_wj(SqlDataReader datareader)
{
while (datareader.Read())
{
//StuName = (string)dataReader[0];//读取第一单元的值
}
datareader.Close();
}
private void 新建库位ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{
//获取扫码输入
int press_sj = (int)e.KeyChar;
//回车符输入
if (press_sj == 13)
{
chuku = 0;
cache = "";
//textBox2.AppendText(press_sj.ToString());
textBox5.Text = "";
textBox5.Text = textBox4.Text;
textBox4.Text = "";
SqlConnection sql_data = sql_open_wj();//连接数据库
//查询编码是否存在
SqlDataReader sql_jiluji = sql_chaxun_wj(sql_data, "SELECT * FROM kucun WHERE 产品编码='" + textBox5.Text + "'");
if (sql_jiluji.Read())
{
cache = "库位号:" + sql_jiluji[1] + "物料编码:" + sql_jiluji[6];
//继续判断是否已经入库
if (sql_jiluji[3].ToString() == "1")
{
//可以出库
chuku = 1;
}
else
{
//未入库
textBox3.Text = cache+"未入库";
chuku = 2;
}
}
else
{
textBox3.Text = "不存在编码";
chuku = 2;
}
sql_jiluji.Close();
//执行出库
if (chuku == 1)
{
sql_zhixing_wj(sql_data, "UPDATE kucun SET 入库时间 = GETDATE(), 入库状态 = 2 WHERE 产品编码='" + textBox5.Text + "'");
textBox3.Text = cache+"出库成功";
}
sql_close_wj(sql_data);
}
}
private void textBox7_KeyPress(object sender, KeyPressEventArgs e)
{
//获取扫码输入
int press_sj = (int)e.KeyChar;
string cache_str;
//回车符输入
if (press_sj == 13)
{
chaxunku = 0;
cache = "";
//textBox2.AppendText(press_sj.ToString());
cache_str = "";
cache_str = textBox7.Text;
textBox7.Text = "";
SqlConnection sql_data = sql_open_wj();//连接数据库
//查询编码是否存在
SqlDataReader sql_jiluji = sql_chaxun_wj(sql_data, "SELECT * FROM kucun WHERE 产品编码='" + cache_str + "'");
if (sql_jiluji.Read())
{
cache = "库位号:" + sql_jiluji[1] + "物料编码:" + sql_jiluji[6];
//继续判断是否已经入库
if (sql_jiluji[3].ToString() == "1")
{
//可以出库
chaxunku = 1;
textBox3.Text = cache;
}
else
{
//未入库
textBox3.Text = "未入库";
chaxunku = 2;
}
}
else
{
textBox3.Text = "不存在编码";
chaxunku = 2;
}
sql_jiluji.Close();
sql_close_wj(sql_data);
}
}
private void textBox8_KeyPress(object sender, KeyPressEventArgs e)
{//获取扫码输入
int press_sj = (int)e.KeyChar;
//回车符输入
if (press_sj == 13)
{//
}
}
private void button1_Click(object sender, EventArgs e)
{ //初始化表格
dataGridView1.Rows.Clear();//清除多余行
int hangshu=0;
String ku_zhuangtai;
//执行查询
SqlConnection sql_data = sql_open_wj();//连接数据库
//查询编码是否存在
SqlDataReader sql_jiluji=sql_chaxun_wj(sql_data,"SELECT * FROM kucun WHERE 产品编码 like '%"+textBox6.Text+"%' and (客牌 like '%"+textBox8.Text+"%' or 客牌 is null)");
while(sql_jiluji.Read())
{
dataGridView1.Rows.Add();
dataGridView1.Rows[hangshu].Cells[0].Value = sql_jiluji[6].ToString();
dataGridView1.Rows[hangshu].Cells[1].Value = sql_jiluji[1].ToString();
if (sql_jiluji[3].ToString()!="1")
{
ku_zhuangtai = "在用";
dataGridView1.Rows[hangshu].DefaultCellStyle.BackColor = Color.Red;
}
else
{
ku_zhuangtai = "在库";
dataGridView1.Rows[hangshu].DefaultCellStyle.BackColor = Color.Green;
}
dataGridView1.Rows[hangshu].Cells[2].Value =ku_zhuangtai;
dataGridView1.Rows[hangshu].Cells[3].Value = sql_jiluji[7].ToString();
dataGridView1.Rows[hangshu].Cells[4].Value = sql_jiluji[8].ToString();
dataGridView1.Rows[hangshu].Cells[5].Value = "出库";
hangshu = hangshu + 1;
}
sql_jiluji.Close();
sql_close_wj(sql_data);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//处理出库
String hanghao=e.RowIndex.ToString();
if (Convert.ToInt32(hanghao)>= 0)
{
String kuweihao = dataGridView1.Rows[Convert.ToInt32(hanghao)].Cells[1].Value.ToString();
SqlConnection sql_data = sql_open_wj();//连接数据库
//执行出库
sql_zhixing_wj(sql_data, "UPDATE kucun SET 入库时间 = GETDATE(), 入库状态 = 2 WHERE 库位号='" + kuweihao + "'");
textBox3.Text = cache + "出库成功";
sql_close_wj(sql_data);
button1_Click(null,null);
}
}
}
}