zoukankan      html  css  js  c++  java
  • 数据库 增 ,删,改, 查询

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    namespace 登陆链接数据库
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e) //链接数据库查询
            {
                string uname = textBox1.Text;
                string upass = textBox2.Text;
                //第一步  链接数据库
                SqlConnection conn = new SqlConnection("server=.;database=yonghu;user=sa;pwd=123");
                conn.Open();
                //第二步  写执行语句
                SqlCommand cmd = conn.CreateCommand();//sqlcommand  帮助写执行语句
                cmd.CommandText = "select *from users where uname='"+uname+"'and upass='"+upass+"'";
                SqlDataReader dr = cmd.ExecuteReader();//读取执行语句  返回一个结果集
                if(dr.Read())
                {
                    MessageBox.Show("登陆成功");
                }
                else
                {
                    MessageBox.Show("登陆失败");
                }
                conn.Close();
    
            }
    
            private void button2_Click(object sender, EventArgs e)//链接数据库添加
            {
                string uname = textBox1.Text;
                string upass = textBox2.Text;
                //链接数据库
                SqlConnection conn = new SqlConnection("server=.;database=yonghu;user=sa;pwd=123");
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();//通过conn创建sqlcommand对象
                cmd.CommandText = "insert into users values('"+uname+"','"+upass+"')";
               int count= cmd.ExecuteNonQuery();//增删改  用executenonquery  执行语句返回影响的行数
               if (count > 0)
               {
                   MessageBox.Show("添加成功");
               }
               else
               {
                   MessageBox.Show("添加失败");
               }
               conn.Close();
            }
    
            
    
           
    
            private void button3_Click(object sender, EventArgs e)//链接数据库修改
            {
                string code = textBox3.Text;
                string uname = textBox1.Text;
                string upass = textBox2.Text;
                //链接数据库
                SqlConnection conn = new SqlConnection("server=.;database=yonghu;user=sa;pwd=123");
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "update users set uname='" + uname + "',upass='" + upass + "'where code=" + code;
                int count = cmd.ExecuteNonQuery();
                if (count > 0)
                {
                    MessageBox.Show("修改成功");
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
                conn.Close();
            }
    
            private void button4_Click(object sender, EventArgs e)//链接数据库删除
            {
                string code = textBox3.Text;
                SqlConnection conn = new SqlConnection("server=.;database=yonghu;user=sa;pwd=123");
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "delete from users where code="+code;
                int count=  cmd.ExecuteNonQuery();
                if (count > 0)
                {
                    MessageBox.Show("删除成功");
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
                conn.Close();
            }
        }
    }
    ------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace 增删改
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                SqlConnection conn = new SqlConnection("server=.;database=yonghu;user=sa;pwd=123");
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "select *from users";
                SqlDataReader dr = cmd.ExecuteReader();
                //开始构建listview表头
                listView1.Columns.Clear();
                listView1.Columns.Add("编号");
                listView1.Columns.Add("姓名");
                listView1.Columns.Add("密码");
                //读取数据库 根据数据库内容往listview里添加行
                listView1.Items.Clear();
                while (dr.Read())
                {
                    ListViewItem vi = new ListViewItem();
                    vi.Text = dr["code"].ToString();
                    vi.SubItems.Add(dr["uname"].ToString());
                    vi.SubItems.Add(dr["upass"].ToString());
                    listView1.Items.Add(vi);
    
    
                }
                conn.Close();
    
            }
        }
    }
  • 相关阅读:
    Linux 下编译Android-VLC开源播放器详解(附源码下载)
    VC/Wince 实现仿Win8 Metro风格界面2——页面滑动切换(附效果图)
    Android Launcher分析和修改3——Launcher启动和初始化
    VC/Wince 实现仿Win8 Metro风格界面1——设计概述和自绘Button(附效果图)
    Android Launcher分析和修改2——Icon修改、界面布局调整、壁纸设置
    Android Launcher分析和修改1——Launcher默认界面配置(default_workspace)
    WARNING: APP_PLATFORM android-14 is larger than android:minSdkVersion 8
    编程工具系列之二------使用GDB的源代码查看功能
    编程工具系列之一------使用GDB的堆栈跟踪功能
    unable to open sync connection
  • 原文地址:https://www.cnblogs.com/wei270647220/p/4332092.html
Copyright © 2011-2022 走看看