综合设计实验成语词典查询系统设计.docx

上传人:大张伟 文档编号:11752377 上传时间:2021-09-03 格式:DOCX 页数:40 大小:117.80KB
返回 下载 相关 举报
综合设计实验成语词典查询系统设计.docx_第1页
第1页 / 共40页
综合设计实验成语词典查询系统设计.docx_第2页
第2页 / 共40页
综合设计实验成语词典查询系统设计.docx_第3页
第3页 / 共40页
综合设计实验成语词典查询系统设计.docx_第4页
第4页 / 共40页
综合设计实验成语词典查询系统设计.docx_第5页
第5页 / 共40页
点击查看更多>>
资源描述

《综合设计实验成语词典查询系统设计.docx》由会员分享,可在线阅读,更多相关《综合设计实验成语词典查询系统设计.docx(40页珍藏版)》请在三一文库上搜索。

1、精品文档综合设计实验成语词典查询系统设计、实验目的1 . 了解SQL言各语句的语法与使用方法;2 .掌握DataProvider和DataSet两个核心组件的常用属性、方法的含义及 使用方法;3 .掌握利用DataProvider和DataSet两个核心组件实现数据库记录的插入、 修改、删除的方法;4 .使学生能够通过老师讲过的内容灵活运用多种控件,实现对简单数据库 的维护,能够自行调试,显示或保存实验结果。并使学生更深入的掌握 面向对象程序设计这门课程。二、基本要求(1)创建成语词典查询系统所需的表(成语词典表),并能连接上数据库。(2)完成对所建成语词典表的插入、修改、删除功能(3)完成对

2、成语的精确和模糊查询(4)完成对成语词典查询结果保存为 Word文档及其导入成语词典。(5)为完成上述功能,还需运用菜单、工具条等多种控件三、实验步骤:1 .创建数据库:打开 SQLServer Management Studio ,创建数据库”成语字典库然后新建一个名为成语字典表的表,如图1所示。百熊拼音简写tiaoyushLieni凤源凿眼对、ftysfengherili风和日西不fhrlgongxifaai恭喜爱财祝堂gxfchaikuotian海国天空格hlctkma daoch en-g gong马到成功酶mdcgtiantianxian gsha ng天天河上演ttxsxinpin

