c#+ SQL 平板生產出庫工具
1.執行窗體效果圖:
2.From1 SOURCE CODE:
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.IO;
using System.Windows;
using System.Data.SqlClient;
using System.Threading;
namespace EX_warehouseScan_V1._00
{
public partial class Form1 : Form
{
public struct Read_Ex_warehouse//儲存讀取的SN資訊
{
public String OrderNumber;
public String BoxNumber;
public List<String> SN;
}
public Form1()
{
String strFullPath = Application.ExecutablePath;//讀取執行路徑
String strFileName = System.IO.Path.GetFileName(strFullPath);//讀取程式名稱
String FileName = strFileName.Substring(0,strFileName.Length-4);
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName(FileName);
if (myProcesses.Length > 1)//判斷程式是否啟動
{
MessageBox.Show("程式已啟動", "系統提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(0);
}
else
{
InitializeComponent();
}
}
private void Form1_Load(object sender, EventArgs e)
{
InitListView();
}
public void InitListView()//初始化ListView表名
{
listView1.Columns.Add("NO.",60,HorizontalAlignment.Left);
listView1.Columns.Add("OrderNumber",160,HorizontalAlignment.Left);
listView1.Columns.Add("BoxNumber",200,HorizontalAlignment.Left);
listView1.Columns.Add("SN",200,HorizontalAlignment.Left);
}
public String[] BaseData_Login = { "NB_SERVER", "E_Panel", "sa", "adminsystem"};
public String FailNumber_Box = String.Empty;
public String FailSN = String.Empty;
public Boolean ReadDataBase_Data_EX_warehouse(List<Read_Ex_warehouse> BoxNumber_Array)//判斷是否資料已出庫
{
Boolean Flag = false;
foreach (Read_Ex_warehouse rew_str in BoxNumber_Array)
{
foreach (String rew_str_ss in rew_str.SN)
{
String StoredProcedure_str_dl="usp_Find_Ex_warehouse_SN_Info";
DataBasesProcess MyBoxNumber_dl = new DataBasesProcess(this.BaseData_Login[0], this.BaseData_Login[1], this.BaseData_Login[3], this.BaseData_Login[2], StoredProcedure_str_dl);
if (MyBoxNumber_dl.Find_Ex_warehouse_SN_Info(rew_str_ss) == false)
{
Flag = false;
FailSN = rew_str_ss;
FailNumber_Box = rew_str.BoxNumber;
return Flag;
}
}
Flag = true;
}
return Flag;
}
public void InsertListViewItem(List<Read_Ex_warehouse> Source_SN)//將資料插入ListView1表中
{
listView1.Enabled = true;
ListViewItem item = new ListViewItem();
int n = 1;
foreach (Read_Ex_warehouse tt in Source_SN)
{
foreach (String aa in tt.SN)
{
item = listView1.Items.Add(n.ToString());
item.SubItems.Add(tt.OrderNumber);
item.SubItems.Add(tt.BoxNumber);
item.SubItems.Add(aa);
n++;
}
}
}
public Boolean Inert_EX_warehouse_Data(List<Read_Ex_warehouse> Source_SN)//插入出庫資料
{
Boolean Flag = false;
String StoredProcedure_str_3 = "usp_Insert_Ex_warehouse_SN_Info";
DataBasesProcess MyBoxNumber_3 = new DataBasesProcess(this.BaseData_Login[0], this.BaseData_Login[1], this.BaseData_Login[3], this.BaseData_Login[2], StoredProcedure_str_3);
foreach (Read_Ex_warehouse ss in Source_SN)
{
foreach (String nn in ss.SN)
{
if (MyBoxNumber_3.Insert_Ex_warehouse_SN_Info(ss.OrderNumber, ss.BoxNumber, nn) == false)
{
Flag = false;
return Flag;
}
}
}
Flag = true;
return Flag;
}
public void ShowLogInfo(float X, float Y, String FailText,Color Mycolor)
{
label2.Left = Convert.ToInt32(groupBox3.Width * X);
label2.Top = Convert.ToInt32(groupBox3.Height * Y);
textBox1.SelectAll();//選中文字框內容
textBox1.Focus();//指定文字框為焦點
label2.ForeColor = Mycolor;
label2.Text = FailText;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
label2.Text = "";//設定日誌內容為空
if (e.KeyChar == (char)Keys.Enter)
{
listView1.Clear();//清空資料表資料
InitListView();//初始化表頭
String StoredProcedure_str_1 = "usp_Find_TieYards_30_Info";
DataBasesProcess MyBoxNumber_1 = new DataBasesProcess(this.BaseData_Login[0], this.BaseData_Login[1], this.BaseData_Login[3], this.BaseData_Login[2], StoredProcedure_str_1);
if (MyBoxNumber_1.Find_TieYards_30_Info(textBox1.Text.Trim()) == true)//檢查30碼合一記錄是否存在
{
List<Read_Ex_warehouse> MyBoxNumberInfo = new List<Read_Ex_warehouse>();
String StoredProcedure_str_2 = "SELECT * FROM TieYards_30 WHERE BoxNumber="[email protected]"'"+textBox1.Text.Trim()[email protected]"'";
DataBasesProcess MyBoxNumber_2 = new DataBasesProcess(this.BaseData_Login[0], this.BaseData_Login[1], this.BaseData_Login[3], this.BaseData_Login[2], StoredProcedure_str_2);
if (MyBoxNumber_2.Get_Ex_warehouse_SN_Info(MyBoxNumberInfo) == true)//讀取30合一資料
{
if (ReadDataBase_Data_EX_warehouse(MyBoxNumberInfo) == true)
{
if (Inert_EX_warehouse_Data(MyBoxNumberInfo) == true)
{
InsertListViewItem(MyBoxNumberInfo);
ShowLogInfo(0.2f, 0.5f, "外箱條碼為:" + textBox1.Text + "出庫掃描成功!!", Color.Green);
}
else
{
ShowLogInfo(0.2f, 0.5f, "外箱條碼為:" + textBox1.Text + "出庫資料儲存過程出錯!!", Color.Red);
}
}
else
{
ShowLogInfo(0.2f, 0.5f, "外箱條碼為:" + textBox1.Text + "已經掃描出庫!!", Color.Red);
}
}
else
{
ShowLogInfo(0.05f, 0.5f, "連線資料庫讀取外箱條碼為:" + textBox1.Text + " 30合一資料錯誤!!",Color.Red);
}
}
else
{
ShowLogInfo(0.1f, 0.5f, "無法從資料庫找到外箱條碼為:" + textBox1.Text + " 資料來源!!",Color.Red);
}
}
}
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
listView1.FullRowSelect = true;
if (this.listView1.SelectedItems.Count > 0)
{
listView1.SelectedItems[0].SubItems[0].ForeColor = Color.Blue;
//先清除原有格式
foreach (ListViewItem item in listView1.Items)
{
item.ForeColor = Color.Black;
}
foreach (ListViewItem item in listView1.Items)
{
item.BackColor = Color.White;
Font f = new Font(Control.DefaultFont, FontStyle.Regular);
item.Font = f;
}
//加粗字型
Font f2 = new Font(Control.DefaultFont, FontStyle.Bold);
listView1.SelectedItems[0].SubItems[0].Font = f2;
//設定選中行背景顏色
listView1.SelectedItems[0].BackColor = Color.LightBlue;
listView1.SelectedItems[0].Selected = false;
}
}
}
}
3.DataBasesProcess Source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Collections;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Web;
namespace EX_warehouseScan_V1._00
{
public class DataBasesProcess:Form1
{
public String DATABASE;
public String PWD;
public String UID;
public String SERVER;
public String StoredProcedure_str;
public DataBasesProcess(String SERVER, String DATABASE, String PWD, String UID, String StoredProcedure_str)
{
this.SERVER = SERVER;
this.DATABASE = DATABASE;
this.PWD = PWD;
this.UID = UID;
this.StoredProcedure_str = StoredProcedure_str;
}
public Boolean Find_TieYards_30_Info(String BoxNumber)//查詢資料庫是否存在外箱資訊
{
Boolean Flag = false;
String Sqlconnectionstr = "server=" + this.SERVER + ";database=" + this.DATABASE + ";uid=" + this.UID + ";pwd=" + this.PWD;
SqlConnection conn = new SqlConnection(Sqlconnectionstr);
conn.Open();
try
{
SqlCommand cmd = new SqlCommand(this.StoredProcedure_str,conn);
cmd.CommandType = CommandType.StoredProcedure;//啟動資料庫儲存過程
cmd.Parameters.Add("@BoxNumber", BoxNumber);
cmd.Parameters.Add("@rs", 1);//設定返回引數
cmd.Parameters["@rs"].Direction = ParameterDirection.Output;//啟動輸出返回
cmd.ExecuteScalar();
if ((int)cmd.Parameters["@rs"].Value == 0)
{
conn.Close();
Flag = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "系統提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
Flag = false;
}
return Flag;
}
public Boolean Find_Ex_warehouse_SN_Info(String SN)//查詢資料庫是否該SN已出庫
{
Boolean Flag = false;
String Sqlconnectionstr = "server=" + this.SERVER + ";database=" + this.DATABASE + ";uid=" + this.UID + ";pwd=" + this.PWD;
SqlConnection conn = new SqlConnection(Sqlconnectionstr);
conn.Open();
try
{
SqlCommand cmd = new SqlCommand(this.StoredProcedure_str,conn);
cmd.CommandType = CommandType.StoredProcedure;//啟動資料庫儲存過程
cmd.Parameters.Add("@SN",SN);
cmd.Parameters.Add("@rs",1);//設定返回引數
cmd.Parameters["@rs"].Direction = ParameterDirection.Output;//啟動輸出返回
cmd.ExecuteScalar();
if ((int)cmd.Parameters["@rs"].Value == 0)
{
conn.Close();
Flag = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "系統提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
Flag = false;
}
return Flag;
}
public Boolean Insert_Ex_warehouse_SN_Info(String OrderNumber, String BoxNumber, String SN)//出庫掃描資料插入數庫
{
Boolean Flag = false;
String Sqlconnectionstr = "server=" + this.SERVER + ";database=" + this.DATABASE + ";uid=" + this.UID + ";pwd=" + this.PWD;
SqlConnection conn = new SqlConnection(Sqlconnectionstr);
conn.Open();
try
{
SqlCommand cmd = new SqlCommand(this.StoredProcedure_str,conn);
cmd.CommandType = CommandType.StoredProcedure;//啟動資料庫存過程
cmd.Parameters.Add("@OrderNumber",OrderNumber);
cmd.Parameters.Add("@BoxNumber", BoxNumber);
cmd.Parameters.Add("@SN",SN);
cmd.Parameters.Add("@rs",1);//設定返回引數
cmd.Parameters["@rs"].Direction = ParameterDirection.Output;//啟動輸出返回
cmd.ExecuteScalar();
if ((int)cmd.Parameters["@rs"].Value == 0)
{
conn.Close();
Flag = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "系統提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
Flag = false;
}
return Flag;
}
public Boolean Get_Ex_warehouse_SN_Info(List<Read_Ex_warehouse> Ex_warehouse_Data)//讀取資料並存儲於List<Read_Ex_warehouse>中
{
Boolean Flag = false;
String Sqlconnectionstr = "server=" + this.SERVER + ";database=" + this.DATABASE + ";uid=" + this.UID + ";pwd=" + this.PWD;
SqlConnection conn = new SqlConnection(Sqlconnectionstr);
conn.Open();
try
{
SqlCommand cmd = conn.CreateCommand();//建立資料庫命令
cmd.CommandText = StoredProcedure_str;// "SELECT * FROM TieYards_30 WHERE BoxNumber="+BoxNumber;//建立查詢語句
SqlDataReader reader = cmd.ExecuteReader();//從資料庫中讀取資料流存入reader中
//從reader中讀取下一行資料,如果沒有資料read.Read()返回flase
String[] Title_SN = { "SN_1", "SN_2", "SN_3", "SN_4", "SN_5", "SN_6", "SN_7", "SN_8", "SN_9", "SN_10", "SN_11" ,
"SN_12","SN_13","SN_14","SN_15","SN_16","SN_17","SN_18","SN_19","SN_20","SN_21","SN_22",
"SN_23","SN_24","SN_25","SN_26","SN_27","SN_28","SN_29","SN_30"};//設定關鍵字名
while (reader.Read())
{
Read_Ex_warehouse Temp = new Read_Ex_warehouse();
Temp.OrderNumber = reader.GetString(reader.GetOrdinal("OrderNumber"));
Temp.BoxNumber = reader.GetString(reader.GetOrdinal("BoxNumber"));
List<String> Temp_SN = new List<string>();
foreach (String ss in Title_SN)
{
if (reader.GetString(reader.GetOrdinal(ss)) != "NULL")
{
Temp_SN.Add(reader.GetString(reader.GetOrdinal(ss)));
}
Temp.SN = Temp_SN;
}
Ex_warehouse_Data.Add(Temp);
}
conn.Close();
Flag = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "系統提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
Flag = false;
}
return Flag;
}
}
}
4.SQL 程式碼圖: