码迷,mamicode.com
首页 > 数据库 > 详细

C#链接数据库增删改查的例子

时间:2016-12-07 13:47:12      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:for   大于   vs2005   cli   eve   添加   turn   服务器   datagrid   

以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中就会显示数据了

C#链接数据库增删改查的例子

标签:for   大于   vs2005   cli   eve   添加   turn   服务器   datagrid   

原文地址:http://www.cnblogs.com/qixu/p/6140677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!