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对象操作数据库的更多相关文章

  1. c# 数据库编程(利用DataSet 和 DataAdaper对象操作数据库--单表操作)

    一.概述 前面2篇文章,介绍了使用SqlCommand对象利用sql命令来操作数据库. 这篇文章我们来介绍使用c#的DataSet 和 DataAdaper对象操作操作数据库. 先来介绍下这两个对象是 ...

  2. c# 数据库编程(利用DataSet 和 DataAdaper对象操作数据库--跨表操作)

    上篇文章我们介绍了如何利用DataSet 和 DataAdaper对象来对单张表进行操作. 本文我们将介绍如何进行跨表操作. 我们通过具体例子方式进行演示,例子涉及到三张表. 1)student表(学 ...

  3. php 用封装类的方法操作数据库和批量删除

    封装类 <?php class DBDA { public $host="localhost"; //服务器地址 public $uid="root"; ...

  4. C#与数据库访问技术总结(六)之Command对象创建SQl语句代码示例

    Command对象创建SQl语句代码示例 说明:前面介绍了 Command 对象的方法和一些属性,回顾一下 Command对象主要用来执行SQL语句.利用Command对象,可以查询数据和修改数据. ...

  5. C#与数据库访问技术总结(五)之Command对象的常用方法

    Command对象的常用方法 说明:上篇总结了Command对象的几个数据成员,这节总结Command对象的常用方法. 同样,在不同的数据提供者的内部,Command对象的名称是不同的,在SQL Se ...

  6. 多线程下,多次操作数据库报错,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或者 ...

  7. Android学习笔记_9_SQLiteOpenHelper对象之数据库增删改查以及事务回滚操作

    一.SQLite数据库: 在Android平台上,集成了一个嵌入式关系型数据库—SQLite,SQLite3支持 NULL.INTEGER.REAL(浮点数字).TEXT(字符串文本)和BLOB(二进 ...

  8. django之三剑客、静态文件配置、请求响应对象、数据库操作

    三剑客 from django.shortcuts import render,HttpResponse,redirect HttpResponse # 返回字符串 render(response, ...

  9. ADO.NET操作数据库(一)

    ---恢复内容开始--- [1]ADO.Net简介2015-12-07-20:16:05 ADO.Net提供对Microsoft SQL Server数据源以及通过OLE DB和XML公开的数据源的一 ...

随机推荐

  1. struct和typedef struct

    转自:http://www.cnblogs.com/qyaizs/articles/2039101.html struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++ ...

  2. orientation和gravity的区别

    orientation:决定UI组件是按行还是列显示 gravity:指定文字对齐方式.

  3. JS 正则表达式详解

    在此提供相关的链接,请访问: http://www.cnblogs.com/dolphinX/p/3486214.html http://www.cnblogs.com/dolphinX/p/3486 ...

  4. mysql中sql语句

    <数据定义语言DDL> 一. create TABLE tableName 创建表 二. alter TABLE tableName 修改表 三. drop TBALE tableName ...

  5. 三层+MVC导出Excel(2)

    背景: 出门在外,一切以健康为主,学习为辅,健康搞好了,学习也不能拉下,在外工作期间,我们在做数据导出的时候,自己封了一个类,利用NPOI进行数据导出Excel,自我感觉良好,特给大家分享一下,希望对 ...

  6. memcached for windows 修改端口和最大内存,以及常用命令

    在windows中使用memcached,必须先下载memcached for win32安装. PHP模块MemCache下载地址:http://downloads.php.net/pierre 服 ...

  7. Codeforces Round #355 (Div. 2)-B

    B. Vanya and Food Processor 题目链接:http://codeforces.com/contest/677/problem/B Vanya smashes potato in ...

  8. 《DSP using MATLAB》示例Example4.2

  9. blade用法

    一.blade条件判断,foreach循环写法 @if(isset($fileInfo) && !empty($fileInfo)) @foreach($fileInfo as $k) ...

  10. 使用Maven构建Android项目

    http://www.ikoding.com/build-android-project-with-maven/ 之前一直在做WEB前端项目,前段时间接手第一个Android项目,拿到代码之后,先试着 ...