独上高楼网站
  •    你所在位置:首页 VS.netC#基础知识〉C#网络应用编程基础练习题与答案(八)
  • C#网络应用编程基础练习题与答案(八)
  • 作者:佚名  文章来源:http://www.knowsky.com/exam.asp  发布日期:2007-10-29  浏览次数:804
  • 打印这篇文章
  •   1. 使用保持连接的方式编写程序,计算各年级平均成绩,并显示结果。

      【解答】

    以下是引用片段:
      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 习题8_6_1
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }
      //添加Button按钮在ListBox中显示结果
      private void button1_Click(object sender, EventArgs e)
      {
      listBox1.Items.Add("年级 平均成绩");
      string connectionString = Properties.Settings.Default.MyDatabaseConnectionString;
      //根据连接字符串创建SqlConnection实例
      SqlConnection conn = new SqlConnection(connectionString);
      //创建SqlCommand实例,并设置SQL语句和使用的连接实例
      SqlCommand cmd = new SqlCommand();
      cmd.CommandText = "select substring(学号,1,2) as 年级,avg(成绩) as 平均成绩 from MyTable2 group by substring(学号,1,2)";
      cmd.Connection = conn;
      try
      {
      conn.Open();
      SqlDataReader r = cmd.ExecuteReader();
      while (r.Read() == true)
      {
      listBox1.Items.Add(string.Format("{0}级 {1}", r[0], r[1]));
      }
      r.Close();
      }
      catch (Exception err)
      {
      MessageBox.Show(err.Message, "计算成绩失败");
      }
      finally
      {
      conn.Close();
      }
      }
      }
      }

      2. 使用保持连接的方式编写程序,查询MyTable2中不及格学生的学号,姓名,性别,成绩。并将结果在ListBox中显示出来。

      【解答】

    以下是引用片段:
      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 习题8_6_2
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }
      private void button1_Click(object sender, EventArgs e)
      {
      listBox1.Items.Add(" 学号 姓名 性别 成绩");
      string connectionString = Properties.Settings.Default.MyDatabaseConnectionString;
      //根据连接字符串创建SqlConnection实例
      SqlConnection conn = new SqlConnection(connectionString);
      //创建SqlCommand实例,并设置SQL语句和使用的连接实例
      SqlCommand cmd = new SqlCommand();
      cmd.CommandText =
      "Select 学号,姓名,性别, 成绩 From MyTable2 Where (成绩<60)";
      cmd.Connection = conn;
      try
      {
      conn.Open();
      SqlDataReader r = cmd.ExecuteReader();
      while (r.Read() == true)
      {
      listBox1.Items.Add( string.Format("{0} {1} {2} {3}", r[0], r[1], r[2], r[3]));
      }
      r.Close();
      }
      catch (Exception err)
      {
      MessageBox.Show(err.Message, "查询成绩失败");
      }
      finally
      {
      conn.Close();
      }
      }
      }
      }

      3. 编写程序,以“[编码]名称”的样式在comboBox1中显示MyTable1的内容。

      【解答】

    以下是引用片段:
      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 习题8_6_3
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }
      private void Form1_Load(object sender, EventArgs e)
      {
      string connectionString = Properties.Settings.Default.MyDatabaseConnectionString;
      //根据连接字符串创建SqlConnection实例
      SqlConnection conn = new SqlConnection(connectionString);
      //创建SqlCommand实例,并设置SQL语句和使用的连接实例
      SqlCommand cmd = new SqlCommand();
      cmd.CommandText = "Select * From MyTable1";
      cmd.Connection = conn;
      try
      {
      conn.Open();
      SqlDataReader r = cmd.ExecuteReader();
      while (r.Read() == true)
      {
      comboBox1.Items.Add(string.Format("[{0}] {1}", r[0], r[1]));
      }
      comboBox1.SelectedIndex = 0;
      r.Close();
      }
      catch (Exception err)
      {
      MessageBox.Show(err.Message, "显示数据失败");
      }
      finally
      {
      conn.Close();
      }
      }
      }
      }

      4. 在画线处填上合适的内容,使程序变得正确完整。

    以下是引用片段:
      string connString="server=localhost;Integrated Security=SSPI;database=pubs";
      SqlConnection conn=____________________________
      string strsql="select * from MyTable2";
      SqlDataAdapter adapter=new SqlDataAdapter(_____________);
      dataset=new DataSet();
      adapter.Fill(________________,"MyTable2");
      this.dataGridView1.DataSource=dataset.Tables["MyTable2"];


      【解答】

    以下是引用片段:
      string connString="server=localhost;Integrated Security=SSPI;database=pubs";
      SqlConnection conn= new SqlConnection(Properties.Settings.Default.MyDatabaseConnectionString);
      string strsql="select * from MyTable2";
      SqlDataAdapter adapter=new SqlDataAdapter(conn);
      dataset=new DataSet();
      adapter.Fill(dataset,"MyTable2");
      this.dataGridView1.DataSource=dataset.Tables["MyTable2"];


      5. 已知数据库中定义了一张person表,表的数据结构如下:

      字段名称字段类型字段含义

      id数字编号

      xm文本姓名

      xb文本性别

      nl数字年龄

      zip文本邮政编码

      用编写代码的方法在DataGridView中显示该数据表中年龄大于18的所有纪录,显示时以编号的升序排序,要求禁止用户编辑数据。

      【解答】

    以下是引用片段:
      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 习题8_6_5
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }
      private void button1_Click(object sender, EventArgs e)
      {
      string connectionstring = Properties.Settings.Default.MyDatabaseConnectionString ;
      SqlConnection conn = new SqlConnection(connectionstring);
      try
      {
      conn.Open();
      SqlDataAdapter adapter = new SqlDataAdapter(
      "select id,xm,xb,nl from person where nl > 18 order by id", conn);
      DataSet dataset = new DataSet();
      //如果不指定表名,则系统自动生成一个默认的表名
      adapter.Fill(dataset, "person");
      //可以使用索引引用生成的表
      dataGridView1.DataSource = dataset.Tables["person"];
      adapter.Dispose();
      }
      catch (Exception err)
      {
      MessageBox.Show(err.Message);
      }
      finally
      {
      conn.Close();
      }
      }
      private void Form1_Load(object sender, EventArgs e)
      {
      //不允许用户直接在最下面的行添加新行
      dataGridView1.AllowUserToAddRows = false;
      //不允许用户直接按Delete键删除行
      dataGridView1.AllowUserToDeleteRows = false;
      }
      }
      }

      6.例8-18的存储过程定义中,将“@surname nvarchar(2),”改为“@surname nchar(2),”,是否仍然能够得到正确结果,为什么?

      【解答】

      不一定。因为如果传递的参数值为“王”,在存储过程中会自动变为“王 ”。

      7. 调用存储过程,设计程序完成下列功能:任意给出一个汉字,统计MyTable2中所有包含该汉字的人数,并显示统计结果。

      【解答】

    以下是引用片段:
      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 习题8_6_7
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }
      private void button1_Click(object sender, EventArgs e)
      {
      SqlConnection conn =
      new SqlConnection(Properties.Settings.Default.MyDatabaseConnectionString);
      SqlCommand cmd = new SqlCommand();
      cmd.Connection = conn;
      //设置SQL语句为存储过程名,命令类型为存储过程
      cmd.CommandText = "SelectFilterStudentsNum";
      cmd.CommandType = CommandType.StoredProcedure;
      //添加存储过程中参数需要的初始值,注意参数名要和存储过程定义的参数名相同
      if( textBox1.Text=="")
      {
      MessageBox.Show("请输入有效信息","错误");
      textBox1.Focus();
      return ;
      }
      cmd.Parameters.AddWithValue("@surname", textBox1.Text);
      cmd.Parameters.AddWithValue("@record", 0);
      //指定哪些参数需要返回结果
      cmd.Parameters["@record"].Direction = ParameterDirection.Output;
      try
      {
      conn.Open();
      //执行存储过程
      cmd.ExecuteNonQuery();
      //显示返回的结果
      MessageBox.Show(string.Format("有{0}条含 {1} 的记录",
      cmd.Parameters["@record"].Value,textBox1.Text));
      }
      catch (Exception err)
      {
      MessageBox.Show(err.Message);
      }
      finally
      {
      conn.Close();
      }
      }
      }
      }


    baidu
  • 打印这篇文章
  • 与本文主题相关的文章
  • 返回首页