Ado.net[增删改查,GET传值]
1.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="_20160520.Home" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="css/gv.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div class="gv">
<table>
<tr>
<td>用户名:</td>
<td>
<asp:TextBox ID="TxtUserName" runat="server" Width="132px"></asp:TextBox>
</td>
<td>班级:</td>
<td>
<asp:DropDownList ID="ddlsclass" runat="server">
<asp:ListItem>---请选择---</asp:ListItem>
<asp:ListItem>微软一班</asp:ListItem>
<asp:ListItem>微软二班</asp:ListItem>
<asp:ListItem>微软三班</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:Button ID="btnsel" runat="server" Text="查询" OnClick="btnsel_Click" />
</td>
</tr>
</table>
<table>
<tr>
<td>用户名:</td>
<td>
<asp:TextBox ID="TxtUsersName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<asp:TextBox ID="TxtPwd" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>姓名:</td>
<td>
<asp:TextBox ID="Txtloginname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>QQ:</td>
<td>
<asp:TextBox ID="Txtqq" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>班级:</td>
<td>
<asp:DropDownList ID="ddlclass" runat="server">
<asp:ListItem>---请选择---</asp:ListItem>
<asp:ListItem>微软一班</asp:ListItem>
<asp:ListItem>微软二班</asp:ListItem>
<asp:ListItem>微软三班</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="" style="text-align: right">
<asp:Button ID="BtnAdd" runat="server" Text="添加" OnClick="BtnAdd_Click"/>
</td>
</tr>
</table>
<table>
<tr>
<td>ID:</td>
<td>
<asp:TextBox ID="TxtDuserid" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="BtnDel" runat="server" Text="删除" OnClick="BtnDel_Click" />
</td>
</tr>
</table>
<table>
<tr>
<td> ID</td>
<td>
<asp:TextBox ID="TxtuUserId" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> 用户名:</td>
<td>
<asp:TextBox ID="TxtuUserName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="" style="text-align: right">
<asp:Button ID="BtnUpdate" runat="server" Text="更新" OnClick="Button1_Click" />
</td>
</tr>
</table>
<asp:GridView ID="IdGridView" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" />
<asp:BoundField DataField="username" HeaderText="用户名" />
<asp:BoundField DataField="PWD" HeaderText="密码" />
<asp:BoundField DataField="loginname" HeaderText="姓名" />
<asp:BoundField DataField="qq" HeaderText="QQ" />
<asp:BoundField DataField="classname" HeaderText="班级" />
<asp:TemplateField HeaderText="详情">
<ItemTemplate>
<a href="UserInfo.aspx?id=<%#Eval("ID") %>" target="_blank">详情</a>
<%--<a href='UserInfo.aspx?userid=<%#Eval("UserId") %>' target="_blank">详情</a
<%-- <a href="one.aspx?">详情</a>--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView> </div>
</form>
</body>
</html>
2.跳转的另一个页面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Text; namespace _20160520
{
public partial class Home : System.Web.UI.Page
{
//假如页面多,每个页面都要写一个连接字符串,所以我们一般放在配置文件中
//string strstring = "server=.; Database=MyBookShop;Integrated Security=false;Uid=sa;Pwd=123456";
// string connStr = "Database=Student;Server=.;Integrated Security=false;Uid=sa;Password=123;"; //通过这个类ConfigurationManager获取web.confiig里面的节点名称来获取连接的字符串
private string conStr = ConfigurationManager.ConnectionStrings["mySchool"].ToString();
//把这个字符串传进去
//SqlConnection con = new SqlConnection(conStr);
SqlConnection con = null;//相当于是电话
SqlCommand cmd = null;//执行sql语句
SqlDataReader dr = null;//用于储存查询结果 private void openDB()
{
con = new SqlConnection(conStr);
con.Open();//和数据库建立起了连接
//我们单独把这两句话封装起来直接调用就好
}
//页面一运行就执行这里面的内容
protected void Page_Load(object sender, EventArgs e)
{
BindUser();
}
public void BindUser()
{
try
{
openDB();
//得到sql语句
//string sql = "select loginid,name,loginpwd,address,ClassName,mail from Users";
string sql = Getswhere();
//执行sql语句
using (cmd = new SqlCommand(sql, con))
//对象有了,我们要通过对象去执行sql语句
{
using (dr = cmd.ExecuteReader())
{
IdGridView.DataSource = dr;
IdGridView.DataBind();
}
//dr.Dispose();//释放资源
//dr.Close();//关闭连接
//con.Dispose();
//con.Close();
}
}
catch
{
Response.Write("网站正在维护中.......!");
}
}
protected void btnsel_Click(object sender, EventArgs e)
{ }
//我们需要得到where后面
public string Getswhere()
{
StringBuilder sb = new StringBuilder();
sb.Append("select ID,username,PWD,loginname,qq,classname from Users where 1=1");
//获取到它的用户名
string username = TxtUserName.Text.Trim();
if (!string.IsNullOrEmpty(username))
{
sb.Append(string.Format("and username='{0}'", username));
}
if(ddlsclass.SelectedIndex>)
{
sb.Append(string.Format("and ClassName='{0}'", ddlsclass.SelectedValue));
}
return sb.ToString();
} //添加
protected void BtnAdd_Click(object sender, EventArgs e)
{
try
{
//要获取到文本框里面的内容
string username = TxtUsersName.Text.Trim();
string pwd = TxtPwd.Text.Trim();
string loginname = Txtloginname.Text.Trim();
string qq = Txtqq.Text.Trim();
//ddlclass.SelectedIndex > 0 ? ddlclass.SelectedValue "" : ;判断它是否被选中,如果选中>0,就等于选中的内容,如果没有被选中的话,就为空
string classname = ddlclass.SelectedIndex > ? ddlclass.SelectedValue: "";
//写sql语句
string sql = string.Format("insert Users (username,PWD,loginname,qq,classname) values ('{0}','{1}','{2}','{3}','{4}')", username, pwd, loginname, qq, classname);
//sql语句有了之后怎么做添加呢?
//第一步和数据库建立起连接
openDB();
//连接建立起来之后,我们创建一个sqlcommand对象
using (cmd = new SqlCommand(sql, con))
{
//这时候我们怎么通过对象去执行它的插入呢?ExecuteNonQuery执行增删改查语句,返回的是int类型
cmd.ExecuteNonQuery();
}
//直接绑定
BindUser();
}
catch
{
Response.Write("网站正在维护中.......!");
}
} //删除
protected void BtnDel_Click(object sender, EventArgs e)
{
int userid = ;//userid默认等于0
//有可能输入的不是整数,不是整数的话,输入之后它有可能发生异常
try
{
//TxtDuserid.Text.Trim()==""?,如果它的内容等于空的话,我们就让它等于0(?0),不为空的话,我们就Convert(将一个数据类型转为另一个基本数据类型)一下
userid = TxtDuserid.Text.Trim() == "" ? : Convert.ToInt32(TxtDuserid.Text.Trim());
}
catch
{
userid = ;
}
//id就得到了,判断数据库里面有没有
string s1 = string.Format("select ID from users where ID={0}", userid);
//和数据库建立起连接
openDB();
using (cmd = new SqlCommand(s1, con))
{
dr = cmd.ExecuteReader();
//查询有没有数据
if (dr.HasRows)
{
//如果有,就执行删除语句,得到一个s2
string s2 = string.Format("delete Users where ID={0}",userid);
dr.Dispose();
dr.Close();
using(cmd = new SqlCommand(s2, con))
{
cmd.ExecuteNonQuery();
}
}
else
{
//没有就
Response.Write("该用户ID不存在!");
}
}
BindUser();
} //更新
protected void Button1_Click(object sender, EventArgs e)
{
int userid = ;//userid默认等于0
//有可能输入的不是整数,不是整数的话,输入之后它有可能发生异常
try
{
//TxtDuserid.Text.Trim()==""?,如果它的内容等于空的话,我们就让它等于0(?0),不为空的话,我们就Convert(将一个数据类型转为另一个基本数据类型)一下
userid = TxtuUserId.Text.Trim() == "" ? : Convert.ToInt32(TxtuUserId.Text.Trim());
}
catch
{
userid = ;
}
//id就得到了,判断数据库里面有没有
string s1 = string.Format("select ID from users where ID={0}", userid);
//和数据库建立起连接
openDB();
using (cmd = new SqlCommand(s1, con))
{
dr = cmd.ExecuteReader();
//查询有没有数据
if (dr.HasRows)
{
//如果有,就执行删除语句,得到一个s2
string s2 = string.Format("update Users set username='{0}' where ID={1}", TxtuUserName.Text.Trim(),userid);
dr.Dispose();
dr.Close();
using (cmd = new SqlCommand(s2, con))
{
cmd.ExecuteNonQuery();
}
}
else
{
//没有就
Response.Write("该用户ID不存在!");
}
}
BindUser();
} }
}
3.Get传值:跳转页面.cs页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Configuration;
using System.Data.SqlClient; namespace _20160520
{
public partial class UserInfo : System.Web.UI.Page
{
private string conStr = ConfigurationManager.ConnectionStrings["mySchool"].ToString();
//把这个字符串传进去
//SqlConnection con = new SqlConnection(conStr);
SqlConnection con = null;//相当于是电话
SqlCommand cmd = null;//执行sql语句
SqlDataReader dr = null;//用于储存查询结果
private void openDB()
{
con = new SqlConnection(conStr);
con.Open();//和数据库建立起了连接
//我们单独把这两句话封装起来直接调用就好
} //private int _id = 0;
//public int id
//{
// get
// {
// //return _userId;
// //传过来的id有可能为空,也有可能不是整数
// //它也有可能不是整数,我们就写一个异常
// try
// {
// //判断它是否为空?get的作用是属性,属性是判断它的读和写,Request.QueryString["ID"]里的id要与Eval("ID")的一致
// //==nul等于空,就让它等于0,不等于空,
// _id = Request.QueryString["ID"] == null ? 0 : Convert.ToInt32(Request.QueryString["ID"].ToString());
// }
// catch
// {
// _id = 0;
// }
// return _id;//这样就得到了传过来的id
// }
// set { _id = value; }
//}
private int _id = ; public int Id
{
get
{
try
{
_id = Request.QueryString["ID"] == null ? : Convert.ToInt32(Request.QueryString["ID"].ToString());
}
catch
{
_id = ;
}
return _id;
}
set { _id = value; }
}
protected void Page_Load(object sender, EventArgs e)
{ } //这样就得到了传过来的id,拼凑出来的字符串显示在前端,更多使用这个GridView只能显示表格的形式
public string GetUserInfor()
{
//拼凑字符串需要引用命名空间using System.Text;
StringBuilder sb = new StringBuilder();
//先得到内容
//判断一下id是否>0,>0我们在拼凑
if (Id > )
{
sb.Append(@"<div class=""da"">");
sb.Append("<table>");
openDB();
string sql = string.Format("select * from users where ID={0}",Id);
using (cmd = new SqlCommand(sql, con))
{
using (dr = cmd.ExecuteReader())
{
//dr = cmd.ExecuteReader();
//对这里面的内容进行读数据,读数据就要进行拼凑字符串
if (dr.Read())
{
//数据里面的用户名怎么获取到?
sb.Append(string.Format("<tr><td>用户名:</td><td>{0}</td></tr>", dr["userName"].ToString()));
sb.Append(string.Format("<tr><td>密码:</td><td>{0}</td></tr>", dr["PWD"].ToString()));
sb.Append(string.Format("<tr><td>姓名:</td><td>{0}</td></tr>", dr["loginname"].ToString()));
sb.Append(string.Format("<tr><td>QQ:</td><td>{0}</td></tr>", dr["qq"].ToString()));
sb.Append(string.Format("<tr><td>班级:</td><td>{0}</td></tr>", dr["classname"].ToString()));
sb.Append(string.Format("<tr><td>信息:</td><td>{0}</td></tr>", dr["information"].ToString()));
}
}
}
sb.Append("</table>"); sb.Append("</div>");
}
return sb.ToString();
}
}
}
4. 跳转页面前端页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfo.aspx.cs" Inherits="_20160520.UserInfo" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%--后台有个字符串怎么显示在前端?前端怎么调用后台页面--%>
<%=GetUserInfor() %>
</div>
</form>
</body>
</html>
效果显示:


