V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
shuaigeawei123
V2EX  ›  C

用 winform 连接数据库,修改键怎么写啊

  •  
  •   shuaigeawei123 · 2017-10-13 16:05:02 +08:00 · 1406 次点击
    这是一个创建于 2589 天前的主题,其中的信息可能已经有所发展或是发生改变。

    写差不多了,就剩个修改,不会把数据库的值带入 texbox 中,修改和新增都是同一个界面

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient;

    namespace Student { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

        private void label1_Click(object sender, EventArgs e)
        {
    
        }
        //删除按钮
        private void button4_Click(object sender, EventArgs e)
        {
            string sql = "delete  from Student1 where id=" + textBox1.Text;
            string st =
            "Server=LIYAOYAO-PC\\PARTSMANSERVER;" +
            "Database=Student1;" +
            "Uid=c4aba27c-80c8-4e83-88e1-441145dba3a3;" +
            "Pwd=1748c8f8-067a-4991-a021-b22a5467ac0a;";
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(st);
                conn.Open();
                SqlCommand com = new SqlCommand(sql, conn);
    
                com.ExecuteNonQuery();
                conn.Close();
                textBox1.Text = "";
    
                
    
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除请输入 id 号");
            }
        }
        //新增按钮
    
        private void button2_Click(object sender, EventArgs e)
        {
            new Form2().Show();
        }
        //查询按钮
        private void button1_Click(object sender, EventArgs e)
        {
            string sql;
            string connectionString =
            "Server=LIYAOYAO-PC\\PARTSMANSERVER;" +
            "Database=Student1;" +
            "Uid=c4aba27c-80c8-4e83-88e1-441145dba3a3;" +
            "Pwd=1748c8f8-067a-4991-a021-b22a5467ac0a;";
            SqlConnection sc = null;
            try
            {
                sc = new SqlConnection(connectionString);
                sc.Open();//打开数据库连接
                if (textBox1.Text == "")
                {
                    sql = "select * from Student1";
                }
                else
                {
                    sql = "select * from Student1 where id=" + textBox1.Text;
                }
                SqlDataAdapter sda = new SqlDataAdapter(sql, sc);//实例化适配器
                DataTable dt = new DataTable();//实例化数据表
                sda.Fill(dt);//保存数据
                dataGridView1.DataSource = dt;//将数据呈现到窗体中
                
                sc.Close();//关闭数据库连接
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接失败" + ex.Message);
            }
        }
        //修改按钮
        private void button3_Click(object sender, EventArgs e)
        {
         
                new Form3().Show();
    
                Form3 f3 = new Form3();
                f3.dataFill();
    
                
    
        }
       
    }
    

    } 能直接把修改按钮补全吗

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5319 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 01:19 · PVG 09:19 · LAX 17:19 · JFK 20:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.