private void button1_Click(object sender, EventArgs e)
{
if (textBox4.Text.Trim() == "")
MessageBox.Show("请填写学号", "提示");
else
{
connection1.Open();
SqlCommand cmd0 = new SqlCommand("select * from 选课表 where 学号='" + textBox4.Text.Trim() + "'", connection1);
if (null == cmd0.ExecuteScalar())
MessageBox.Show("此学号不存在");
else
{
textBox1.Text = textBox4.Text;
SqlDataAdapter dr = new SqlDataAdapter("select 课程号 from 选课表 where 学号='" + textBox4.Text.Trim() + "'", connection1);
DataSet st = new DataSet();
dr.Fill(st, "选课表");
comboBox1.DisplayMember = "课程号";
comboBox1.ValueMember = "学号";
comboBox1.DataSource = st.Tables[0].DefaultView;
SqlCommand cmd3 = new SqlCommand("select 成绩 from 选课表 where 学号='" + textBox4.Text.Trim() + "'", connection1);
textBox3.Text = cmd3.ExecuteScalar().ToString();
}
}
connection1.Close();
}
是怎么修改表的内容的?
{
if (textBox3.Text.Trim() == "")
MessageBox.Show("请填写完整信息", "提示");
else
{
connection1.Open();
SqlCommand cmd = new SqlCommand("select * from 选课表 where 学号='" + textBox4.Text.Trim() + "'", connection1);
if (cmd.ExecuteScalar() == null)
MessageBox.Show("此学号不存在");
else
{
string sql, sql1,sql2;
sql1 = "UPDATE 选课表 SET 成绩=" + textBox3.Text.ToString();
sql2 = " where 学号='" + textBox4.Text.ToString () + "' and 课程号='" + comboBox1.Text.Trim();
sql = sql1 + sql2 + "';";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
MessageBox.Show("修改分数成功");
textBox1.Clear();
textBox1.Focus();
textBox3.Clear();
textBox4.Clear();
}
connection1.Close();
}
} private void 修改选课_Load(object sender, EventArgs e)
{
} private void button3_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定要删除该条信息吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
connection1.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection1;
string sql = "delete from 选课表 where 学号='" + textBox4.Text.Trim() + "'";
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
SqlDataReader dl = cmd.ExecuteReader();
MessageBox.Show("删除成功");
textBox1.Clear();
textBox1.Focus();
textBox3.Clear();
textBox4.Clear();
dl.Close();
connection1.Close();
}
else
{
this.Close();
}
}
那这些呢
"select 课程号 from 选课表 where 学号='" + textBox4.Text.Trim() + "'", connection1
"select 成绩 from 选课表 where 学号='" + textBox4.Text.Trim() +
以上是你的程序里出现的语句,哪有update或者insert 语句啊,怎么修改数据库的内容,只是查询而已。
sql2 = " where 学号='" + textBox4.Text.ToString () + "' and 课程号='" + comboBox1.Text.Trim();
根据学号 课程号 修改成绩
用ADO.NET里封装的类与方法,来操作数据库中的数据。
Sqlconnection 就是访问System.Data.SqlClient
而SqldataReader 是访问System.Data