Professional Documents
Culture Documents
Lab10王宇鹏2251593
Lab10王宇鹏2251593
《数据库技术基础》
实验报告
实验报告题目:
指导教师: 袁科萍
日期: 2023 年 11 月 24 日
一、实验目的
1.学习用 SqlCommand 对象实现数据库查询和更新的基本方法。
2.熟练掌握 VC#.NET 访问数据库的多种方法。
二、实验内容
1.完成学生成绩录入、查询系统的功能界面设计。运行结果参考验图 10.1 与实验图
10.2。
(1)成绩录入:
设计代码:
static string path = @"Data Source=WANGYUPENG\WYP;Initial Catalog=university;Integrated
Security=True";
SqlConnection myconn=new SqlConnection(path);
private void Form1_Load(object sender, EventArgs e)
{
string querry = "select snum from student";
///
SqlCommand mycmd1 = new SqlCommand(querry, myconn);
myconn.Open();
SqlDataReader myreader1 = mycmd1.ExecuteReader();
while (myreader1.Read()) {
comboBox1.Items.Add(myreader1.GetValue(0).ToString());
}
myconn.Close();
comboBox2.Text = "请先选择学号";
label4.Text = " ";
}
private void button1_Click(object sender, EventArgs e)
{
string querry="insert into sc values ('"+comboBox1.Text+"','"+comboBox2.Text+"','"+textBox1.Text+"')";
////
myconn.Open();
SqlCommand mycmd3 = new SqlCommand(querry, myconn);
try
{
mycmd3.ExecuteNonQuery();
label4.Text = "该成绩已录入。";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
label4.Text = " ";
}
myconn.Close();
}
重复点击确认:
关闭弹窗后,查看班号栏:
(2)查询系统:
设计代码:
static string path = @"Data Source=WANGYUPENG\WYP;Initial Catalog=university;Integrated
Security=True";
SqlConnection myconn = new SqlConnection(path);
DataSet myset = new DataSet();
SqlDataAdapter myadapt;
private void Form2_Load(object sender, EventArgs e)
{
string querry1 = "select snum from student";
myadapt = new SqlDataAdapter(querry1, myconn);
myadapt.Fill(myset, "snum");
int num = myset.Tables[0].Rows.Count;
for (int i = 0; i < num ;i++ )
comboBox1.Items.Add(myset.Tables[0].Rows[i].ItemArray[0]);
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
string q21 = "select student.snum,sname,count(cnum),avg(score) from
student,sc,sections where student.snum='";
string q22 = "'and sections.secnum=sc.secnum and student.snum=sc.snum group by
student.sname,student.snum ";
string querry2 = q21 + comboBox1.SelectedItem.ToString() + q22;
myadapt = new SqlDataAdapter(querry2, myconn);
myadapt.Fill(myset, "score");
myset.Tables[1].PrimaryKey=new DataColumn[]{myset.Tables[1].Columns[0]};
int i =
myset.Tables[1].Rows.IndexOf(myset.Tables[1].Rows.Find(comboBox1.SelectedItem));
textBox1.Text = myset.Tables[1].Rows[i].ItemArray[1].ToString();
textBox2.Text = myset.Tables[1].Rows[i].ItemArray[2].ToString();
textBox3.Text = myset.Tables[1].Rows[i].ItemArray[3].ToString();
}
运行结果截图:
选择学号为 s001 后:
选择学号为 s008 后:
综上,程序可以正常运行。