概述
这里我选择使用的源数据库是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;
}
}
}
}
}