You are on page 1of 7

同济大学

《数据库技术基础》
实验报告

实验报告题目:

实验十 用 ADO.NET 对象访问数据库(二)

姓名: 王宇鹏 学号: 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();
}

private void comboBox2_Click(object sender, EventArgs e)


{
comboBox2.Items.Clear();
comboBox2.Text = "请先选择学号";
string q21 = "select secnum from sections where cnum not in(";
string q22 = "select cnum from sections where secnum in(";
string q23 = "select secnum from sc where snum='";
string querry2 = q21 + q22 + q23 + comboBox1.Text+ "' ))";
////
SqlCommand mycmd2 = new SqlCommand(querry2, myconn);
myconn.Open();
SqlDataReader myreader2 = mycmd2.ExecuteReader();
while (myreader2.Read())
{
comboBox2.Items.Add(myreader2.GetValue(0).ToString());
}
myconn.Close();
}

private void comboBox1_TextChanged(object sender, EventArgs e)


{
comboBox2.Items.Clear();
comboBox2.Text = "请先选择学号";
label4.Text = " ";
}
 运行界面截图:
程序界面打开后:
在此基础上,选择学号“s001”并点击 班号 栏后:

s001 所有课程成绩已录入,选择学号 s002 后,点击班号栏:

选择 13001 并输入分数 100,点击确认后:

重复点击确认:
关闭弹窗后,查看班号栏:

发现为 13001 输入分数后,13002 失效(同课不同班)。


综上,程序可以正常运行。

(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 后:
综上,程序可以正常运行。

You might also like