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

2017-10-13 16:05:02 +08:00
 shuaigeawei123

写差不多了,就剩个修改,不会把数据库的值带入 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();

            

    }
   
}

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

1406 次点击
所在节点    C
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/397387

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX