C# 连接数据库(添加,修改,删除)

一、添加年级信息到表中

 /// <summary>
        /// 添加年级表
        /// INSERT INTO grade VALUES('IT11')
        /// </summary>
        private static void addGrade(string gname)
        {
            SqlConnection con = null;
            try
            {
                //1.连接字符串
                string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";

                //2.连接对象
                con = new SqlConnection(str);

                //3.打开连接
                con.Open();

                //4.sql语句
                //INSERT INTO grade VALUES('IT11')
                StringBuilder bulider = new StringBuilder();
                bulider.AppendLine("INSERT");
                bulider.AppendLine(" INTO ");
                bulider.AppendLine(" grade ");
                bulider.AppendLine(" VALUES ");
                bulider.AppendFormat("('{0}') ", gname);
                // Console.WriteLine(bulider.ToString());

                //5.命名对象
                SqlCommand cmd = new SqlCommand(bulider.ToString(), con);

                //6.执行sql语句 返回Int
                int count = cmd.ExecuteNonQuery();

              // Console.WriteLine(count);

                if (count > 0)
                {
                    Console.WriteLine("OK!");
                }
                else
                {
                    Console.WriteLine("no!");
                }
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                con.Close();
            }

        }

 二、修改学生的信息

    

/// <summary>
        /// 修改学生表
        /// UPDATE stu SET BornDate='1992-1-1',Phone='80011011' WHERE StudentName='白燕'
        /// </summary>
        private static void updateStudent(string date,string tel,string name)
        {
            SqlConnection con = null;
            try
            {
                //1.连接字符串
                string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";

                //2.连接对象
                con = new SqlConnection(str);

                //3.打开连接
                con.Open();

                //4.sql语句
                //UPDATE stu SET BornDate='1992-1-1',Phone='80011011' WHERE StudentName='白燕'
                StringBuilder bulider = new StringBuilder();
                bulider.AppendLine("UPDATE");
                bulider.AppendLine(" stu ");
                bulider.AppendLine(" SET ");
                bulider.AppendFormat("  BornDate='{0}',Phone='{1}' ",date,tel);
                bulider.AppendLine(" WHERE ");
                bulider.AppendFormat("StudentName='{0}' ", name);
                // Console.WriteLine(bulider.ToString());

                //5.命名对象
                SqlCommand cmd = new SqlCommand(bulider.ToString(), con);

                //6.执行sql语句 返回Int
                int count = cmd.ExecuteNonQuery();

                // Console.WriteLine(count);

                if (count > 0)
                {
                    Console.WriteLine("OK!");
                }
                else
                {
                    Console.WriteLine("no!");
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                con.Close();
            }

        }

 三、删除学生信息

/// <summary>
        /// 删除学生表
        /// delete stu WHERE StudentName='白燕'
        /// </summary>
        private static void delStudent(string name)
        {
            SqlConnection con = null;
            try
            {
                //1.连接字符串
                string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";

                //2.连接对象
                con = new SqlConnection(str);

                //3.打开连接
                con.Open();

                //4.sql语句
                // delete stu WHERE StudentName='白燕'
                StringBuilder bulider = new StringBuilder();
                bulider.AppendLine("delete");
                bulider.AppendLine(" stu ");
                bulider.AppendLine(" WHERE ");
                bulider.AppendFormat("StudentName='{0}' ", name);
                // Console.WriteLine(bulider.ToString());

                //5.命名对象
                SqlCommand cmd = new SqlCommand(bulider.ToString(), con);

                //6.执行sql语句 返回Int
                int count = cmd.ExecuteNonQuery();

                // Console.WriteLine(count);

                if (count > 0)
                {
                    Console.WriteLine("OK!");
                }
                else
                {
                    Console.WriteLine("no!");
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                con.Close();
            }

        }

 四、总结

      操作添加,删除,修改数据,其实就是用SQLCommand命令对象中的ExecuteNonQuery()来执行,返回是一个整数型的数据。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值