使用command对象操作数据库
1.Command对象查询数据库
protected void Button1_Click(object sender, EventArgs e)
{
//读取web.config节点配置
string strcon = ConfigurationManager.ConnectionStrings["testjm"].ConnectionString;
//实例化sqlConnection对象
SqlConnection con = new SqlConnection(strcon);
//数据库建立连接打开
con.Open();
string strsql = "select * from userinfo where name=@name";//查询语句
SqlCommand mycmd = new SqlCommand(strsql, con);
mycmd.Parameters.Add("@name", SqlDbType.VarChar,).Value = TextBox1.Text.Trim();
SqlDataAdapter myda = new SqlDataAdapter(mycmd);//实例化SqlDataAdapter,把strsql查询语句通过con传递给数据库
DataSet myds = new DataSet();//实例化DataSet为myds
myda.Fill(myds, "userinfo");//填充数据集
GridView1.DataSource = myds;//界面上显示返回的数据集,指定数据源
GridView1.DataBind();//绑定数据库 myda.Dispose();
myds.Dispose(); con.Close();//关闭连接
}
2.Command对象添加数据
/// <summary>
/// 封装查询userinfo表信息
/// </summary>
protected void bind()
{ SqlConnection con = getcon();
//数据库建立连接打开
con.Open();
string strsql = "select * from userinfo";//查询语句
SqlDataAdapter myda = new SqlDataAdapter(strsql,con);
DataSet myds = new DataSet();
myda.Fill(myds);
GridView1.DataSource = myds;//界面上显示返回的数据集,指定数据源
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();//绑定数据库
myda.Dispose();
myds.Dispose();
con.Close(); } /// <summary>
/// 封装数据库连接
/// </summary>
/// <returns></returns>
protected SqlConnection getcon()
{
//读取web.config节点配置
string strcon = ConfigurationManager.ConnectionStrings["testjm"].ConnectionString;
//实例化sqlConnection对象
SqlConnection con1 = new SqlConnection(strcon);
return con1;
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btSumbit_Click(object sender, EventArgs e)
{
SqlConnection con = getcon();
//数据库建立连接打开
con.Open();
string strinsert = "insert into userinfo(id,name,password,age) values(" + this.tbid.Text.Trim() + ",'" + this.tbname.Text.Trim() + "','" + this.tbpwd.Text.Trim() + "'," + this.tbage.Text.Trim() + ")";
SqlCommand mycmd = new SqlCommand(strinsert, con);
mycmd.ExecuteNonQuery();
mycmd.Dispose();
con.Close();//关闭连接
this.bind();
}
/// <summary>
/// 添加数据中的重置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btReset_Click(object sender, EventArgs e)
{
tbid.Text = "";
tbname.Text = "";
tbpwd.Text = "";
tbage.Text = "";
}
3.Command对象修改数据
/// <summary>
/// 单击编辑按钮,会触发RowEditing事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.bind();
}
/// <summary>
/// 更新数据,RowUpdating更新前的事件,RowUpdated更新后的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int cid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string cName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[])).Text.ToString();
string cPwd = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[])).Text.ToString();
string strupdate = "update userinfo set name='" + cName + "',password='"+ cPwd +"'where id=" + cid;
SqlConnection con = getcon();
con.Open();
SqlCommand mycmd = new SqlCommand(strupdate, con);
mycmd.ExecuteNonQuery();
mycmd.Dispose();
con.Close();//关闭连接
this.bind();
}
/// <summary>
/// 单击更新中的取消按钮,触发RowCancelingEdit事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -;
this.bind();
}
4.Command对象删除数据
/// <summary>
/// 删除数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int cid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string strdelete = "delete from userinfo where id=" + cid;
SqlConnection con = getcon();
con.Open();
SqlCommand mycmd = new SqlCommand(strdelete,con);
mycmd.ExecuteNonQuery();
mycmd.Dispose();
con.Close();
this.bind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.Cells[].Controls[]).Attributes.Add("onclick", "return confirm('确定要删除这天数据吗 ?')");
}
}
使用command对象操作数据库的更多相关文章
- c# 数据库编程(利用DataSet 和 DataAdaper对象操作数据库--单表操作)
一.概述 前面2篇文章,介绍了使用SqlCommand对象利用sql命令来操作数据库. 这篇文章我们来介绍使用c#的DataSet 和 DataAdaper对象操作操作数据库. 先来介绍下这两个对象是 ...
- c# 数据库编程(利用DataSet 和 DataAdaper对象操作数据库--跨表操作)
上篇文章我们介绍了如何利用DataSet 和 DataAdaper对象来对单张表进行操作. 本文我们将介绍如何进行跨表操作. 我们通过具体例子方式进行演示,例子涉及到三张表. 1)student表(学 ...
- php 用封装类的方法操作数据库和批量删除
封装类 <?php class DBDA { public $host="localhost"; //服务器地址 public $uid="root"; ...
- C#与数据库访问技术总结(六)之Command对象创建SQl语句代码示例
Command对象创建SQl语句代码示例 说明:前面介绍了 Command 对象的方法和一些属性,回顾一下 Command对象主要用来执行SQL语句.利用Command对象,可以查询数据和修改数据. ...
- C#与数据库访问技术总结(五)之Command对象的常用方法
Command对象的常用方法 说明:上篇总结了Command对象的几个数据成员,这节总结Command对象的常用方法. 同样,在不同的数据提供者的内部,Command对象的名称是不同的,在SQL Se ...
- 多线程下,多次操作数据库报错,There is already an open DataReader associated with this Command which must be closed first.
原文:https://www.cnblogs.com/sdusrz/p/4433108.html 执行SqlDataReader.Read之后,如果还想用另一个SqlCommand执行Insert或者 ...
- Android学习笔记_9_SQLiteOpenHelper对象之数据库增删改查以及事务回滚操作
一.SQLite数据库: 在Android平台上,集成了一个嵌入式关系型数据库—SQLite,SQLite3支持 NULL.INTEGER.REAL(浮点数字).TEXT(字符串文本)和BLOB(二进 ...
- django之三剑客、静态文件配置、请求响应对象、数据库操作
三剑客 from django.shortcuts import render,HttpResponse,redirect HttpResponse # 返回字符串 render(response, ...
- ADO.NET操作数据库(一)
---恢复内容开始--- [1]ADO.Net简介2015-12-07-20:16:05 ADO.Net提供对Microsoft SQL Server数据源以及通过OLE DB和XML公开的数据源的一 ...
随机推荐
- PHP二维数组的分页
方法一: <?php $arr_click = array( array( 'clicks' => 3, 'clickDate' =>'2015-10-11' ), array( ' ...
- 利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出
我们知道,一般都导出的Word文档或者Excel文档,基本上分为两类,一类是动态生成全部文档的内容方式,一种是基于固定模板化的内容输出,后者在很多场合用的比较多,这也是企业报表规范化的一个体现. 我的 ...
- Linux学习笔记(20) Linux系统管理
1.进程管理 进程是正在执行的一个程序或命令,每一个进程都是一个运行的实体,都有自己的地址空间,并占用一定的系统资源. 进程管理的作用有判断服务器健康状态.查看系统中所有进程及杀死进程.一般都可以采用 ...
- 【T_SQL】基础 续+
十.模糊查询 1.LIKE --查询时,字段中的内容并不一定与查询内容完全匹配,只要字段中含有这些内容. SELECT StuName AS 姓名 FROM Stuinfo WHERE stuname ...
- flex模拟微信布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 在MySQL中存储大文件
我们的目标:把一首mp3保存到MySQL数据库中! 由于MySQL默认当存入的数据太大时会抛异常,所以应在my.ini中添加如下配置!max_allowed_packet=10485760,这样,可以 ...
- Asp.Net Mvc 控制器与视图的数据传递
数据传递也就是控制器和视图之间的交互,比如在视图中提交的数据,在控制器怎么获取,或者控制器从业务层获得一些数据,怎么传递到视图中,让视图显示在客户端呢?带着这些疑问,我们接着看.. 下面 ...
- 《DSP using MATLAB》示例Example4.8
代码: b = [0,1]; a = [3, -4, 1]; % polynomials coefficients [R,p,c] = residuez(b,a) [b,a] = residuez(R ...
- DSP using MATLAB 示例 Example3.10
用到的性质 上代码: n = -5:10; x = rand(1,length(n)) + j * rand(1,length(n)); k = -100:100; w = (pi/100)*k; % ...
- PHP 传值和传引用、传地址的区别
传值, 是把实参的值赋值给行参 那么对行参的修改,不会影响实参的值 传地址 是传值的一种特殊方式,只是他传递的是地址,不是普通的如int 那么传地址以后,实参和行参都指向同一个对象 ...