《C#方面经典试题与答案.doc》由会员分享,可在线阅读,更多相关《C#方面经典试题与答案.doc(6页珍藏版)》请在三一文库上搜索。
1、5. 设计一个Windows应用程序,窗体上有一个TextBox控件、一个Button控件。要求,每当用户单击按钮时,文本框都会增加一行文字来反映单击的次数,例如“第3次单击按钮”。【解答】1) 窗体界面如图Ex5-5-1所示;2) 窗体中主要控件属性设置如表Ex5-5-1;表Ex5-5-1 窗体中的主要控件属性控件Name属性功能其它属性TextBox控件textBox1显示信息ScrollBars=Vertical; Multiline=TrueButton控件Button1触发添加信息事件3) 主要事件代码。双击代码全选1234567891011121314以下是引用片段: inti=1
2、; boolAdd=true; privatevoidbutton1_Click(objectsender,EventArgse) if(Add)textBox1.Text+=第+i+次单击按钮 ; i+; privatevoidbutton2_Click(objectsender,EventArgse) Add=false; 6. 编写一段程序,向ListBox控件listBox1中,自动添加10个数,每个数占一项。【解答】主要代码如下:双击代码全选1234567891011121314以下是引用片段: publicpartialclassForm1:Form intm=1; private
3、voidbutton1_Click(objectsender,EventArgse) for(inti=m;im+10;i+) listBox1.Items.Add(i); m=m+10; usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Data.SqlClient; namespace习题8_6_1 pu
4、blicpartialclassForm1:Form publicForm1() InitializeComponent(); /添加Button按钮在ListBox中显示结果 privatevoidbutton1_Click(objectsender,EventArgse) listBox1.Items.Add(年级平均成绩); stringconnectionString=Properties.Settings.Default.MyDatabaseConnectionString; /根据连接字符串创建SqlConnection实例 SqlConnectionconn=newSqlConn
5、ection(connectionString); /创建SqlCommand实例,并设置SQL语句和使用的连接实例 SqlCommandcmd=newSqlCommand(); cmd.CommandText=selectsubstring(学号,1,2)as年级,avg(成绩)as平均成绩fromMyTable2groupbysubstring(学号,1,2); cmd.Connection=conn; try conn.Open(); SqlDataReaderr=cmd.ExecuteReader(); while(r.Read()=true) listBox1.Items.Add(s
6、tring.Format(0级1,r0,r1); r.Close(); catch(Exceptionerr) MessageBox.Show(err.Message,计算成绩失败); finally conn.Close(); 2. 使用保持连接的方式编写程序,查询MyTable2中不及格学生的学号,姓名,性别,成绩。并将结果在ListBox中显示出来。【解答】双击代码全选12345678910111213141516171819202122232425262728293031以下是引用片段: usingSystem; usingSystem.Collections.Generic; usi
7、ngSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Data.SqlClient; namespace习题8_6_2 publicpartialclassForm1:Form publicForm1() InitializeComponent(); privatevoidbutton1_Click(objectsender,EventArgse) listBox1.Items.Add(学号姓名性别成绩);
8、stringconnectionString=Properties.Settings.Default.MyDatabaseConnectionString; /根据连接字符串创建SqlConnection实例 SqlConnectionconn=newSqlConnection(connectionString); /创建SqlCommand实例,并设置SQL语句和使用的连接实例 SqlCommandcmd=newSqlCommand(); cmd.CommandText= Select学号,姓名,性别,成绩FromMyTable2Where(成绩60); cmd.Connection=con
9、n; try conn.Open(); SqlDataReaderr=cmd.ExecuteReader(); while(r.Read()=true) listBox1.Items.Add(string.Format(0123,r0,r1,r2,r3); r.Close(); catch(Exceptionerr) MessageBox.Show(err.Message,查询成绩失败); finally conn.Close(); 3. 编写程序,以“编码名称”的样式在comboBox1中显示MyTable1的内容。【解答】双击代码全选1234567891011121314151617181
10、920212223242526272829303132333435363738394041424344458以下是引用片段: usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Data.SqlClient; namespace习题8_6_3 publicpartialclassForm1:Form publi
11、cForm1() InitializeComponent(); privatevoidForm1_Load(objectsender,EventArgse) stringconnectionString=Properties.Settings.Default.MyDatabaseConnectionString; /根据连接字符串创建SqlConnection实例 SqlConnectionconn=newSqlConnection(connectionString); /创建SqlCommand实例,并设置SQL语句和使用的连接实例 SqlCommandcmd=newSqlCommand(); cmd.CommandText=Select*FromMyTable1; cmd.Connection=conn; try conn.Open(); SqlDataReaderr=cmd.ExecuteReader(); while(r.Read()=true) comboBox1.Items.Add(string.Format(01,r0,r1); comboBox1.SelectedIndex=0; r.Close(); catch(Exceptionerr) MessageBox.Show(err.Message,显示数据失败); finally conn.Close();