zoukankan      html  css  js  c++  java
  • ado.net操作数据库

     

    //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcon = new SqlConnection(strCon);
            sqlcom = new SqlCommand(sqlstr,sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            bind();
        }

    //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            sqlcon = new SqlConnection(strCon);
            string sqlstr = "update 表 set 字段1='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
                + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcom=new SqlCommand(sqlstr,sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            GridView1.EditIndex = -1;
            bind();
        }

    //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }

    //绑定
        public void bind()
        {
            string sqlstr = "select * from 表";
            sqlcon = new SqlConnection(strCon);
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlcon.Open();
            myda.Fill(myds, "表");
            GridView1.DataSource = myds;
            GridView1.DataKeyNames = new string[] { "id" };//主键
            GridView1.DataBind();
            sqlcon.Close();
        }

     

  • 相关阅读:
    数组常用遍历方法总结
    文本控制行数,超出省略号显示
    数据结构入门
    数论函数补充 公式推导
    几何入门合集 gym101968 problem F. Mirror + gym102082 Problem F Fair Chocolate-Cutting + gym101915 problem B. Ali and Wi-Fi
    COCI 2018/2019 CONTEST #2 T4 Maja T5Sunčanje Solution
    数论函数
    数论入门
    USACO1.4 1.5 搜索剪枝与数字 洛谷OJ P1214 P1215 P1217 P1218
    USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)
  • 原文地址:https://www.cnblogs.com/shiningrise/p/1436810.html
Copyright © 2011-2022 走看看