VS2010 C# SQLServer 开发的 简单的公司员工信息管理系统
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;
using System.IO;
//using 连接数据库;
namespace 连接数据库
{
public partial class workerInfo : Form
{
public workerInfo()
{
InitializeComponent();
}
public static string workername=null;
public static string workerID = null;
public static string picfilename = null;
private void btn_quanbiao_Click(object sender, EventArgs e)
{
WorkerList fr = new WorkerList();
fr.Show();
}
#region 单击查询按钮
private void btn_search_Click(object sender, EventArgs e)
{
string inputtxt = comboBox_input.Text;
if (inputtxt == "请选择")
{
MessageBox.Show("请选择一个查询条件", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
int item = 0;
if (inputtxt == "姓名") item = 1;
if (inputtxt == "新编号") item = 2;
if (inputtxt == "原编号") item = 3;
switch (item)
{
case 1:
inputtxt = "name";
break;
case 2:
inputtxt = "new_id";
break;
case 3:
inputtxt = "old_id";
break;
}
string inputval = cbb_input.Text;
showMessage(inputtxt, inputval);
}
}
#endregion
#region 读取单条记录
public string ShowSign(string xiangmu, string txt, string val)
{
string sign = ""; //获取的记录
SqlConnection mycon = DBLink.linkDB();
try
{
mycon.Open();
}
catch (System.Exception ex)
{
MessageBox.Show("数据库连接失败" ex.Message, "提示");
this.Close();
}
string sql = "select " xiangmu " from workerinfo where " txt " = '" val "'";
try
{
// 查询
SqlCommand command = new SqlCommand(sql, mycon);
SqlDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
sign = Convert.ToString(dataReader[0]);
}
dataReader.Close();
}
catch (System.Exception ex)
{
MessageBox.Show("读取数据时出错/n" ex.Message, "提示");
//this.Close();
}
return sign;
}
#endregion
#region 显示员工信息
public void showMessage(string input, string inputvalue)
{
string inputtxt = input;
string inputval = inputvalue;
lab_age.Text = ShowSign("age", inputtxt, inputval);
lab_college.Text = ShowSign("college", inputtxt, inputval);
lab_infoname.Text = ShowSign("name", inputtxt, inputval);
lab_jiguan.Text = ShowSign("jiguan", inputtxt, inputval);
lab_nowID.Text = ShowSign("new_id", inputtxt, inputval);
lab_oldID.Text = ShowSign("old_id", inputtxt, inputval);
lab_job.Text = ShowSign("job", inputtxt, inputval);
lab_sex.Text = ShowSign("sex", inputtxt, inputval);
lab_job.Text = ShowSign("job", inputtxt, inputval);
lab_people.Text = ShowSign("pep", inputtxt, inputval);
textBox2.Text = ShowSign("licenseID", inputtxt, inputval);
//获取标识符
workerID = lab_nowID.Text;
workername = lab_infoname.Text;
//接下来显示从数据库中获取 的图片。。。。
//////////////////////////////////////////////////////////////////////////
byte[] mypic = GetArrayFromDB();
if (mypic == null)
{
ruku.Enabled = false;
pictureBox1.Image = null;
return;
}
else
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = GetImagenFromArray(mypic);
ruku.Enabled = false;
}
}
#endregion
#region 从数据库中读取图片
private byte[] GetArrayFromDB()
{
byte[] matriz = null;
// string strcon = "Server=" strServerName ";Database=" strDatabase ";User id=" strUser ";Password=" strPsw;
SqlConnection mycon = DBLink.linkDB();
try
{
mycon.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = mycon;
cmd.CommandText = "select pic from workerinfo where name='" workername "'";
string cmdresult = Convert.ToString(cmd.ExecuteScalar());
if (cmdresult == "")
{
return null;
}
else
{
cmd.Parameters.Clear();
//cmd.Parameters.AddWithValue("Descripcion", NombreDeArchivo).SqlDbType = SqlDbType.VarChar;
matriz = (byte[])cmd.ExecuteScalar();
}
}
catch
{
MessageBox.Show("数据库使用出错");
}
finally
{
mycon.Close();
mycon.Dispose();
}
return matriz;
}
private Image GetImagenFromArray(byte[] matriz)
{
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(matriz, 0, matriz.Length);
Image imagen = Image.FromStream(memoryStream);
return imagen;
}
#endregion
#region 载入图片、图片入库
private void 载入照片_Click(object sender, EventArgs e)
{
//Stream myStream = null;
//string fliename = null;
//string filepath = null;
string ImgPath = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "F:\\图片\\图片\\美女2";
openFileDialog1.Filter = "jpge图像文件 (*.jpeg)|*.jpeg|JPG图像文件 (*.jpg)|*.jpg|BMP图像文件 (*.bmp)|(*.bmp)|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.Title = "打开图片";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
ImgPath = openFileDialog1.FileName;
picfilename = ImgPath;
Image img = Image.FromFile(ImgPath);//双引号里是图片的路径
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = img;
// fliename = Path.GetFileName(openFileDialog1.FileName);
// filepath = Path.GetDirectoryName(openFileDialog1.FileName);
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " ex.Message);
}
}
if (pictureBox1.Image != null)
{
ruku.Enabled = true;
}
}
private void ruku_Click(object sender, EventArgs e)
{
if (GetArrayFromDB()!=null)
{
MessageBox.Show("该记录图片已存在,是否替换", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
Image myimage = pictureBox1.Image;
byte[] imgbte = pictureIn.GetArrayFromImagen(myimage);
pictureIn.GuardarImagen(imgbte);
if (GetArrayFromDB()==null)
{
MessageBox.Show("图片没有入库成功", "提示");
}
else
{
MessageBox.Show("图片入库成功", "提示");
}
}
#endregion
#region 自动加载右键选项
/// <summary>
/// 加载右键选项
/// </summary>
public void loadItem()
{
SqlConnection newcon = DBLink.linkDB();
string selitem = comboBox_input.Text;
int item = 0;
if (selitem == "姓名") item = 1;
if (selitem == "新编号") item = 2;
if (selitem == "原编号") item = 3;
switch (item)
{
case 1:
selitem = "name";
break;
case 2:
selitem = "new_id";
break;
case 3:
selitem = "old_id";
break;
}
string selvalue = cbb_input.Text;
string sql = string.Format("select {0} from workerinfo where {1} like '%{2}%' order by {3} ", selitem, selitem, selvalue, selitem);
try
{
newcon.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = newcon;
SqlDataReader reader = cmd.ExecuteReader();
// cbb_input.Items.Clear();
while (reader.Read())
{
cbb_input.Items.Add(reader[0]);
}
reader.Dispose();
reader.Close();
newcon.Dispose();
newcon.Close();
}
catch
{
newcon.Close();
MessageBox.Show("出错");
}
}
/// <summary>
/// 下拉响应事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbb_input_DropDown(object sender, EventArgs e)
{
comboBox_input.Items.Clear();
loadItem();
}
#endregion
#region 得到上个/下个员工的ID
/// <summary>
/// 获取后几位数 public string GetLastStr(string str,int num)
/// </summary>
/// <param name="str">要截取的字符串</param>
/// <param name="num">返回的具体位数</param>
/// <returns>返回结果的字符串</returns>
public string GetLastStr(string str, int num)
{
int count = 0;
if (str.Length > num)
{
count = str.Length - num;
str = str.Substring(count, num);
}
return str;
}
public string nextID(string str)
{
string nextid = null;
nextid = GetLastStr(str, 3);
int tempID = Convert.ToInt16(nextid) 1;
return "A" tempID.ToString().PadLeft(3, '0');
}
public string beforeID(string str)
{
string nextid = null;
nextid = GetLastStr(str, 3);
int tempID = Convert.ToInt16(nextid) - 1;
return "A" tempID.ToString().PadLeft(3, '0');
}
#endregion
#region 单击显示 上一个/下一个 员工信息
private void btn_before_Click(object sender, EventArgs e)
{
string id = null;
if (workerID == null)
{
MessageBox.Show("当前没有显示的员工,请先进行查询操作","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
else
{
id = beforeID(workerID);
if (id == "A000")
{
MessageBox.Show("已经是第一个了");
return;
}
else
{
showMessage("new_id", id);
}
}
}
private void btn_next_Click(object sender, EventArgs e)
{
string id = null;
if (workerID == null)
{
MessageBox.Show("当前没有显示的员工,请先进行查询操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
id = nextID(workerID);
if (id == "A026")
{
MessageBox.Show("已经是最后一个了!!");
return;
}
else
{
showMessage("new_id", id);
}
}
}
#endregion
private void comboBox_input_DropDown(object sender, EventArgs e)
{
//GridItem item = new GridItem();
comboBox_input.Items.Clear();
comboBox_input.Items.Add("姓名");
comboBox_input.Items.Add("新编号");
comboBox_input.Items.Add("原编号");
}
}
}
评论