396 lines
14 KiB
C#
396 lines
14 KiB
C#
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 WindowsFormsApp1.db;
|
||
using WindowsFormsApp1.mod;
|
||
|
||
namespace WindowsFormsApp1.form
|
||
{
|
||
public partial class OEEaddress_Manage : Form
|
||
{
|
||
public OEEaddress_Manage()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
private void 查询_Click(object sender, EventArgs e)
|
||
{
|
||
GetInfo();
|
||
}
|
||
|
||
private void 关闭_Click(object sender, EventArgs e)
|
||
{
|
||
this.Close();
|
||
}
|
||
|
||
private void OEEaddress_Manage_Load(object sender, EventArgs e)
|
||
{
|
||
dataGridView1.AutoGenerateColumns = false;
|
||
SQLiteHelper<t_plc> sQLiteHelper1 = new db.SQLiteHelper<t_plc>();
|
||
var result_combox = sQLiteHelper1.QueryAll();
|
||
if (!result_combox.Success)
|
||
{
|
||
MessageBox.Show("查询失败");
|
||
return;
|
||
}
|
||
|
||
var result1 = new List<object>
|
||
{
|
||
new {name = "read",value = "read"},
|
||
new {name = "write",value = "write"}
|
||
};
|
||
|
||
var result2 = new List<object>
|
||
{
|
||
new {name = "real",value = "real"},
|
||
new {name = "string",value = "string"},
|
||
new {name = "uint",value = "uint"},
|
||
new {name = "bool",value = "bool"},
|
||
new {name = "int",value = "int"},
|
||
new {name = "dint",value = "dint"},
|
||
new {name = "byte",value = "byte"},
|
||
};
|
||
|
||
var comboBoxColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns[3];
|
||
// 绑定数据源到 ComboBox
|
||
comboBoxColumn.DataSource = result1;
|
||
comboBoxColumn.DisplayMember = "name";
|
||
comboBoxColumn.ValueMember = "value";
|
||
|
||
comboBoxColumn.DataPropertyName = "type";
|
||
|
||
var comboBoxColumn1 = (DataGridViewComboBoxColumn)dataGridView1.Columns[5];
|
||
// 绑定数据源到 ComboBox
|
||
comboBoxColumn1.DataSource = result2;
|
||
comboBoxColumn1.DisplayMember = "name";
|
||
comboBoxColumn1.ValueMember = "value";
|
||
|
||
comboBoxColumn1.DataPropertyName = "addresstype";
|
||
|
||
DataGridViewButtonColumn modifyColumn = new DataGridViewButtonColumn();
|
||
modifyColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
|
||
modifyColumn.Name = "ModifyColumn";
|
||
modifyColumn.HeaderText = "修改";
|
||
modifyColumn.Text = "修改";
|
||
modifyColumn.UseColumnTextForButtonValue = true;
|
||
dataGridView1.Columns.Add(modifyColumn);
|
||
|
||
// 添加删除按钮列
|
||
DataGridViewButtonColumn deleteColumn = new DataGridViewButtonColumn();
|
||
deleteColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
|
||
deleteColumn.Name = "DeleteColumn";
|
||
deleteColumn.HeaderText = "删除";
|
||
deleteColumn.Text = "删除";
|
||
deleteColumn.UseColumnTextForButtonValue = true;
|
||
|
||
dataGridView1.CellContentClick += DataGridView1_CellContentClick;
|
||
|
||
dataGridView1.Columns.Add(deleteColumn);
|
||
|
||
GetInfo();
|
||
}
|
||
|
||
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
|
||
{
|
||
// 检查点击的是否为按钮列
|
||
if (e.RowIndex >= 0)
|
||
{
|
||
DataGridView dataGridView = (DataGridView)sender;
|
||
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
|
||
|
||
int selectid = int.Parse(row.Cells[0].Value.ToString());
|
||
if (e.ColumnIndex == 10)
|
||
{
|
||
OEEaddress_Insert oeeInsert = new OEEaddress_Insert();
|
||
oeeInsert.dataid = selectid;
|
||
oeeInsert.Operation = OperationType.Edit;
|
||
oeeInsert.ShowDialog();
|
||
GetInfo();
|
||
}
|
||
else if (e.ColumnIndex == 11)
|
||
{
|
||
DialogResult result = MessageBox.Show("你确定要删除数据吗?", "确认提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
|
||
|
||
if (result == DialogResult.OK)
|
||
{
|
||
SQLiteHelper<t_oeeaddress> sQLiteHelper = new db.SQLiteHelper<t_oeeaddress>();
|
||
var delresult = sQLiteHelper.Delete(selectid);
|
||
if (delresult.Success)
|
||
{
|
||
MessageBox.Show("删除成功");
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("删除失败");
|
||
}
|
||
GetInfo();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public void GetInfo()
|
||
{
|
||
try
|
||
{
|
||
dataGridView1.DataSource = null;
|
||
SQLiteHelper<t_oeeaddress> sQLiteHelper = new db.SQLiteHelper<t_oeeaddress>();
|
||
var result = sQLiteHelper.QueryAll();
|
||
if (result.Success)
|
||
{
|
||
List<object> list = new List<object>();
|
||
foreach (var plcaddress in result.Data)
|
||
{
|
||
list.Add(new
|
||
{
|
||
id = plcaddress.id,
|
||
plcid = plcaddress.plcid,
|
||
code = plcaddress.code,
|
||
type = plcaddress.type,
|
||
dbaddress = plcaddress.dbaddress,
|
||
addresstype = plcaddress.addresstype,
|
||
addresstypelegth = plcaddress.addresstypelegth,
|
||
addressoffset = plcaddress.addressoffset,
|
||
remark = plcaddress.remark,
|
||
value = plcaddress.value,
|
||
addressdecimal = plcaddress.addressdecimal
|
||
});
|
||
}
|
||
dataGridView1.DataSource = list;
|
||
dataGridView1.Refresh();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"查询失败,错误原因:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
private void button1_Click(object sender, EventArgs e)
|
||
{
|
||
OEEaddress_Insert oeeInsert = new OEEaddress_Insert();
|
||
oeeInsert.ShowDialog();
|
||
GetInfo();
|
||
}
|
||
|
||
// 添加导入按钮点击事件处理
|
||
private void 导入_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
OpenFileDialog openFileDialog = new OpenFileDialog();
|
||
openFileDialog.Filter = "CSV文件|*.csv";
|
||
openFileDialog.Title = "选择要导入的CSV文件";
|
||
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
string filePath = openFileDialog.FileName;
|
||
ImportFromCSV(filePath);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"导入失败,错误原因:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
private void ImportFromCSV(string filePath)
|
||
{
|
||
try
|
||
{
|
||
List<t_oeeaddress> addressList = new List<t_oeeaddress>();
|
||
|
||
// 读取CSV文件 - 修改编码方式
|
||
// 尝试使用UTF-8编码读取
|
||
string[] lines = System.IO.File.ReadAllLines(filePath, Encoding.UTF8);
|
||
|
||
// 如果检测到乱码,可以尝试其他编码
|
||
if (ContainsGarbledText(lines))
|
||
{
|
||
// 尝试使用GB2312编码
|
||
lines = System.IO.File.ReadAllLines(filePath, Encoding.GetEncoding("gb2312"));
|
||
|
||
// 如果仍然乱码,再尝试其他编码
|
||
if (ContainsGarbledText(lines))
|
||
{
|
||
// 尝试使用GBK编码
|
||
lines = System.IO.File.ReadAllLines(filePath, Encoding.GetEncoding("gbk"));
|
||
}
|
||
}
|
||
|
||
// 从第2行开始读取数据(假设第1行是标题)
|
||
for (int i = 1; i < lines.Length; i++)
|
||
{
|
||
string line = lines[i];
|
||
if (string.IsNullOrWhiteSpace(line))
|
||
continue;
|
||
|
||
// 分割CSV行并去除双引号
|
||
string[] values = SplitCsvLineAndRemoveQuotes(line);
|
||
|
||
if (values.Length < 10)
|
||
{
|
||
MessageBox.Show($"第{i + 1}行数据格式不正确,请检查CSV文件格式");
|
||
break;
|
||
}
|
||
|
||
var test = values[0];
|
||
try
|
||
{
|
||
t_oeeaddress address = new t_oeeaddress
|
||
{
|
||
plcid = Convert.ToInt32(values[0]),
|
||
code = values[1],
|
||
type = values[2],
|
||
dbaddress = values[3],
|
||
addresstype = values[4],
|
||
addresstypelegth = Convert.ToInt32(values[5]),
|
||
addressoffset = Convert.ToInt32(values[6]),
|
||
addressdecimal = Convert.ToInt32(values[7]),
|
||
remark = values[8] ?? "",
|
||
};
|
||
|
||
addressList.Add(address);
|
||
}
|
||
catch (FormatException)
|
||
{
|
||
MessageBox.Show($"第{i + 1}行数据格式转换错误,请检查数据类型是否正确");
|
||
break;
|
||
}
|
||
}
|
||
|
||
SaveImportedData(addressList);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"CSV导入失败,错误原因:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
// 添加一个方法来分割CSV行并去除双引号
|
||
private string[] SplitCsvLineAndRemoveQuotes(string line)
|
||
{
|
||
// 使用正则表达式处理CSV,考虑引号内的逗号
|
||
List<string> result = new List<string>();
|
||
bool inQuotes = false;
|
||
StringBuilder field = new StringBuilder();
|
||
|
||
for (int i = 0; i < line.Length; i++)
|
||
{
|
||
char c = line[i];
|
||
|
||
if (c == '"')
|
||
{
|
||
// 如果是引号,切换引号状态
|
||
inQuotes = !inQuotes;
|
||
// 不将引号添加到结果中
|
||
}
|
||
else if (c == ',' && !inQuotes)
|
||
{
|
||
// 如果是逗号且不在引号内,则添加当前字段并重置
|
||
result.Add(field.ToString().Trim());
|
||
field.Clear();
|
||
}
|
||
else
|
||
{
|
||
// 其他字符直接添加
|
||
field.Append(c);
|
||
}
|
||
}
|
||
|
||
// 添加最后一个字段
|
||
result.Add(field.ToString().Trim());
|
||
|
||
return result.ToArray();
|
||
}
|
||
|
||
// 添加一个辅助方法检测文本是否包含乱码
|
||
private bool ContainsGarbledText(string[] lines)
|
||
{
|
||
// 简单检测:如果包含特定的乱码字符,则认为是乱码
|
||
// 这里只是一个简单的示例,可能需要根据实际情况调整
|
||
string pattern = @"[\u0000-\u001F\u007F-\u009F]"; // 控制字符
|
||
|
||
foreach (string line in lines)
|
||
{
|
||
if (System.Text.RegularExpressions.Regex.IsMatch(line, pattern))
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
private void SaveImportedData(List<t_oeeaddress> addressList)
|
||
{
|
||
if (addressList.Count == 0)
|
||
{
|
||
MessageBox.Show("没有有效数据可导入");
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
SQLiteHelper<t_oeeaddress> sQLiteHelper = new db.SQLiteHelper<t_oeeaddress>();
|
||
var result = sQLiteHelper.InsertAll(addressList);
|
||
|
||
if (result.Success)
|
||
{
|
||
// 更新缓存
|
||
var allAddresses = sQLiteHelper.QueryAll();
|
||
if (allAddresses.Success)
|
||
{
|
||
Init.t_Oeeaddresses = allAddresses.Data;
|
||
}
|
||
|
||
MessageBox.Show($"成功导入{addressList.Count}条数据");
|
||
GetInfo(); // 刷新数据显示
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show($"导入失败:{result.Msg}");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"保存导入数据失败,错误原因:{ex.Message}");
|
||
}
|
||
}
|
||
// 添加清空按钮点击事件处理
|
||
private void 清空_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
DialogResult result = MessageBox.Show("确定要删除所有数据吗?此操作不可恢复!", "警告",
|
||
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
|
||
|
||
if (result == DialogResult.OK)
|
||
{
|
||
SQLiteHelper<t_oeeaddress> sQLiteHelper = new db.SQLiteHelper<t_oeeaddress>();
|
||
var deleteResult = sQLiteHelper.DeleteAll();
|
||
|
||
if (deleteResult.Success)
|
||
{
|
||
MessageBox.Show("所有数据已成功删除!");
|
||
GetInfo(); // 刷新数据显示
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show($"删除失败:{deleteResult.Msg}");
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"清空数据失败,错误原因:{ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
} |