Realize additions and deletions to change search link under .net sql case

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43735840/article/details/102710994

On a blog, we realized the .net link database functions to achieve registration and login feature it in our registration function, there has been such a following piece of code

string sql = "select *from letter where Username='"+username+"' and Password='"+password+"'";

In the sql is the use of select query, so here's a check shall select, after we appeared such a code

string sql = "insert into letter values('" + username + "','" + password1 + "')";

Insert the sql that is inserted in use, here is the increase in such an insert
so we called the additions and deletions to change search since it has been realized by the search, then delete and change so they need to achieve it?
1. Basics

ExecuteNonQuery()

Surely we are no stranger to this function, appears on a blog are for return select results, then we look in detail to use this function

string sql = "insert into letter values('" + username + "','" + password1 + "')";
                    cmd = new SqlCommand(sql, con);
                    int result = cmd.ExecuteNonQuery();

Above this is the last blog in the code, we can see, in the insert use, inserted successfully sql returns such a description, where the returned result value is the value where the red arrow is pointing, insert a few lines what returns digital, returns this number also represents the successful insertion
Here Insert Picture Description
and this function is the same thing applies to update the change and delete the delete operation, except select only use the read operation
2, delete 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>

First of all the same, we define a deleted page to enter delete account, click Delete to enter the Home of delect function run (no interpreted code, and has a blog on)
when the page came in the user's transmission time, we first determine the user is not going to our data tables inside, any deletion, if not delete fails, as is the select function to traverse plus read to find out whether the use of flag-tagged data exist, when we present data, using the delete function is operated sql

delete from 表名 where (列名='删除对象')

The whole process open and close the database and run on the same time, successfully entered the success of the interface, whereas failure page

<!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>

After this time successfully deleted, the account does not exist, then we return to the login page

<!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>

If you delete fails, we return to the selection page just
below is a function of operating under the 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 modify the
same as above similar
Site

<!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>

fail to edit

<!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>

Successfully modified

<!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 Code

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. Modify the overall integrity
after on a blog, we are a successful login to jump directly to the resume page, where we want to change it, let it skip operation selected page
Here Insert Picture Description
to enter the page after login account can be the account password change logout functionality and user name
5. the overall effect
beginning within our table is empty
Here Insert Picture Description
at this time I registered a aaa, account 111
Here Insert Picture Description
shown here registration is successful
Here Insert Picture Description
then we log up, a screen appears so, we are here to aaa password change 111 222
Here Insert Picture Description
Here Insert Picture Description
look at the database table information
Here Insert Picture Description
password was successfully changed, then remove users aaa
Here Insert Picture Description
Here Insert Picture Description
this time database restore calm
Here Insert Picture Description
6 summarizes
insert a general principle and on the blog, as long as it's string sql statement change, you can achieve a basic CRUD functions

Guess you like

Origin blog.csdn.net/qq_43735840/article/details/102710994