3、gqihe心情Xpjdl.NULLNUUNULLNULLEXF,成语字我库-db。.成语字里表 *西景资源苣这器 Q x谢S善割产,演后,(SQL Server 11.0,2100 - zxfzxf-p(小E1U数提存F二至统数据库性,软据性快照区J ReportServer|4 J ReportSe rverTem pDBb Qj成语字典库+1 _J教据宾关妾驾td 袅 国口茶烷表 Fil eTablesdbd成语手奥表2 .创建项目:在Microsoft Visual Studio 2010中创建一个名为“成语字典应用”的 Windows窗体应用程序项目。3 .设计界面:在空白窗体中添加菜

4、单 Button、Label、TextBox、ComboBoxRichTextBox、 DataGridView、MenuStrip、SqlConnection、SqlDataAdapter 、 SqlCommandSaveFileDialog 、OpenFileDialog 控件,对控件的属性进行修改,如表 1所示。并且对 菜单栏上的选项设置快捷键和访问键。控件名称属性属性值MenuStripItems浏览(查看所有、保存结果、退出)、查询(精 确查询、模糊查询)、添加、删除、修改、退出LabellText选择查询方式:GroupBoxIText操作界囿Label2Text设置查询值:Lab

5、el3Text显示界囿:Label4Text拼音:Label5Text成语:Label6Text备注:Label7Text拼音简写:Label8Textlabel8button1Text精确查询button2Text模糊查询button3Text添加button4Text修改button5Text删除button6Text导出到Word文件Button7Text从Word文件中导入comboBox1Items拼音、成语、备注、拼音简写表1控件属性及属性值02欢迎下载精品文档图二图二为进行整体布局后窗体效果图。4 .在Forml.h编写菜单、按钮、标签的事件(单击事件)。(1)在窗体的头文件中引

6、用 System:Data:SqlClient命名空间后,才可以使用该命名空间内定义的如SqlConnection等类的对象。因此需要在窗体的头文件 中添加如下语句:using namespace System二Data二SqlClient;因为要有文件的导出和导入所以要在命名空间部分加上:using namespace System:: IO;并且在窗体类中要定义一个 SqlConnecti0nA 类型的对象con,并在Forml 类的构造函数中进行初始化。代码如下所示。成语字典Forml(void) InitializeComponent(); / /TODO:在此处添加构造函数代码 /

7、con=gcnew SqlConnection (); con- Connectionstring = LData Source=.;Initial Catalog=3欢立下载精品文档库;Integrated Security=True;/用Connection控件链接到服务器名为”.”数据库名为“成语字典库,用windowsIH验证方式登陆SqlConnection人con; /在窗体的头文件中定义一个con( 2)在视图设计器下点击菜单栏“浏览” 双击“查看所有” 便进入此按键的单击事件函数下编写代码。private:System: Void 查看所有 ToolStripMenuItem_

8、Click(System: Object A sender,System: EventArgs a e)String A sql=select * from 成语字典表;/ 定义了 sql 字符串,其内容为sqlsever数据库的查询语句DataSet A ds=gcnew DataSet ();/ 定义了数据集的对象dsSqlDataAdapter A ourda=gcnew SqlDataAdapter (sql,con); try / 后面写可能发生的异常事件ourda- Fill (ds,zd);this-dataGridView1- DataSource =ds- Tables zd

9、;/ dataGridView1 中显 示表中的内容con- Open();if (con- State =ConnectionState : Open)String A sql= select count(*) from 成语字典表;SqlCommandA cmd = gcnew SqlComman(d sql,con);String A myinformation= 表中成语的总数是: +cmd-ExecuteScalar ()- ToString () + 条;label8- Text =myinformation; /label8 显示表中成语的总数 if (con- State =C

10、onnectionState : Open) con- Close ();catch( System: Data: SqlClient : SqlException A ex)/ 显示异常信息 MessageBox: Show( 数据的异常信息是:+ex- Message, 提示信息 );( 3)在视图设计器下点击菜单栏“查询 ” 双击“精确查询 ” 便进入此按键的单 击事件函数下编写代码。private:System: Void 精确查询 ToolStripMenuItem_Click(System: Object A sender,System: EventArgs A e)String

11、A sql= select * from 成语字典表where +comboBox1- Text +=+textBox1- Text +;MessageBox: Show(sql);/ 显示 sql 语句 DataTable A ourtable=gcnew DataTable ();SqlDataReader Ad;SqlCommandcmd=gcnew SqlCommandsql,con);SqlDataAdapter A ourda = gcnew SqlDataAdapter ( sql,con); if(textBox1- Text =)MessageBox: Show( 请输入要查

12、找成语的相关信息);return;Try/ 将查询到的数据添加到富文本框中为导出做准备 con- Open();rd=cmd- ExecuteReader ();if(rd- Read()richTextBox1-Text+=rd拼音-ToString()+t;richTextBox1-Text+=rd成语-ToString()+t;richTextBox1-Text+=rd备注-ToString()+t;richTextBox1- Text +=rd 拼音简写 - ToString ()+n;catch( System: Data: SqlClient : SqlException A ex

13、) MessageBox: Show( 数据异常信息是: +ex- Errors , 提示信息 ); return;finallyrd- Close ();if(con- State =ConnectionState : Open)con- Close ();tryourda- Fill (ourtable);this-dataGridView1- DataSource =ourtable;catch( System: Data: SqlClient : SqlException A ex) MessageBox: Show( 数据异常信息是: +ex- Errors , 提示信息 );( 4

14、)在视图设计器下点击菜单栏“查询 ” 双击“模糊查询 ” 便进入此按键的单击事件函数下编写代码。模糊查询的代码只需将sql 查询语句变为 String A sql= select * from 成语字典表where +comboBox1- Text + like%+textBox1- Text +%;5)在视图设计器下双击菜单栏里“添加” 项便进入此按键的单击事件函数下 编写代码。private:System: Void 添力口ToolStripMenuItem_Click(System: Object A sender,System:: EventArgs a e)if(textBox3-

15、Text =) MessageBox: Show( 添加的对象内容不能为空);return; try con- Open(); if (con- State =ConnectionState : Open) String Asql=insert into 成语字典表(拼音 , 成语 , 备注 , 拼音简写)values(+textBox2-Text+?+textBox3-Text +,+textBox4- Text +,+textBox5-ext +);MessageBox: Show(sql);SqlCommanAdcmd=gcnew SqlComman(dsql,con);cmd- Exe

16、cuteNonQuery();MessageBox: Show( 添加成功记录); catch( SqlException Aex) MessageBox: Show( 数据的异常信息是: +ex- Message, 提示信息 );finally if(con- State =ConnectionState : Open) con - Close ();( 6)在视图设计器下双击菜单栏里“修改” 项便进入此按键的单击事件函数下编写代码。private: System: Void 修改 ToolStripMenuItem_Click( System: Object A sender, Syste

17、m: EventArgs A e)String A sql= update 成语字典表set +comboBox1- Text +=+textBox2- Text + where +comboBox1- Text + =+textBox1- Text +;MessageBox: Show(sql);DataTable A ourtable=gcnew DataTable ();SqlDataAdapter A ourda = gcnew SqlDataAdapter ( sql,con); try ourda- Fill (ourtable);catch( System:: Data:: Sq

18、lClient 二 SqlException 人 ex) MessageBox: Show( 数据的异常信息是: +ex- Message, 提示信息 ); ( 7)在视图设计器下双击菜单栏里“删除” 项便进入此按键的单击事件函数下编写代码。private:System:: Void 删除ToolStripMenuItem_Click( System: Object A sender,System: EventArgs a e)if(comboBox1- Text =|textBox1- Text =)/ 选择删除方式和设置删除值不能为空MessageBox: Show( 删除对象的内容不能为

19、空);return; if( Windows: Forms: DialogResult : OK!= MessageBox: Show( 确定要删除记录吗? , 删除 , MessageBoxButtons: OKCancel) return;/ 如果点击取消则返回 try con- Open();if (con- State =ConnectionState : Open)String A sql= delete from 成语字典表where +comboBox1- Text +=+textBox1- Text +;MessageBox: Show(sql);SqlCommanAd cmd

20、 = gcnew SqlComman(d sql,con);cmd- ExecuteNonQuery();MessageBox: Show( 您已经成功删除+comboBox1-Text +=+textBox1- Text + 的记录 );catch( SqlException A ex) MessageBox: Show( 数据的异常信息是: +ex- Message, 提示信息 ); finallyif (con- State =ConnectionState : Open) con- Close (); ( 8)在视图设计器下双击菜单栏里“退出” 项便进入此按键的单击事件函数下编写代码。

21、private: System: Void 退出 ToolStripMenuItem_Click( System: Object A sender, System: EventArgs A e) this- Close ();(9)在视图设计器下双击”导出到Wor叔件”按钮便进入此按键的单击事件函数下编写代码,思路是将富文本框中的内容导出到word。private:System:: Void button6_Click( System:: Object A sender, System:: EventArgs A e)if(saveFileDialog1- ShowDialog()= Wind

22、ows: Forms: DialogResult : OK) StreamWriter A str= File : CreateText (saveFileDialog1- FileName); str- Write (richTextBox1- Text );/ 内容从 richTextBox1 写入到 word str- Close (); (10)在视图设计器下双击“从Worcft件中导入”按钮便进入此按键的单击事件函数下编写代码。private: System: Void button7_Click( System: Object A sender, System: EventArgs

23、 A e) openFileDialog1- InitialDirectory =E:;/ 设置了起始目录openFileDialog1- Filter =富文本文件(*.rtf)|*.rtf| 文本文件 (*.doc)|*.doc;/选择文件类型if( System: Windows: Forms: DialogResult : OK=openFileDialog1- ShowDialog () srtFileName=openFileDialog1- FileName;StreamReaderA sd= File : OpenText(srtFileName);this-richTextB

24、ox1-Text =;this-richTextBox1-Text =sd- ReadToEnd();/ 内容添加到 richTextBox1 中sd- Close (); 四窗体运行和调试按下启动调试(F5)编译成功,如果有错误则应根据错误信息和位置找到错误并改正。 也可以利用设置断点的方式来进行排错。 我在程序调试过程中就遇到以下问题:控件忘记添加,数据库的链接失败,对象引用有错误等。在编写从word 中导入的功能时遇到导入的内容如果是汉字就有错误,我就改用数据流的 方式成功解决问题。界面上的所有功能都可以使用达到了实验要求。五实验小结本次实验的内容是在学习完C+W Windows窗体应用

25、程序的基础知识数据库 SQL Server 之后一次各知识点结合的综合性实验。本次实验运用所学知识对一些概念, 一些知识点的理解更进一步加深。 能够通过老师讲的内容灵活运用多种10 欢迎下载。控件,实现对简单数据库的维护,能够自行调试,显或保存实验结果。对更深入的掌握面向对象程序设计这门课程有很大的帮助。六代码清单Form1.h#pragma oncenamespace 成语字典应用 using namespace using namespace using namespaceusing namespaceSystem;System: ComponentMode;lSystem: Collec

26、tions ;System: Windows: Forms;using namespace using namespace using namespaceSystem: Data;System: Data: SqlClient ;System: Drawing ;using namespace System: IO;/ / Form1 摘要/ public ref class Form1 : public System: Windows: Forms: Formpublic:String 人 srtFileName;Form1(void)InitializeComponent();/TODO:

27、 在此处添加构造函数代码 /con=gcnew SqlConnection ();con- ConnectionString = LData Source=.;Initial Catalog= 成语字典 库;Integrated Security=True;SqlConnection A con;private:System:Data:SqlClient : SqlConnection A sqlConnection1;public:private:System:Data:SqlClient : SqlCommanAd sqlSelectCommand1;private:System:Data

28、:SqlClient : SqlCommanAd sqlInsertCommand1;private:System:Data:SqlClient : SqlCommanAd sqlUpdateCommand1;private:System:Data:SqlClient : SqlCommanAd sqlDeleteCommand1;private:System:Data:SqlClient : SqlDataAdapter A sqlDataAdapter1;private:System:Data:SqlClient : SqlCommanAd sqlCommand1;精品文档private:

29、System:Windows:Forms:Label A label8;private:System:Windows:Forms:Label 1A iabei6;private:System:Windows:Forms:Label A label5;private:System:Windows:Forms:Label A label4;private:System:Windows:Forms:Button A button3;private:System:Windows:Forms:Button A button4;private:System:Windows:Forms:Button A b

30、utton5;private:System:Windows:Forms:Button A button6;private:System:Windows:Forms:TextBoxA textBox3;private:System:Windows:Forms:Label A label7;private:System:Windows:Forms:TextBoxA textBox5;private:System:Windows:Forms:TextBoxA textBox2;private:System:Windows:Forms:TextBoxA textBox4;private:System:

31、Windows:Forms:GroupBoxA groupBox1;private:System:Windows:Forms:RichTextBox A richTextBox1;private:System:Windows:Forms:SaveFileDialog A saveFileDialog1;private:System:Windows:Forms:Button A button7;private:System:Windows:Forms:OpenFileDialog A openFileDialog1;protected:/ / 清理所有正在使用的资源。/ Form1()if (c

32、omponents)delete components;private:protected:System: Windows: Forms: MenuStrip A menuStrip1;private:System: Windows: Forms: ToolStripMenuItem A 浏览 ToolStripMenuItem;private:System: Windows: Forms: ToolStripMenuItem A 查看所有ToolStripMenuItem;private:System: Windows: Forms: ToolStripMenuItem A 保存结果Tool

33、StripMenuItem;private:System: Windows: Forms: ToolStripSeparator A toolStripMenuItem1;private:System: Windows: Forms: ToolStripMenuItem A 退出 ToolStripMenuItem1;private:System: Windows: Forms: ToolStripMenuItem A 查询 ToolStripMenuItem;private:System: Windows: Forms: ToolStripMenuItem A 精确查询ToolStripMe

34、nuItem;private:System:Windows:Forms:ToolStripMenuItemA模糊查询ToolStripMenuItem;private:System:Windows:Forms:ToolStripMenuItemA添加 ToolStripMenuItem;private:System:Windows:Forms:ToolStripMenuItemA修改 ToolStripMenuItem;private:System:Windows:Forms:ToolStripMenuItemA删除 ToolStripMenuItem;private:System:Windo

35、ws:Forms:ToolStripMenuItemA刷新 ToolStripMenuItem;private:System:Windows:Forms:ToolStripMenuItemA退出 ToolStripMenuItem;private:System:Windows:Forms:Label A label1;private:System:Windows:Forms:Label A label2;private:System:Windows:Forms:Label A label3;private:System:Windows:Forms:DataGridView A dataGrid

36、View1;private:System:Windows:Forms:Button A button1;private:System:Windows:Forms:Button A button2;private:private:System: Windows:System: Windows:Forms: ComboBoX comboBoxI;Forms: TextBoxA textBoxl;private:/ / 必需的设计器变量。/ System:: ComponentModel: Container Acomponents;#pragma region Windows Form Desig

37、ner generated code/ / 设计器支持所需的方法- 不要/ 使用代码编辑器修改此方法的内容。/ void InitializeComponent(void)this-menuStrip1 = (gcnew System: Windows: Forms: MenuStrip ();this- 浏览 ToolStripMenuItem = (gcnewSystem: Windows: Forms: ToolStripMenuItem ();this- 查看所有ToolStripMenuItem = (gcnewSystemWindowsFormsToolStripMenuItem(

38、);this- 保存结果ToolStripMenuItem = (gcnewSystemWindowsFormsToolStripMenuItem();this-toolStripMenuItem1 = (gcnewSystemWindowsFormsToolStripSeparator();this- 退出 ToolStripMenuItem1 = (gcnewSystemWindowsFormsToolStripMenuItem();this- 查询 ToolStripMenuItem = (gcnewSystemWindowsFormsToolStripMenuItem();this-

39、精确查询ToolStripMenuItem = (gcnewSystemWindowsFormsToolStripMenuItem();this- 模糊查询ToolStripMenuItem = (gcnewSystemWindowsFormsToolStripMenuItem();this- 添加 ToolStripMenuItem = (gcnewSystemWindowsFormsToolStripMenuItem();this- 修改 ToolStripMenuItem = (gcnewSystemWindowsFormsToolStripMenuItem();this- 删除 Too

40、lStripMenuItem = (gcnewSystemWindowsFormsToolStripMenuItem();this- 刷新 ToolStripMenuItem = (gcnewSystemWindowsFormsToolStripMenuItem();this- 退出 ToolStripMenuItem = (gcnewSystemWindowsFormsToolStripMenuItem();this-label1 = (gcnewSystemWindowsFormsLabel();this-label2 = (gcnewSystemWindowsFormsLabel();t

41、his-label3 = (gcnewSystemWindowsFormsLabel();this-dataGridView1 = (gcnew System Windows Forms DataGridView ();this-button1 = (gcnewSystem Windows Forms Button ();this-button2 = (gcnewSystem Windows Forms Button ();this-comboBox1 = (gcnew System Windows Forms ComboBo(x); this-textBox1 = (gcnew System

42、 Windows Forms TextBox();this-sqlConnection1 = (gcnew System Data SqlClient SqlConnection ();39 欢迎下载。this-sqlSelectCommand1 = (gcnew this-sqlInsertCommand1 = (gcnew this-sqlUpdateCommand1 = (gcnew this-sqlDeleteCommand1 = (gcnew this-sqlDataAdapter1 = (gcnewSystemDataSqlClientSystemDataSqlClientSystemDataSqlClientSystemDataSqlClientSqlComma

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1