首页 技术 正文
技术 2022年11月14日
0 收藏 300 点赞 5,033 浏览 3884 个字

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('确定要删除这天数据吗 ?')");
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,299