zoukankan      html  css  js  c++  java
  • sql连接数据

    这个难道没办法杀出了么?

    以users表为例,有三个字段,自增长的编号id,int类型;名称name,nvarchar类型,密码pwd,nvarchar类型 
    首先在vs2005中引入using System.Data.SqlClient;命名空间

            /// <summary>
            /// 增加 
            /// </summary> 
            /// <param name="name">姓名</param>         
            /// <param name="pwd">密码</param>         
            /// <returns></returns> 
            public int Insert(string name, string pwd)
            {
                SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
                conn.Open();
                string sql = "insert into users(name,pwd) values(@name,@pwd)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter parn = new SqlParameter("@name", name);
                cmd.Parameters.Add(parn);
                SqlParameter parp = new SqlParameter("@pwd", pwd);
                cmd.Parameters.Add(parn);
                int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功
                conn.Close();
                cmd.Dispose();
                return result;
            }
            /// <summary>
            /// 删除 
            /// </summary> 
            /// <param name="name">姓名</param>
            /// <param name="pwd">密码</param>
            /// <returns></returns>
            public int Update(int id)
            {
                SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
                conn.Open();
                string sql = "delete from users where id=@id";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter parn = new SqlParameter("@id", id);
                cmd.Parameters.Add(parn);
                int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示删除成功
                conn.Close();
                cmd.Dispose();
                return result;
            }
            /// <summary>
            /// /// 修改 
            /// </summary> 
            /// <param name="name">姓名</param>
            /// <param name="pwd">密码</param>
            /// <returns></returns> 
            public int Insert(string name, string pwd, int id)
            {
                SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
                conn.Open();
                string sql = "update users set name=@name,pwd=@pwd where id=@id";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter parn = new SqlParameter("@name", name);
                cmd.Parameters.Add(parn);
                SqlParameter parp = new SqlParameter("@pwd", pwd);
                cmd.Parameters.Add(parn);
                SqlParameter pari = new SqlParameter("@id", id);
                cmd.Parameters.Add(pari);
                int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示修改成功 
                conn.Close();
                cmd.Dispose();
                return result;
            }
            /// <summary>
            /// /// 查询 
            /// </summary> 
            /// <returns></returns>
            public DataTable Select()
            {
                SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
                conn.Open();
                string sql = "select * from users";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                conn.Close();
                cmd.Dispose();
                return dt;
            }        
    

      

    方法写好后,下面举一个查询的例子,在form窗体中拖一个DataGridView,然后在Load方法中 
        private void Form1_Load(object sender, EventArgs e)         { 
                  dataGridView1.DataSource = Select();         } 
    这样一运行,DataGridView中就会显示数据了 

  • 相关阅读:
    Cocos2d-x 3.0 屏幕触摸及消息分发机制
    stretchableImageWithLeftCapWidth气泡拉伸
    海量数据插入数据库效率对照測试 ---ADO.NET下SqlBulkCopy()对照LINQ 下InsertAllOnSubmit()
    银联+移动+三星PK微信、余额宝
    热力学三大定律与熵
    热力学三大定律与熵
    OpenGL(六) gluLookAt和gluPerspective函数解析
    特殊字符
    特殊字符
    函数的功能
  • 原文地址:https://www.cnblogs.com/jianghaidong/p/3711860.html
Copyright © 2011-2022 走看看