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

    string strcon = @"Data Source=.;Initial Catalog=XSXX;Integrated Security=True";
    //Data Source=数据库 Initial Catalog=目录

    添加操作:

    using (SqlConnection con=new SqlConnection(strcon))   //连接数据库
    {
    
            string sql = "INSERT INTO XS VALUES('" + user + "','" + pwd + "')"; //数据库指令  XS:表名称  
          //INSERT INTO XS VALUES('张山','123456');
    using (SqlCommand cmd=new SqlCommand(sql,con)) { con.Open(); //打开数据库 cmd.ExecuteNonQuery();//执行命令 } }

    删除操作:

    int r = dataGridView1.SelectedRows.Count;  //要删除的行数
    if (r != 0) { int index = dataGridView1.CurrentRow.Index; //获取该行的位置 string s = dataGridView1.Rows[index].Cells[0].Value.ToString(); //第一个字段的内容 using (SqlConnection con = new SqlConnection(strcon)) { string sql = "delete from " + Table + " where 账号='" + s + "'";
    //delete from XS where name='张三' 删除指令
    using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); cmd.ExecuteNonQuery(); } } }

    修改:

     using (SqlConnection con = new SqlConnection(strcon))
     {
           string sql = "update " + Taable + " set 密码='" + s + "' where 账号='" + user + "'";
           // update XS set 密码='654321' where 账号='张三'
           using (SqlCommand cmd = new SqlCommand(sql, con))
           {
                con.Open();
                cmd.ExecuteNonQuery();
                          
           }
                      
      }

    查询:

    using (SqlConnection sqlcon = new SqlConnection(strcon))
    {
       sqlcon.Open();
       string sql = "SELECT * FROM " + Taable + " where 账号='" + content + "'";
      //SELECT * FROM XS where 账号='张三'; SqlCommand cmd
    = new SqlCommand(sql, sqlcon); SqlDataAdapter myda = new SqlDataAdapter(cmd); myst.Tables.Clear(); //Dataset myst=new Dataset(); myda.Fill(myst, "" + Taable + ""); dataGridView1.DataSource = myst.Tables["" + Taable + ""]; }
  • 相关阅读:
    用js实现 全选功能(Gridview)
    图片 加文字水印或者图片水印
    Gridview中RowCommand事件的应用
    图片噪点的产生
    DataList实现折叠效果
    利用Request对象的CurrentExecutionFilePath做分页
    利用分页控件AspNetPager来做数据的分页
    log4Net一些记忆性总结
    彩色验证码
    组织机构设计器窗体基类 代码分析
  • 原文地址:https://www.cnblogs.com/ww123/p/10211720.html
Copyright © 2011-2022 走看看