先简单介绍一下我创建的窗体一些控件:
还在功能实现阶段,界面还没有美化,所以界面比较丑,原谅一下。
为了编码、便于记忆的方便,逐个将文本框的名字进行了修改。
(1)对数据库中表的内容进行查找:
代码如下:
private void button1_Click(object sender, EventArgs e)
{
string connStr = @"Server=.; Initial Catalog=E-Genealogy; Integrated Security=True";//integrated security=true:集成验证,使用Windows验证的方式去连接到数据库服务器。
string id = txtB2.Text;//数据库中成员ID是主键,也就是txtB2中的内容是成员ID
SqlConnection conn = new SqlConnection(connStr);//建立一个b数据库连接对象conn
conn.Open();//一个方法,用于打开conn所连接的数据库
string sql = @"SELECT * FROM Member WHERE Member_ID='" + txtB2.Text + "'AND Genealogy_ID='"+txtB1.Text+"'";//通过成员ID和族谱ID来进行条件比较
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
if (!dr.Read())
{
MessageBox.Show("该成员不存在!");//未查找到就弹出提示框
return;
}
//查找成功就在相应文本框中输入信息
txtB3.Text = dr["Member_ID"].ToString();//将表格中成员ID输入到txtB3文本框中用于显示查找结果
txtB4.Text = dr["Member_name"].ToString();
txtB5.Text = dr["Member_sex"].ToString();
txtB6.Text = dr["Member_birth"].ToString();
txtB7.Text = dr["Member_died"].ToString();
txtB8.Text = dr["Member_origin"].ToString();
txtB9.Text = dr["Member_address"].ToString();
txtB10.Text = dr["Member_live"].ToString();
txtB11.Text = dr["Member_father"].ToString();
txtB12.Text = dr["Member_spouseID"].ToString();
txtB13.Text = dr["Genealogy_ID"].ToString();
conn.Close();
}
**string sql = @“SELECT * FROM Member WHERE Member_ID=’” + txtB2.Text + “‘AND Genealogy_ID=’”+txtB1.Text+"’";**这一句中有两个判定条件中间用AND连接。特别注意“=”右边的标点符号:先是一对单引号,里面再加入一对双引号。
(2)对表中内容进行修改:
private void button3_Click(object sender, EventArgs e)
{
string connStr = @"Server=.; Initial Catalog=E-Genealogy; Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string sql = @"UPDATE Member SET [Member_name]='" + txtB4.Text + "',[Member_sex]='" + txtB5.Text + "',[Member_birth]='" + txtB6.Text + "',[Member_died]='" + txtB7.Text + "',[Member_origin]='" + txtB8.Text + "',[Member_address]='" + txtB9.Text + "',[Member_live]='" + txtB10.Text + "',[Member_father]='" + txtB11.Text + "',[Member_spouseID]='" + txtB12.Text + "'";
sql += " WHERE [Member_ID]='" + txtB2.Text + "'"; //判定条件,根据成员ID来对成员信息进行修改
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("成员信息修改成功!");
}
catch (Exception ex)
{
MessageBox.Show("更新出错!" + ex.Message);
}
conn.Close();
}
string sql = @“UPDATE Member SET [Member_name]=’” + txtB4.Text + “’,[Member_sex]=’” + txtB5.Text + “’,[Member_birth]=’” + txtB6.Text + “’,[Member_died]=’” + txtB7.Text + “’,[Member_origin]=’” + txtB8.Text + “’,[Member_address]=’” + txtB9.Text + “’,[Member_live]=’” + txtB10.Text + “’,[Member_father]=’” + txtB11.Text + “’,[Member_spouseID]=’” + txtB12.Text + “’”;这一句是对表中9列属性进行修改(其中一个主键、一个外键不用修改)
特别注意属性与属性之间的标点符号
[Member_sex]=’" + txtB5.Text + "’,
等号左边是表中属性名,等号右边先加一对单引号,再加一对双引号,双引号里面为相应文本框的内容,最后加逗号将其与后面的属性分隔开。
(3)对表中内容进行删除:
删除一个成员比较简单,代码如下:
private void button4_Click(object sender, EventArgs e)
{
bool flag = false;//作为删除结果的标志信息
string connStr = @"Server=.; Initial Catalog=E-Genealogy; Integrated Security=True";
string id = txtB2.Text;//通过成员ID这个主键来进行条件比较
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string sql = @"DELETE FROM Member WHERE Member_ID='" + id + "'";//通过成员ID主键进行删除
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show(id + "号成员删除成功!");
flag = true;//删除成功,标志变为true
}
catch (Exception ex)
{
MessageBox.Show("删除出错!" + ex.Message);
}
conn.Close();
//如果删除成,将文本框中的内容变为“***”,覆盖掉原来信息
if(flag)
{
txtB3.Text = "***";
txtB4.Text = "***";
txtB5.Text = "***";
txtB6.Text = "***";
txtB7.Text = "***";
txtB8.Text = "***";
txtB9.Text = "***";
txtB10.Text = "***";
txtB11.Text = "***";
txtB12.Text = "***";
txtB13.Text = "***";
}
(4)向表中添加一行多列数据:
private void button2_Click(object sender, EventArgs e)
{
string connStr = @"Server=.; Initial Catalog=E-Genealogy; Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string sql = "insert into Member(Member_ID,Member_name,Member_sex,Member_live,Member_spouseID,Member_father,Member_address,Member_origin,Member_birth,Member_died,Genealogy_ID) values('" + txtB3.Text + "', '" + txtB4.Text + "', '" + txtB5.Text + "', '" + txtB10.Text + "', '" + txtB12.Text + "', '" + txtB11.Text + "', '" + txtB9.Text + "', '" + txtB8.Text + "', '" + txtB6.Text + "', '" + txtB7.Text + "', '" + txtB13.Text + "')";//插入信息
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("信息已存入数据库!");
}
catch (Exception msg)
{
MessageBox.Show("出问题了!\n出错原因:" + msg.Message);
}
conn.Close();
}
string sql = “insert into Member(Member_ID,Member_name,Member_sex,Member_live,Member_spouseID,Member_father,Member_address,Member_origin,Member_birth,Member_died,Genealogy_ID) values(’” + txtB3.Text + “’, '” + txtB4.Text + “’, '” + txtB5.Text + “’, '” + txtB10.Text + “’, '” + txtB12.Text + “’, '” + txtB11.Text + “’, '” + txtB9.Text + “’, '” + txtB8.Text + “’, '” + txtB6.Text + “’, '” + txtB7.Text + “’, '” + txtB13.Text + “’)”;
这一句同样要注意列与列之间的符号问题,容易出错。