上一篇博客中,我们实现了.net链接数据库实现注册功能呢和登录功能,在我们的注册功能中,出现了下面这么一段代码
string sql = "select *from letter where Username='"+username+"' and Password='"+password+"'";
select在sql中即为查询的用途,这里的查即为这么一个select,之后我们还出现了这么一段代码
string sql = "insert into letter values('" + username + "','" + password1 + "')";
Insert在sql中即为插入的用途,这里的增即为这么一个insert
那么我们所谓的增删查改既然已经实现了增与查,那么删与改又需要这么去实现呢?
1.基础知识
ExecuteNonQuery()
想必大家对这个函数并不陌生,在上一篇博客中出现是是用于返回select的结果,接下来我们来看看这个函数的详细使用方法
string sql = "insert into letter values('" + username + "','" + password1 + "')";
cmd = new SqlCommand(sql, con);
int result = cmd.ExecuteNonQuery();
上面这个是上一次博客里的代码,我们可以看见,在insert使用下,插入成功后sql会返回这么一个描述,这里所返回的result值就是这里红色箭头所指向的值,插入几行就返回什么数字,而返回这个数字也代表着插入成功
而这个函数同样道理也适用于update的改和delete的删操作,唯独select只能使用read的操作
2,delete删除操作
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>选择</title>
</head>
<body>
<form action="/Home/delete">
请输入删除的账户<br />
账户:<input type="text" name="username"><br />
<input type="submit" value="删除">
<input type="button" value="返回" onclick="javascrtpt:window.location.href='operation.html'" />
</form>
</body>
</html>
首先还是老样子,我们定义一个删除的页面,来输入删除的账号,点击删除后进入Home的delect函数运行(不再解释代码了,上一篇博客都有)
当这个页面传输进来用户的时候,我们首先要去判断这个用户在不在我们的数据表里面,有的话删除,没有的话删除失败,一样是select加read的函数去遍历查找利用flag标记数据是否存在,当我们数据存在的时候,就可以使用delete的sql函数进行操作
delete from 表名 where (列名='删除对象')
整个过程数据库打开关闭运行和上一次一样,成功进入成功的界面,反之失败页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>删除成功</title>
</head>
<body>
删除成功
<form action="/Home/Login">
<input type="button" value="返回" onclick="javascrtpt:window.location.href='Login.html'" />
</form>
</body>
</html>
此时删除成功之后,该账户已经不存在,那我们返回登录页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>删除失败</title>
</head>
<body>
删除失败
<form action="/Home/Login">
<input type="button" value="返回" onclick="javascrtpt:window.location.href='close.html'" />
</form>
</body>
</html>
如果删除失败了,我们返回刚刚的选择页面
下面是Home操作下的函数
public void delete()
{
string username = Request.Query["username"].ToString();
SqlConnection con = null;
string conStr = "Data Source =.; Initial Catalog = master; Integrated Security = True";
con = new SqlConnection(conStr);
con.Open();
string Sql = "select* from letter where (Username='" + username + "')";
SqlCommand cmd = new SqlCommand(Sql, con);
int flag = 0;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
flag = 1;
}
con.Close();
if (flag==0)
{
Response.Redirect("http://localhost:5000/deletefail.html");
}else
{
con = null;
conStr = "Data Source =.; Initial Catalog = master; Integrated Security = True";
con = new SqlConnection(conStr);
con.Open();
Sql = "delete from letter where (Username='" + username + "')";
cmd = new SqlCommand(Sql, con);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
Response.Redirect("http://localhost:5000/deletesuccess.html");
}else
{
Response.Redirect("http://localhost:5000/deletefail.html");
}
}
3.update修改
和上面一样雷同
网站显示
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>修改</title>
</head>
<body>
<form action="/Home/operation">
用户名:<input type="text" name="username"><br />
旧密码:<input type="password" name="usedpassword"><br />
新密码:<input type="password" name="nowpassword1"><br />
确认密码:<input type="password" name="nowpassword2"><br />
<input type="submit" value="修改">
<input type="button" value="删除" onclick="javascrtpt:window.location.href='close.html'" />
</form>
</body>
</html>
修改失败
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="/Home/Login">
修改失败<br/>
用户名及密码不正确<br />
密码确认不唯一<br />
<input type="button" value="返回" onclick="javascrtpt:window.location.href='operation.html'" />
</form>
</body>
</html>
修改成功
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>修改成功</title>
</head>
<body>
修改成功;
<form action="/Home/Login">
<input type="button" value="返回" onclick="javascrtpt:window.location.href='operation.html'" />
</form>
</body>
</html>
Home代码
public void operation()
{
string username = Request.Query["username"].ToString();
string usedpassword = Request.Query["usedpassword"].ToString();
string nowpassword1 = Request.Query["nowpassword1"].ToString();
string nowpassword2 = Request.Query["nowpassword2"].ToString();
SqlConnection con = null;
string conStr = "Data Source =.; Initial Catalog = master; Integrated Security = True";
con = new SqlConnection(conStr);
con.Open();
string Sql = "select *from letter where (Username='" + username + "'and Password='" + usedpassword + "')";
SqlCommand cmd = new SqlCommand(Sql, con);
int flag = 0;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
flag = 1;
}
con.Close();
if (flag==0||(nowpassword1!=nowpassword2))
{
Response.Redirect("http://localhost:5000/operationfail.html");
}else if (flag==1&& (nowpassword1 == nowpassword2))
{
con = null;
conStr = "Data Source =.; Initial Catalog = master; Integrated Security = True";
con = new SqlConnection(conStr);
con.Open();
Sql = "update letter set Password='"+nowpassword1+"' where (Username='" + username + "'and Password='" + usedpassword + "')";
cmd = new SqlCommand(Sql, con);
int result = cmd.ExecuteNonQuery();
if (result==1)
{
Response.Redirect("http://localhost:5000/operationsuccess.html");
}else
{
Response.Redirect("http://localhost:5000/operationfail.html");
}
con.Close();
}
}
4.整体修改完整
在上一个博客中,我们登录成功之后是直接跳到简历页面,在这里,我们要修改一下,让其跳到操作选择的页面
登录账号之后进入这个页面可以进行账户的密码修改和用户名注销的功能
5.整体效果
一开始我们的表内为空
此时我注册了一个aaa,111的账户
这里显示注册成功
然后我们登录上来,此时会显示这么一个界面,我们在这里把aaa的密码111改成222
看看数据库表的信息
密码被成功修改了,接下来删除用户aaa
此时数据库恢复平静
6,总结
总体上原理和上一次博客的insert一样,只要将其sql的string语句更改,就可以实现基本的增删改查的功能