實驗7 ADO.NET管理數據庫

概述

這裏我選擇使用的源數據庫是vs2019自帶的sqlserver。
如何創建:點擊跳轉
注意細節,如果要將id設置爲自增字段,需要改這裏
在這裏插入圖片描述
好了,進入正題:



代碼以及運行截圖

1. 實現數據庫的增刪查改功能

(1)註冊(向數據庫中添加記錄)

亂碼參考:點擊跳轉
運行截圖:
在這裏插入圖片描述
在這裏插入圖片描述


(2)登錄(從數據庫中查詢記錄),要求採用DataReader對象。

在這裏插入圖片描述
在這裏插入圖片描述

(3)修改密碼(修改數據庫中的記錄)

在這裏插入圖片描述
在這裏插入圖片描述

(4)註銷(刪除數據庫中的記錄)

在這裏插入圖片描述
在這裏插入圖片描述
代碼:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
namespace WebApplication6
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string username = TextBox1.Text;
            string password = TextBox2.Text;
            string connString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            SqlConnection con = new SqlConnection(connString);
            con.Open();
            string sql = @"insert into person (username,password) values ("+"'"+username+"'"+","+"'"+password+"'"+")";
            SqlCommand sqlCommand = new SqlCommand(sql, con);
            sqlCommand.ExecuteNonQuery();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string username = TextBox1.Text;
            string password = TextBox2.Text;
            string connString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            SqlConnection con = new SqlConnection(connString);
            con.Open();
            string sql = @"select password from person where username="+"'"+username+"'";
            SqlCommand sqlCommand = new SqlCommand(sql, con);
            SqlDataReader reader = sqlCommand.ExecuteReader();
            string password1="";
            if (reader.Read())
                password1 = reader.GetString(0);
            if (password.Equals(password1)) {
                MessageBox.Show("登錄成功");
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string username = TextBox1.Text;
            string password = TextBox2.Text;
            string connString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            SqlConnection con = new SqlConnection(connString);
            con.Open();
            string sql = @"update person set password=" + "'" + password + "' " + "where username='" + username+"'";
            SqlCommand sqlCommand = new SqlCommand(sql, con);
            sqlCommand.ExecuteNonQuery();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            string username = TextBox1.Text;
            string password = TextBox2.Text;
            string connString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            SqlConnection con = new SqlConnection(connString);
            con.Open();
            string sql = @"delete from person where username='" + username + "'";
            SqlCommand sqlCommand = new SqlCommand(sql, con);
            sqlCommand.ExecuteNonQuery();
        }
    }
}

2. 採用ExecuteScalar方法,改寫(2)中登錄功能的代碼。

如何彈出消息框,參考:點擊跳轉
在這裏插入圖片描述
代碼:

protected void Button2_Click(object sender, EventArgs e)
        {
            string username = TextBox1.Text;
            string password = TextBox2.Text;
            string connString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            SqlConnection con = new SqlConnection(connString);
            con.Open();
            string sql = @"select password from person where username="+"'"+username+"'";
            SqlCommand sqlCommand = new SqlCommand(sql, con);
            string password1 = (string)sqlCommand.ExecuteScalar();
            if (password.Equals(password1)) {
                MessageBox.Show("登錄成功");
            }
        }

3. 採用DataAdapter對象和DataSet對象,改寫(2)中登錄功能的代碼。

在這裏插入圖片描述
代碼:

 protected void Button2_Click(object sender, EventArgs e)
        {
            string username = TextBox1.Text;
            string password = TextBox2.Text;
            string connString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            SqlConnection con = new SqlConnection(connString);
            con.Open();
            string sql = @"select password from person where username="+"'"+username+"'";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "person");
            string password1 = "";
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                password1 = row[0].ToString();
            }
            if (password.Equals(password1)) {
                MessageBox.Show("登錄成功");
            }
        }

4. 首先附加“IPAddress”數據庫到SQL Server2008中,數據庫中“IP”表的結構如圖27所示,部分數據如圖28所示,建立如圖所示的頁面,輸入IP地址,將查詢結果顯示在Label控件中,如圖29所示,要求採用DataReader對象。

在這裏插入圖片描述
在這裏插入圖片描述
代碼:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication5
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string connString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            SqlConnection con = new SqlConnection(connString);
            con.Open();
            string sql= @"select * from IPAddress";
            SqlCommand sqlCommand = new SqlCommand(sql, con);
            SqlDataReader reader = sqlCommand.ExecuteReader();
            string or = TextBox1.Text;
            while (reader.Read())
            {
                string start= reader.GetString(0);
                string end = reader.GetString(1);
                if (or.CompareTo(start) >= 0 && or.CompareTo(end) <= 0) {
                    Label1.Text = reader.GetString(2);
                    break;
                }
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章