📄 mainform.cs
字号:
myDataAdapter.UpdateCommand.Parameters.Add("@Oicq", SqlDbType.Char, 10, "Oicq");
myDataAdapter.UpdateCommand.Parameters.Add("@电子邮件", SqlDbType.Char, 100, "电子邮件");
myDataAdapter.UpdateCommand.Parameters.Add("@图片",SqlDbType.Image).SourceColumn = "图片";
myDataAdapter.UpdateCommand.Parameters.Add("@旧姓名", SqlDbType.Char, 50, "姓名").SourceVersion = DataRowVersion.Original;
myDataAdapter.DeleteCommand.Parameters.Add("@姓名", SqlDbType.Char,50, "姓名").SourceVersion = DataRowVersion.Original;
}
public void myUpdate()
{
// 初始化myDataAdapter的SelectCommand
myDataAdapter.SelectCommand.Connection = myConnection;
myDataAdapter.SelectCommand.CommandText= "SELECT * FROM Records WHERE 用户名='" + currentUser + "'";
// 初始化myDataSet
myDataSet.Clear();
// 导入Records表
myDataAdapter.Fill(myDataSet,"Records");
// 初始化TreeView
TreeNode mynode;
DataTable dt = myDataSet.Tables["Records"];
// 把记录信息导入treeView
treeViewMain.BeginUpdate();
// 清楚treeView原本的所有结点
treeViewMain.Nodes.Clear();
foreach (DataRow dr in dt.Rows)
{
mynode = new TreeNode();
mynode.Text = (string)dr["姓名"];
mynode.Text = mynode.Text.Trim();
treeViewMain.Nodes.Add(mynode);
}
treeViewMain.EndUpdate();
// 初始化状态条
statusBarMain.Panels[0].Text = "当前用户: " + currentUser;
statusBarMain.Panels[1].Text =" 共有记录:" + myDataSet.Tables["Records"].Rows.Count.ToString();
statusBarMain.ShowPanels= true;
}
private void treeViewMain_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
// 搜索被点击的记录
DataTable dt = myDataSet.Tables["Records"];
DataColumn[] dc = new DataColumn[1];
dc[0] = dt.Columns["姓名"];
// 设置主键
dt.PrimaryKey = dc;
object key = treeViewMain.SelectedNode.Text.Trim();
// 根据主键进行索引
DataRow dr = dt.Rows.Find(key);
// 修改textBox和comboBox等控件的信息
if (dr != null)
{
textBoxName.Text = (string)dr["姓名"];
if (!dr.IsNull("性别"))
{
comboBoxSex.SelectedIndex = (((string)dr["性别"]).Trim() == "男") ? 0 :1;
}
// 日期数据类型的处理方法
if (!dr.IsNull("出生日期"))
{
DateTime datetime = (DateTime)dr["出生日期"];
textBoxYear.Text = datetime.Year.ToString().Trim();
comboBoxMonth.SelectedIndex = datetime.Month - 1 ;
comboBoxDay.SelectedIndex = datetime.Day - 1 ;
}
// 利用三元运算符设置控件的文本属性
textBoxBelong.Text = dr.IsNull("生肖")? "" : (string)dr["生肖"];
textBoxConstellation.Text = dr.IsNull("星座") ? "" : (string)dr["星座"];
textBoxAddress.Text = dr.IsNull("家庭地址") ? "" : (string)dr["家庭地址"];
textBoxPhone.Text =dr.IsNull("电话") ? "" : (string)dr["电话"];
textBoxMobile.Text =dr.IsNull("手机") ? "" : (string)dr["手机"];
textBoxOicq.Text = dr.IsNull("Oicq") ? "" : (string)dr["Oicq"];
textBoxEmail.Text = dr.IsNull("电子邮件") ? "" : (string)dr["电子邮件"];
// 从DataSet的数据中读入图片
if (!dr.IsNull("图片"))
{
byte[] myData = new byte[0];
myData = (byte[])dr["图片"];
int bmpSize = new int();
bmpSize = myData.Length;
MemoryStream ms = new MemoryStream();
ms.Write(myData,0,bmpSize);
Bitmap bmp;
bmp = new Bitmap(ms);
pictureBoxLook.Image = bmp;
}
// 没有图片的时候将图片设为空
else
{
pictureBoxLook.Image = null;
}
}
// 如果找不到这条记录,只能是新记录,设置对应的属性
else
{
textBoxName.Text = treeViewMain.SelectedNode.Text.Trim();
textBoxYear.Text = "";
comboBoxMonth.SelectedIndex = 0;
comboBoxDay.SelectedIndex = 0;
textBoxBelong.Text = "";
textBoxConstellation.Text = "";
textBoxAddress.Text = "";
textBoxPhone.Text = "";
textBoxMobile.Text = "";
textBoxOicq.Text = "";
textBoxEmail.Text = "";
}
}
private void treeViewMain_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
{
// 修改对应记录的信息
DataTable dt = myDataSet.Tables["Records"];
DataColumn[] dc = new DataColumn[1];
dc[0] = dt.Columns["姓名"];
dt.PrimaryKey = dc;
object key = treeViewMain.SelectedNode.Text.Trim();
DataRow dr = dt.Rows.Find(key);
dr["姓名"] = e.Label.Trim();
// 更新treeView和姓名框
treeViewMain.SelectedNode.Text = e.Label.Trim();
textBoxName.Text = treeViewMain.SelectedNode.Text.Trim();
}
private void mIAddRecord_Click(object sender, System.EventArgs e)
{
// 建立一个新记录的名称
DataTable dt = myDataSet.Tables["Records"];
DataColumn[] dc = new DataColumn[1];
dc[0] = dt.Columns["姓名"];
// 设置主键
dt.PrimaryKey = dc;
object keyName = "新纪录";
DataRow dr;
int i = 1;
do
{
keyName = "新纪录" + i.ToString();
dr = dt.Rows.Find(keyName);
i++;
} while (dr!=null);
// 为TreeView增加一个新结点
TreeNode mynode;
mynode = new TreeNode();
mynode.Text = keyName.ToString();
treeViewMain.BeginUpdate();
treeViewMain.Nodes.Add(mynode);
treeViewMain.SelectedNode = mynode;
treeViewMain.EndUpdate();
// 在DataSet中增加新的一行
DataRow myRow = myDataSet.Tables["Records"].NewRow();
myRow["用户名"] = currentUser;
myRow["姓名"] = mynode.Text.Trim();
myDataSet.Tables["Records"].Rows.Add(myRow);
// 初始化控件信息
comboBoxSex.SelectedIndex = 0 ;
comboBoxMonth.SelectedIndex = 0;
comboBoxDay.SelectedIndex = 0 ;
statusBarMain.Panels[0].Text = "当前用户: " + currentUser;
statusBarMain.Panels[1].Text =" 共有记录:" + myDataSet.Tables["Records"].Rows.Count.ToString();
statusBarMain.ShowPanels= true;
// 更新数据库信息,保持DataSet于数据库的一致
myDataAdapter.Update(myDataSet,"Records");
}
private void buttonSaveChange_Click(object sender, System.EventArgs e)
{
DialogResult result;
// 弹出确认信息
result = MessageBox.Show(this,"确定要修改用户信息吗?", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
if(result == DialogResult.Yes)
{
// 检查姓名字段是否为空
if (textBoxName.Text.Trim() == "")
{
MessageBox.Show("姓名不能为空,请重新输入!","错误");
return;
}
DataTable dt = myDataSet.Tables["Records"];
// 检查该记录的姓名是否可用
bool bExist=false;
foreach (DataRow drw in dt.Rows)
{
if (drw["姓名"] != treeViewMain.SelectedNode.Text.Trim() &&
drw["姓名"] == textBoxName.Text.Trim() )
bExist=true;
}
if (bExist)
{
MessageBox.Show("该记录的姓名已被其它记录使用,请重新选择!","错误");
return;
}
// 修改对应记录的信息
DataColumn[] dc = new DataColumn[1];
dc[0] = dt.Columns["姓名"];
dt.PrimaryKey = dc;
object key = treeViewMain.SelectedNode.Text.Trim();
DataRow dr = dt.Rows.Find(key);
dr["姓名"] = textBoxName.Text.Trim();
dr["性别"] = comboBoxSex.SelectedText;
// DateTime数据类型的处理
if (textBoxYear.Text.Trim()!="")
{
// 对年份进行语法检查
try
{
Convert.ToDecimal(textBoxYear.Text.Trim());
}
catch (FormatException ee)
{
MessageBox.Show("年份输入不正确,请重新输入!","错误");
return;
}
int year,month,day;
year = Convert.ToInt16(textBoxYear.Text);
month = comboBoxMonth.SelectedIndex + 1;
day = comboBoxDay.SelectedIndex + 1;
DateTime datetime = new DateTime(year,month,day);
dr["出生日期"] = datetime;
}
if (textBoxBelong.Text.Trim()!="")
{
dr["生肖"] = textBoxBelong.Text.Trim();
}
if (textBoxConstellation.Text.Trim()!="")
{
dr["星座"] = textBoxConstellation.Text.Trim();
}
if (textBoxAddress.Text.Trim()!="")
{
dr["家庭地址"] = textBoxAddress.Text.Trim();
}
if (textBoxPhone.Text.Trim()!="")
{
dr["电话"] = textBoxPhone.Text.Trim();
}
if (textBoxMobile.Text.Trim()!="")
{
dr["手机"] = textBoxMobile.Text.Trim();
}
if (textBoxOicq.Text.Trim()!="")
{
dr["Oicq"] = textBoxOicq.Text.Trim();
}
if (textBoxEmail.Text.Trim()!="")
{
dr["电子邮件"] = textBoxEmail.Text.Trim();
}
// 文件类型数据的处理
if (pictureBoxLook.Image != null)
{
MemoryStream ms = new MemoryStream();
pictureBoxLook.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
byte[] myData = new Byte[ms.Length];
ms.Position =0;
ms.Read(myData,0,Convert.ToInt32(ms.Length));
dr["图片"] = myData;
ms.Close();
}
// treeView的同步更新
treeViewMain.SelectedNode.Text = textBoxName.Text;
// 更新数据库信息,保持DataSet于数据库的一致
myDataAdapter.Update(myDataSet,"Records");
}
}
private void buttonDelete_Click(object sender, System.EventArgs e)
{
DialogResult result;
// 弹出确认信息
result = MessageBox.Show(this,"确定要删除记录吗?", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
if(result == DialogResult.Yes)
{
// 删除对应记录的信息
DataTable dt = myDataSet.Tables["Records"];
DataColumn[] dc = new DataColumn[1];
dc[0] = dt.Columns["姓名"];
dt.PrimaryKey = dc;
object key = treeViewMain.SelectedNode.Text.Trim();
DataRow dr = dt.Rows.Find(key);
dr.Delete();
treeViewMain.Nodes.Remove(treeViewMain.SelectedNode);
treeViewMain.SelectedNode = treeViewMain.TopNode;
// 更新数据库信息,保持DataSet于数据库的一致
myDataAdapter.Update(myDataSet,"Records");
statusBarMain.Panels[0].Text = "当前用户: " + currentUser;
statusBarMain.Panels[1].Text =" 共有记录:" + myDataSet.Tables["Records"].Rows.Count.ToString();
statusBarMain.ShowPanels= true;
}
}
private void pictureBoxLook_DoubleClick(object sender, System.EventArgs e)
{
openFileDialogPic.ShowDialog();
Bitmap bmp;
bmp =new Bitmap(openFileDialogPic.FileName);
pictureBoxLook.Image = bmp;
}
private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
myDataSet.Clear();
myConnection.Close();
}
private void mIQuit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -