📄 changefriend.cs
字号:
this.label13.ForeColor = System.Drawing.Color.Black;
this.label13.Location = new System.Drawing.Point(16, 120);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(85, 23);
this.label13.TabIndex = 2;
this.label13.Text = "固定电话:";
//
// label14
//
this.label14.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label14.ForeColor = System.Drawing.Color.Black;
this.label14.Location = new System.Drawing.Point(24, 72);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(64, 23);
this.label14.TabIndex = 1;
this.label14.Text = "姓名:";
//
// btnchange
//
this.btnchange.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnchange.Location = new System.Drawing.Point(304, 400);
this.btnchange.Name = "btnchange";
this.btnchange.Size = new System.Drawing.Size(88, 32);
this.btnchange.TabIndex = 6;
this.btnchange.Text = "更新";
this.btnchange.Click += new System.EventHandler(this.btnchange_Click);
//
// btndel
//
this.btndel.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btndel.Location = new System.Drawing.Point(392, 400);
this.btndel.Name = "btndel";
this.btndel.Size = new System.Drawing.Size(96, 32);
this.btndel.TabIndex = 7;
this.btndel.Text = "删除";
this.btndel.Click += new System.EventHandler(this.btndel_Click);
//
// btnexit
//
this.btnexit.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnexit.Image = ((System.Drawing.Image)(resources.GetObject("btnexit.Image")));
this.btnexit.Location = new System.Drawing.Point(488, 400);
this.btnexit.Name = "btnexit";
this.btnexit.Size = new System.Drawing.Size(88, 32);
this.btnexit.TabIndex = 8;
this.btnexit.Text = "退出";
this.btnexit.Click += new System.EventHandler(this.btnexit_Click);
//
// btnnext
//
this.btnnext.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnnext.Location = new System.Drawing.Point(216, 400);
this.btnnext.Name = "btnnext";
this.btnnext.Size = new System.Drawing.Size(88, 32);
this.btnnext.TabIndex = 9;
this.btnnext.Text = "下一条";
this.btnnext.Click += new System.EventHandler(this.btnnext_Click);
//
// btnpre
//
this.btnpre.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnpre.Location = new System.Drawing.Point(128, 400);
this.btnpre.Name = "btnpre";
this.btnpre.Size = new System.Drawing.Size(88, 32);
this.btnpre.TabIndex = 10;
this.btnpre.Text = "上一条";
this.btnpre.Click += new System.EventHandler(this.btnpre_Click);
//
// changefriend
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.btnsearch;
this.ClientSize = new System.Drawing.Size(752, 461);
this.Controls.Add(this.btnpre);
this.Controls.Add(this.btnnext);
this.Controls.Add(this.btnexit);
this.Controls.Add(this.btndel);
this.Controls.Add(this.btnchange);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox2);
this.Name = "changefriend";
this.Text = "修改信息";
this.Load += new System.EventHandler(this.changefriend_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 查询当前记录的id标示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public int searchid()
{
int id2=0;
if (txtname.Text=="")
{
MessageBox.Show("更改之前,请查询被更改记录的姓名值!");
return 0;
}
else
{
string str="select id from stuinf where name='" + txtname.Text;
str +="'";
Classdb searchid=new Classdb();
searchcon=searchid.conds();
searchcon=searchid.conds();
SqlDataReader sqldr;
SqlCommand sqlcom=new SqlCommand(str,searchcon);
try
{
searchcon.Open();
sqldr=sqlcom.ExecuteReader();
while(sqldr.Read())
{
id2=Convert.ToInt16(sqldr[0]);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
searchcon.Close();
sqlcom.Dispose();
}
return id2;
}
}
#region 查询符合查询条件的记录
private void btnsearch_Click(object sender, System.EventArgs e)
{
ds.Clear();
string amountlike;//确定查询条件是等于还是类似
string fields;//纪录查询字段
string findvalue;//纪录查询字段的值;
if (cmbsearch.Text=="姓名")
{
fields="name";
}
else if(cmbsearch.SelectedText=="电话号码")
{
fields="telephone";
}
else if(cmbsearch.SelectedText=="手机号码")
{
fields="mobilephone";
}
else if(cmbsearch.SelectedText=="QQ号码")
{
fields="qq";
}
else if(cmbsearch.SelectedText=="电子邮件")
{
fields="email";
}
else
{
MessageBox.Show("请从下拉列表中选择查询字段!");
return;
}
if (txtsearchvalue.Text=="")
{
MessageBox.Show("请输入查询值");
return;
}
if (cmbsearch2.Text=="等于")
{
amountlike="=";
findvalue="'" + txtsearchvalue.Text;
findvalue += "'";
}
else
{
amountlike="like";
findvalue="'%" + txtsearchvalue.Text;
findvalue += "%'";
}
string strsearch="select name,images,mobilepho,telephone,email,adddate,qq,nowaddress,familyaddress,others from stuinf where ";
strsearch +=fields;
strsearch +=" ";
strsearch +=amountlike;
strsearch +=" ";
strsearch +=findvalue;
Classdb searchdb=new Classdb();
searchcon=searchdb.conds();
SqlDataAdapter da=new SqlDataAdapter(strsearch,searchcon);
// da.SelectCommand.CommandText=strsearch;
//da.SelectCommand.Connection=searchcon;
// int i;
try
{
searchcon.Open();
da.Fill(ds);
count=ds.Tables[0].Rows.Count;
if (count==0)
{
MessageBox.Show("没有找到相关纪录,请重新输入查询条件进行查询!");
label3.Text="没有找到任何记录!";
}
else
{
DataRow dr;
dr=ds.Tables[0].Rows[0];
txtname.Text=dr["name"].ToString();
txtimages.Text=dr["images"].ToString();
txtmobilephone.Text=dr["mobilepho"].ToString();
txttelephone.Text=dr["telephone"].ToString();
txtemail.Text=dr["email"].ToString();
txtadddate.Text=dr["adddate"].ToString();
txtqq.Text=dr["qq"].ToString();
txtnowaddress.Text=dr["nowaddress"].ToString();
txtfamilyaddress.Text=dr["familyaddress"].ToString();
txtothers.Text=dr["others"].ToString();
label3.Text="共找到 " + count.ToString();
label3.Text += "条记录,当前显示的是第 1 条记录";
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
searchcon.Close();
da.Dispose();
}
}
#endregion
#region 显示查询结果中的上条记录
private void btnpre_Click(object sender, System.EventArgs e)
{
int i=0;
j=j-1;
if(j>=i)
{
DataRow dr;
dr=ds.Tables[0].Rows[j];
txtname.Text=dr["name"].ToString();
txtimages.Text=dr["images"].ToString();
txtmobilephone.Text=dr["mobilepho"].ToString();
txttelephone.Text=dr["telephone"].ToString();
txtemail.Text=dr["email"].ToString();
txtadddate.Text=dr["adddate"].ToString();
txtqq.Text=dr["qq"].ToString();
txtnowaddress.Text=dr["nowaddress"].ToString();
txtfamilyaddress.Text=dr["familyaddress"].ToString();
txtothers.Text=dr["others"].ToString();
int x;
x=j+1;
label3.Text="共 ";
label3.Text += count.ToString();
label3.Text +=" 条记录,";
label3.Text +="当前显示的是第 ";
label3.Text += x.ToString();
label3.Text +=" 条记录";
}
else
{
MessageBox.Show("已经是第一条纪录了!");
}
}
#endregion
#region 显示查询结果中的下条记录
private void btnnext_Click(object sender, System.EventArgs e)
{
j=j+1;
int i=ds.Tables[0].Rows.Count;
if(j<i)
{
//MessageBox.Show(j.ToString());
DataRow dr;
dr=ds.Tables[0].Rows[j];
txtname.Text=dr["name"].ToString();
txtimages.Text=dr["images"].ToString();
txtmobilephone.Text=dr["mobilepho"].ToString();
txttelephone.Text=dr["telephone"].ToString();
txtemail.Text=dr["email"].ToString();
txtadddate.Text=dr["adddate"].ToString();
txtqq.Text=dr["qq"].ToString();
txtnowaddress.Text=dr["nowaddress"].ToString();
txtfamilyaddress.Text=dr["familyaddress"].ToString();
txtothers.Text=dr["others"].ToString();
label3.Text="共 ";
int x;
x=j+1;
label3.Text += count.ToString();
label3.Text +=" 条记录,";
label3.Text +="当前显示的是第 ";
label3.Text +=x.ToString();
label3.Text +=" 条记录";
}
else
{
MessageBox.Show("已经是最后一条纪录了!");
}
}
#endregion
#region 修改记录
private void btnchange_Click(object sender, System.EventArgs e)
{
int changenum;//记录被更新的记录数;
int currentid=searchid();
if (currentid!=0)
{
string updatestr; //定义更新语句
updatestr="update stuinf set name='" + txtname.Text + "',images='";
updatestr +=txtimages.Text + "',mobilepho='" + txtmobilephone.Text;
updatestr +="',telephone='" + txttelephone.Text + "',email='";
updatestr +=txtemail.Text + "',qq='" + txtqq.Text + "',adddate='";
updatestr +=txtadddate.Text + "',nowaddress='" + txtnowaddress.Text;
updatestr +="',familyaddress='" + txtfamilyaddress.Text + "',others='";
updatestr +=txtothers.Text + "' where id=" + currentid;
// MessageBox.Show(updatestr);
Classdb updatedb=new Classdb();
searchcon=updatedb.conds();
SqlCommand sqlcom=new SqlCommand(updatestr,searchcon);
try
{
searchcon.Open();
changenum=sqlcom.ExecuteNonQuery();
if(changenum==0)
{
MessageBox.Show("更新失败!" );
}
else
{
MessageBox.Show("更新成功!");
}
}
finally
{
searchcon.Dispose();
sqlcom.Dispose();
}
}
}
#endregion
#region 删除记录
private void btndel_Click(object sender, System.EventArgs e)
{
int currentid=searchid();
int i;//记录受该操作影响的行数;
if (MessageBox.Show("当前记录即将被删除,一旦删除,将不可恢复,确定删除吗?","删除记录",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation)==DialogResult.Yes)
{
Classdb deldb=new Classdb();
searchcon=deldb.conds();
String delstr="delect from stuinf where id=" + currentid;
SqlCommand sqlcom=new SqlCommand(delstr,searchcon);
try
{
searchcon.Open();
i=sqlcom.ExecuteNonQuery();
if(i==0)
{
MessageBox.Show("删除失败!");
}
else
{
MessageBox.Show("删除成功!");
}
}
catch
{
MessageBox.Show("删除成功!");
}
finally
{
searchcon.Dispose();
sqlcom.Dispose();
}
}
}
#endregion
private void btnexit_Click(object sender, System.EventArgs e)
{
this.Dispose();
}
private void changefriend_Load(object sender, System.EventArgs e)
{
label3.Text=""; //初始化查询结果值
txtname.Enabled=false; //使姓名字段不可修改
}
private void txtimages_TextChanged(object sender, System.EventArgs e)
{
try
{
if(txtimages.Text!="")
{
pictureBox1.Image=Image.FromFile(txtimages.Text);
}
else
{
pictureBox1.Image=Image.FromFile("images\\no.gif");
}
}
catch
{
pictureBox1.Image=Image.FromFile("images\\no.gif");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -