写差不多了,就剩个修改,不会把数据库的值带入 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();
}
}
} 能直接把修改按钮补全吗