版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的
图99A-32供应商管理窗口设计
全部代码如下:
OleDbConnection connection;
private void FormSupplier_Load(object sender, EventArgs e)
{
connection = new OleDbConnection(classMod.databaseConnString);
//打开数据连接
connection.Open();
fillLv();
}
public static void ShowForm()
{
FormSupplier frm = new FormSupplier();
frm.ShowDialog();
}
//填充数据选项,主要是 lvUser
private void fillLv()
{
lvSupplier.Items.Clear();
//新建OleDbCommand对象实例
OleDbCommand command = new OleDbCommand();
//=========填充用户列表==================
//要执行的SQL查询
command.CommandText = "select 供应商ID,公司名称,联系人姓名,联系人职务,地址,电话,是否停用 from 供应商";
//设置OleDbCommand的数据连接为OleDbConnection
command.Connection = connection;
//声明OleDbDataReader对象
OleDbDataReader odReader;
//通过OleDbCommand的ExecuteReader方法获得OleDbDataReader对象实例。
odReader = command.ExecuteReader();
//如果OleDbDataReader中包含数据
if (odReader.HasRows)
{
//循环读取每一行数据,直到Read方法返回False
while (odReader.Read())
{
ListViewItem lvi = new ListViewItem(odReader.GetValue(0).ToString());
lvi.SubItems.Add(odReader.GetValue(1).ToString());
lvi.SubItems.Add(odReader.GetValue(2).ToString());
lvi.SubItems.Add(odReader.GetValue(3).ToString());
lvi.SubItems.Add(odReader.GetValue(4).ToString());
lvi.SubItems.Add(odReader.GetValue(5).ToString());
lvi.SubItems.Add(odReader.GetValue(6).ToString());
lvSupplier.Items.Add(lvi);
}
}
//关闭数据读取器
odReader.Close();
}
//点击lvGoods中的项目,将数据添加到对应位置
private void lvSupplier_MouseClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo lvhti = lvSupplier.HitTest(e.X, e.Y);
if (lvhti.Item == null)
return;
txtName.Text = lvhti.Item.SubItems[1].Text;
txtContacts.Text = lvhti.Item.SubItems[2].Text;
txtJob.Text = lvhti.Item.SubItems[3].Text;
txtAddr.Text = lvhti.Item.SubItems[4].Text;
txtTel.Text = lvhti.Item.SubItems[5].Text;
}
//增加供应商信息
private void btnAdd_Click(object sender, EventArgs e)
{
string errMsg = checkData();
if (errMsg != "")
{
MessageBox.Show(errMsg);
return;
}
//新建OleDbCommand对象实例
OleDbCommand command = new OleDbCommand();
//设置OleDbCommand的数据连接为OleDbConnection
command.Connection = connection;
//供应商公司名称
string supplierName = txtName.Text.Trim();
//检查是否已经存在该供应商
for (int i = 0; i < lvSupplier.Items.Count; i++)
{
if (supplierName == lvSupplier.Items[i].SubItems[1].Text)
if (MessageBox.Show("该供应商已经存在,是否继续添加?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
return;
}
//供应商联系人姓名
string supplierContacts = txtContacts.Text.Trim();
//供应商联系人职务
string supplierJob = txtJob.Text.Trim();
//供应商地址
string supplierAddr = txtAddr.Text.Trim();
//供应商电话号码
string supplierTel = txtTel.Text.Trim();
string sqlString;
sqlString = "insert into 供应商(公司名称,联系人姓名,联系人职务,地址,电话,是否停用) " +
"values('" + supplierName + "','" + supplierContacts + "','" + supplierJob + "','" + supplierAddr + "','" + supplierTel + "','否')";
command.CommandText = sqlString;
//不管是新增还是修改,都不用返回值,所以使用ExecuteNonQuery。
command.ExecuteNonQuery();
//刷新列表中的数据
fillLv();
}
//检查数据合法性
private string checkData()
{
if (txtName.Text.Trim() == "")
return "供应商公司名称不能为空";
if (txtContacts.Text.Trim() == "")
return "供应商联系人姓名不能为空";
if (txtJob.Text.Trim() == "")
return "供应商联系人职务不能为空";
if (txtAddr.Text.Trim() == "")
return "供应商地址不能为空";
if (txtTel.Text.Trim() == "")
return "供应商电话号码不能为空";
return "";
}
//修改供应商信息
private void btnEdit_Click(object sender, EventArgs e)
{
if (lvSupplier.SelectedItems.Count < 1)
{
MessageBox.Show("未选择需要修改的数据");
return;
}
string errMsg = checkData();
if (errMsg != "")
{
MessageBox.Show(errMsg);
return;
}
//新建OleDbCommand对象实例
OleDbCommand command = new OleDbCommand();
//设置OleDbCommand的数据连接为OleDbConnection
command.Connection = connection;
//供应商ID
int supplierID = int.Parse(lvSupplier.SelectedItems[0].Text);
//供应商公司名称
string supplierName = txtName.Text.Trim();
//供应商联系人姓名
string supplierContacts = txtContacts.Text.Trim();
//供应商联系人职务
string supplierJob = txtJob.Text.Trim();
//供应商地址
string supplierAddr = txtAddr.Text.Trim();
//供应商电话号码
string supplierTel = txtTel.Text.Trim();
string sqlString;
sqlString = "update 供应商 set 公司名称='" + supplierName +
"',联系人姓名='" + supplierContacts +
"',联系人职务='" + supplierJob +
"',地址='" + supplierAddr +
"',电话='" + supplierTel +
"',是否停用='否') where 供应商ID=" + supplierID;
command.CommandText = sqlString;
//不管是新增还是修改,都不用返回值,所以使用ExecuteNonQuery。
command.ExecuteNonQuery();
//刷新列表中的数据
fillLv();
}
private void FormSupplier_FormClosing(object sender, FormClosingEventArgs e)
{
connection.Close();
}
//停用供应商
//存在的数据通常已经关联了很多其它表的数据,因此,一般不要轻易设置删除
private void btnIsUse_Click(object sender, EventArgs e)
{
if( lvSupplier.SelectedItems.Count < 1 )
{
MessageBox.Show("未选择需要修改的数据");
return;
}
//供应商ID
int supplierID = int.Parse(lvSupplier.SelectedItems[0].Text);
//供应商公司名称
string supplierName = lvSupplier.SelectedItems[0].SubItems[1].Text;
string isUseOld = lvSupplier.SelectedItems[0].SubItems[6].Text;
string isUseNew = isUseOld == "否" ? "是" : "否";
string operation = isUseOld == "否" ? "停用" : "启用";
if (MessageBox.Show("确实要" + operation + "供应商 " + supplierName + " 吗?", operation + "供应商", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
return;
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "update 供应商 set 是否停用='" + isUseNew + "' where 供应商ID=" + supplierID;
command.ExecuteNonQuery();
//刷新lvUser中的显示
fillLv();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
学习更多vb.net知识,请参看vb.net 教程 目录
学习更多C#知识,请参看C#教程 目录