All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Delphi 7功能强大的数据库访问技术
asp.net2.0验证控件之RequiredFieldValidator控件使用方法
Linux系统下Apache与Tomcat整合的简单方法_php资料_编程技术
ASP.NE技巧:GridView绑定DropdownList_[Asp.Net教程]
PHP has encountered an Access Violation解决方法总结_[PHP教程]
GridView添加统计(合计)行_[Asp.Net教程]
C#网络应用编程基础练习题与答案(二)_[Asp.Net教程]
心得:PHP对文本数据库的五大基本操作方法_php资料_编程技术
c#判断数据NULL值的方法_[Asp.Net教程]
ASP.NET,Forums,页面模型分析_.net资料_编程技术
Reading number is top 10 pictures
漂亮脸蛋魔鬼身材2
9.3阅兵全景图8-航空梯队
白嫩丰满美女照片
Look at the Spring Festival people crowded into the what kind
NeedWallpaper13
Household design classic black and white
这才是真正的人体艺术2
胸部遭到偷窥的女人们
Startling Russian girl blind date scene2
Fat bird to fly
Download software ranking
apache-tomcat-6.0.33
SQL2000 For 4IN1
株洲本地在线棋牌游戏
WebService在.NET中的实战应用教学视频 → 第3集
Twenty piece of palm leaf
Ashlynn Video3
I'm come from Beijing2
VC++6.0简体中文版
Sora aoi 120 minutes
C#程序员参考手册
delv published in(发表于) 2014/1/10 6:29:33 Edit(编辑)
C#网络应用编程基础练习题与答案(八)_[Asp.Net教程]

C#网络应用编程基础练习题与答案(八)_[Asp.Net教程]

C#网络应用编程基础练习题与答案(八)_[Asp.Net教程]

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();
  }
  }
  }
  }

来源:天极







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.