今天学习了ADO.NET对数据库的基本操作,让我一直纠结的难题,在老师的讲解下都一一明白了,感觉老师讲的很好,通过今天我做作业,让我的收获很大,下面我来分享一下我的练习题吧
<connectionStrings>
<add name="sqlcnn" connectionString="data source=.;initial catalog=ShuJuKuCaoZuo;integrated security=true;"/>
</connectionStrings>
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)//注册
{
SqlCommand com=GetCommand();
com.CommandText = "insert into TLbook(name,password,sex,telephone) values('"+this.TextBox1.Text+"','"+this.TextBox2.Text+"','"+this.TextBox4.Text+"','"+this.TextBox5.Text+"')";
com.Connection.Open();
int a = com.ExecuteNonQuery();
if (a!=0)
{
Response.Write("注册成功");
this.TextBox1.Text = null;
this.TextBox4.Text = null;
this.TextBox5.Text = null;
}
else
{
Response.Write("注册失败");
}
com.Connection.Close();
}
private static SqlCommand GetCommand( )//封装方法
{
string sqlCnn = ConfigurationManager.ConnectionStrings["sqlcnn"].ConnectionString;
SqlCommand com;
SqlConnection con = new SqlConnection(sqlCnn);
com = con.CreateCommand();
return con.CreateCommand();
}
protected void Button4_Click(object sender, EventArgs e)//查询
{
SqlCommand com = GetCommand();
com.CommandText = "select sex,telephone from TLbook where name='"+this.TextBox1.Text+"'";
com.Connection.Open();
SqlDataReader reader = com.ExecuteReader();
if (reader.Read())
{
//this.TextBox1.Text = reader.GetString(reader.GetOrdinal("name"));
this.TextBox4.Text = reader.GetString(reader.GetOrdinal("sex"));
this.TextBox5.Text = reader["telephone"].ToString();
}
else
{
this.Response.Write("用户不存在!");
}
reader.Close();
com.Connection.Close();
}
protected void Button2_Click(object sender, EventArgs e)//删除
{
SqlCommand com = GetCommand();
com.CommandText = "delete from TLbook where name='"+this.TextBox1.Text+"'";
com.Connection.Open();
int a = com.ExecuteNonQuery();
if (a!=0)
{
Response.Write("删除失败");
}
else
{
Response.Write("删除成功");
}
com.Connection.Close();
}
protected void Button3_Click(object sender, EventArgs e)//修改
{
SqlCommand com = GetCommand();
com.CommandText = "update TLbook set
password=@password,sex=@sex,telephone=@telephone where
name=@name";
SqlParameter param = com.CreateParameter();
param.ParameterName = "@name";
param.SqlDbType = System.Data.SqlDbType.NChar;
param.Size = 10;
param.Value = this.TextBox1.Text;
com.Parameters.Add(param);
param = new SqlParameter("@password",System.Data.SqlDbType.NChar,10);
param.Value = this.TextBox2.Text;
com.Parameters.Add(param);
param = new SqlParameter("@sex",System.Data.SqlDbType.Char,2);
param.Value = this.TextBox4.Text;
com.Parameters.Add(param);
param = new SqlParameter("@telephone",System.Data.SqlDbType.Int);
param.Value = this.TextBox5.Text;
com.Parameters.Add(param);
com.Connection.Open();
int a=com.ExecuteNonQuery();
this.Response.Write("成功");
if (a != 0)
{
this.Response.Write("修改成功");
}
else
{
this.Response.Write("修改失败");
}
com.Connection.Close();
}
}
分享到:
相关推荐
ext.net.community.1.0rc1组件与.net的数据库增删改查操作。通用合适。
Ado.Net技术,NET语言的BS和CS通用的底层代码,net语言链接数据库的底层代码,封装成方法+详细的说明和步骤,傻瓜式操作,必然能看懂的代码说明+示例。
采用MVC+ADO.NET进行增删改查案例,采用VS2017跟sqlserver2008 可直接解压运行,一套齐全
特别适合新手学EF4.0
为初学者更好的了解ADO.NET简单的增删改查
asp.net mvc 增删改查实例,不是使用entity framework来实现的,使用传统的ADO.NET来实现的,很好的一个实例。
C#-ADO.net连接数据库-进行增删改查操作
采用了简单的三层:Entity(表字段的get/set方法)、BLL(表增、删、改、查方法封装)、Pages(页面展示)。 自己封装了SQLClass类,采用ADO.NET操作SQLServer数据库。 使用异步AJAX实现增、删、改、查操作。展示...
ADO.NET实现对数据库的增删改查
ADO.NET Entity Framework 增删改查语句句法
ADO.NET数据增删改查,datagridview 单选,SQL语句参数操作,文本框只能输入正整数 完整源码
使ADO.NET的使用变的方便的类库,轻松实现增删改查,具体使用方法亦可参考本人博客http://blog.csdn.net/cndotaci/archive/2008/09/03/2870007.aspx
个人做的某鸟一期项目,ADO.NET增删改查,界面凑合。 功能完善,且有拓展。 数据库是SQL2008R2
学生管理系统源码,ADO.NET进行增删改查学生基本和系别信息,适合毕业设计。
DbHelper C#实现的dbhelper类,支持mysql、sqlserver、oracle等多种数据库、支持事务
使用sqlite3 数据库 SQLite ADO .NET实现 winfrom开发 例子 实现增删改查
ASP.NET MVC5增删改查,包含LINQ和ADO两种(ADO只实现了List Detail和Delete,Edit留给你自己思考了),LINQ版本实现了完整的CRUD,并且演示了MVC5 的新特性Attribute Route以及异步的修改、删除控制器。 相關討論參...
C# vs2017与SQL Server 2012的增删改查的简单应用实例,在控制下运行,基于System.Data.SqlClient的Sqlconnection和SqlCamand的实例应用
ajax. ado.net增删改查 .net ajax. ado.net jquery .net
很具代表性的代码,C#连接数据库并实现增删改查功能。