使用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公开的数据源的一 ...
随机推荐
- js 上传文件后缀名的判断 var flag=false;应用
js 上传文件后缀名的判断 var flag=false;应用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...
- Entity Framework Code First for SQL Compact
这篇博客将介绍EF Code First中如何使用SQL Compact.SQL Compact是微软推出的免费的关系型数据库,目前最新版本是SQL Compact 4.0.微软的开发工具VS 201 ...
- java调用shell获取返回值
转自:http://blog.csdn.net/tengdazhang770960436/article/details/12014839 1.shell文件return.sh echo 1 echo ...
- 求数组的长度 C
对于数组array,计算其占用内存大小和元素个数的方法如下: C/C++ code ? 1 2 3 4 5 //计算占用内存大小 sizeof(array) //计算数组元素个数 sizeof(a ...
- 用C#基于WCF创建TCP的Service供Client端调用
本文将详细讲解用C#基于WCF创建TCP的Service供Client端调用的详细过程 1):首先创建一个Windows Service的工程 2):生成的代码工程结构如下所示 3):我们将Servi ...
- Java学习笔记(二)——变量与常量
一.java中的关键字 Java 语言中有一些具有特殊用途的词被称为关键字.关键字对 Java 的编译器有着特殊的意义,在程序中应用时一定要慎重哦!! 二.认识Java标识符 1.定义 标识符就是用于 ...
- hadoop中map和reduce的数量设置问题
转载http://my.oschina.net/Chanthon/blog/150500 map和reduce是hadoop的核心功能,hadoop正是通过多个map和reduce的并行运行来实现任务 ...
- 【spring bean】 spring中bean之间的引用以及内部bean
在spring中会有如下的几种情况: 1.在当前容器中,(即在spring.xml这一个配置文件中),一个bean引用了另一个bean. 使用 1> <ref bean="另 ...
- caffe中添加local层
下载caffe-local,解压缩; 修改makefile.config:我是将cuudn注释掉,去掉cpu_only的注释; make all make test(其中local_test出错,将文 ...
- X-UA-Compatible属性的解释
问题描述: 代码如下: <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE& ...