Ado.net[增删改查,GET传值]的更多相关文章
- ADO.NET 增删改查的基本用法
ADO.NET:数据访问技术 就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中也可以将数据库中的数据提取到内存中供程序调用 所有数据访问技术的基础 连接 ...
- LinQ和ADO.Net增删改查 备忘
是否些倦了 SqlConnection conn=new SqlConnection();一系列繁冗的代码? 来试试Linq吧 查: using System.Data.SqlClient; name ...
- ADO.net 增删改查
ADO.net 一.定义:编程开发语言与数据库连接的一门语言技术 二.链接: 在vs中操作数据库需在开头进行链接 链接内容:using System.Data.SqlClient 三.引用数据库: 四 ...
- ado.net增删改查练习
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- ado.net增删改查操作
ado.net是数据库访问技术将数据库中的数据,提取到内存中,展示给用户看还可以将内存中的数据写入数据库中去 并不是唯一的数据库访问技术,但是它是最底层最基础的数据库访问技术 使用ado.net对数据 ...
- ADO.net 增删改查封装DBhelper
using System; using System.Collections.Generic; using System.Data.SqlClient;//引用数据库客户端 using System. ...
- WebForm增删改查
最基本的,拼接字符串在Literal里面显示表,IsPostBack,增删改查基本,?传值 Request接收 LinQ to SQL类 在Default主页里面拖入Literal控件再加入一个按钮, ...
- Ado.net[登录,增删改查,Get传值,全选,不选,批量删除,批量更新]
[虽然说,开发的时候,我们可以使用各种框架,ado.net作为底层的东西,作为一个合格的程序员,在出问题的时候我们还是要知道如何调试] 一.增删改查 cmd.ExecuteReader();执行查询, ...
- ado.net C#如何实现数据库增删改查
ado.net数据库访问技术将数据库中的数据,提取到内存中,展示给用户看还可以将内存中的数据写入数据库中去 并不是唯一的数据库访问技术,但是它是最底层的数据库访问技术也就是说是最麻烦但是是最不可缺少的 ...
随机推荐
- [数据库连接池二]Java数据库连接池--C3P0和JDNI.
前言:上一篇文章中讲了DBCP的用法以及实现原理, 这一篇再来说下C3P0和JDNI的用法. 1.1.C3P0数据源 C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规 ...
- C#打开新页面
最常用的页面跳转(原窗口被替代):Response.Redirect("XXX.aspx"); 利用url地址打开本地网页或互联网:Respose.Write("< ...
- 解决TryUpdateModel对象为空的问题
MVC中的TryUpdateModel确实给开发带来不少便利,但是如果绑定在View上的文本控件没有填写值的时候,再执行TryUpdateModel对应的实体属性就会为空. 如果数据库中对应的字段不允 ...
- 实现步骤: 推送&传感器&UIDynamic
一.本地通知基本使用: #01.请求授权(8.0以前默人授权) #02.创建本地通知 #03.设置通知内容 #04.设置通知时间(多久后发通知) #05.发送通知 二.本地通知而外设置: #01.设置 ...
- Index Seek和Index Scan的区别
Index Seek是Sql Server执行查询语句时利用建立的索引进行查找,索引是B树结构,Sql Server先查找索引树的根节点,一级一级向下查找,在查找到相应叶子节点后,取出叶子节点的数据. ...
- html5遵循的5个设计原则
前面的话 实际上,html5并不是由w3c直接制定的,w3c的方向是xhtml2,而不是html5.当xhtml2脱离现实,无法付诸实践时,w3c工作组才将研究方向转向html5.为什么xhtml2从 ...
- 用命令行编译java并生成可执行的jar包
用命令行编译java并生成可执行的jar包 1.编写源代码. 编写源文件:CardLayoutDemo.java并保存,例如:I:\myApp\CardLayoutDemo.java.程序结构如下: ...
- hdu 1811Rank of Tetris (并查集 + 拓扑排序)
/* 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B ...
- 让Team Foundation Server/TFS自动记住用户名密码解决方案
在使用Team Foundation Server(以下简称TFS) 的时候,在每次打开Visual Studio TFS时候,需要输入用户名和秘密,比较麻烦.现提供一种方法可以解决这个问题: 依次执 ...
- Yii2的深入学习--自动加载机制
Yii2 的自动加载分两部分,一部分是 Composer 的自动加载机制,另一部分是 Yii2 框架自身的自动加载机制. Composer自动加载 对于库的自动加载信息,Composer 生成了一个 ...