综合设计实验__成语词典查询系统设计.docx_第1页
综合设计实验__成语词典查询系统设计.docx_第2页
综合设计实验__成语词典查询系统设计.docx_第3页
综合设计实验__成语词典查询系统设计.docx_第4页
综合设计实验__成语词典查询系统设计.docx_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

综合设计实验 成语词典查询系统设计一、实验目的1了解sql语言各语句的语法与使用方法;2掌握dataprovider 和dataset两个核心组件的常用属性、方法的含义及使用方法;3掌握利用dataprovider 和dataset两个核心组件实现数据库记录的插入、修改、删除的方法;4使学生能够通过老师讲过的内容灵活运用多种控件,实现对简单数据库的维护,能够自行调试,显示或保存实验结果。并使学生更深入的掌握面向对象程序设计这门课程。二、基本要求(1)创建成语词典查询系统所需的表(成语词典表),并能连接上数据库。 (2)完成对所建成语词典表的插入、修改、删除功能(3)完成对成语的精确和模糊查询(4)完成对成语词典查询结果保存为word文档及其导入成语词典。(5)为完成上述功能,还需运用菜单、工具条等多种控件三、实验步骤:1. 创建数据库:打开sql server management studio,创建数据库”成语字典库“然后新建一个名为成语字典表的表,如图1所示。图12. 创建项目:在microsoft visual studio 2010中创建一个名为“成语字典应用”的windows窗体应用程序项目。3. 设计界面:在空白窗体中添加菜单button、label、textbox、combobox、richtextbox、datagridview、menustrip、sqlconnection、sqldataadapter、sqlcommand、savefiledialog、openfiledialog控件,对控件的属性进行修改,如表1所示。并且对菜单栏上的选项设置快捷键和访问键。控件名称属性属性值menustripitems浏览(查看所有、保存结果、退出)、查询(精确查询、模糊查询)、添加、删除、修改、退出label1text选择查询方式:groupbox1text操作界面label2text设置查询值:label3text显示界面:label4text拼音:label5text成语:label6text备注:label7text拼音简写:label8textlabel8button1text精确查询button2text模糊查询button3text添加button4text修改button5text删除button6text导出到word文件button7text从word文件中导入combobox1items拼音、成语、备注、拼音简写表1控件属性及属性值图二图二为进行整体布局后窗体效果图。4. 在form1.h 编写菜单、按钮、标签的事件(单击事件)。(1)在窗体的头文件中引用system:data:sqlclient命名空间后,才可以使用该命名空间内定义的如sqlconnection等类的对象。因此需要在窗体的头文件中添加如下语句: using namespace system:data:sqlclient;因为要有文件的导出和导入所以要在命名空间部分加上:using namespace system:io;并且在窗体类中要定义一个sqlconnection 类型的对象con,并在form1类的构造函数中进行初始化。代码如下所示。 form1(void)initializecomponent();/todo: 在此处添加构造函数代码/con=gcnew sqlconnection();con-connectionstring = ldata source=.;initial catalog=成语字典库;integrated security=true;/用connection控件链接到服务器名为”.“数据库名为“成语字典库“,用windows身份验证方式登陆sqlconnection con; /在窗体的头文件中定义一个con(2)在视图设计器下点击菜单栏“浏览”双击“查看所有”便进入此按键的单击事件函数下编写代码。private: system:void 查看所有toolstripmenuitem_click(system:object sender, system:eventargs e) string sql=select * from 成语字典表;/定义了sql字符串,其内容为sqlsever数据库的查询语句 dataset ds=gcnew dataset();/定义了数据集的对象ds sqldataadapter ourda=gcnew sqldataadapter(sql,con); try /后面写可能发生的异常事件 ourda-fill(ds,zd); this-datagridview1-datasource=ds-tableszd;/ datagridview1中显示表中的内容 con-open(); if (con-state=connectionstate:open) string sql= select count(*) from 成语字典表; sqlcommand cmd = gcnew sqlcommand( sql,con); string myinformation=表中成语的总数是: + cmd-executescalar()-tostring() + 条; label8-text=myinformation; /label8显示表中成语的总数 if (con-state=connectionstate:open) con-close(); catch(system:data:sqlclient:sqlexception ex)/显示异常信息 messagebox:show(数据的异常信息是:+ex-message,提示信息); (3)在视图设计器下点击菜单栏“查询”双击“精确查询”便进入此按键的单击事件函数下编写代码。private: system:void 精确查询toolstripmenuitem_click(system:object sender, system:eventargs e) string sql= select * from 成语字典表 where +combobox1-text+ =+textbox1-text+; messagebox:show(sql);/显示sql语句 datatable ourtable=gcnew datatable(); sqldatareaderrd; sqlcommandcmd=gcnew sqlcommand( sql,con); sqldataadapter ourda = gcnew sqldataadapter( sql,con); if(textbox1-text=) messagebox:show(请输入要查找成语的相关信息); 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 ex) messagebox:show(数据异常信息是:+ex-errors,提示信息); return; finally rd-close(); if(con-state=connectionstate:open) con-close(); try ourda-fill(ourtable); this-datagridview1-datasource=ourtable; catch(system:data:sqlclient:sqlexception ex) messagebox:show(数据异常信息是:+ex-errors,提示信息); (4)在视图设计器下点击菜单栏“查询”双击“模糊查询”便进入此按键的单击事件函数下编写代码。模糊查询的代码只需将sql查询语句变为string sql= select * from 成语字典表 where +combobox1-text+ like%+textbox1-text+%;(5)在视图设计器下双击菜单栏里“添加”项便进入此按键的单击事件函数下编写代码。private: system:void 添加toolstripmenuitem_click(system:object sender, system:eventargs e) if(textbox3-text=) messagebox:show(添加的对象内容不能为空); return; try con-open(); if (con-state=connectionstate:open) stringsql=insert into 成语字典表(拼音,成语,备注,拼音简写)values(+textbox2-text+,+textbox3-text+,+textbox4-text+,+textbox5-text+); messagebox:show(sql); sqlcommandcmd=gcnew sqlcommand(sql,con); cmd-executenonquery(); messagebox:show(添加成功记录); catch(sqlexceptionex) messagebox:show(数据的异常信息是:+ex-message,提示信息); finally if(con-state=connectionstate:open) con -close(); (6)在视图设计器下双击菜单栏里“修改”项便进入此按键的单击事件函数下编写代码。private: system:void 修改toolstripmenuitem_click(system:object sender, system:eventargs e) string sql= update 成语字典表 set +combobox1-text+ =+textbox2-text+ where +combobox1-text+ =+textbox1-text+; messagebox:show(sql); datatable ourtable=gcnew datatable(); sqldataadapter ourda = gcnew sqldataadapter( sql,con); try ourda-fill(ourtable); catch(system:data:sqlclient:sqlexception ex) messagebox:show(数据的异常信息是:+ex-message,提示信息); (7)在视图设计器下双击菜单栏里“删除”项便进入此按键的单击事件函数下编写代码。private: system:void 删除toolstripmenuitem_click(system:object sender, system:eventargs e) if(combobox1-text=|textbox1-text=)/选择删除方式和设置删除值不能为空 messagebox:show(删除对象的内容不能为空); return; if(windows:forms:dialogresult:ok!=messagebox:show(确定要删除记录吗?,删除,messageboxbuttons:okcancel) return;/如果点击取消则返回 try con-open(); if (con-state=connectionstate:open) string sql= delete from 成语字典表 where +combobox1-text+ =+textbox1-text+; messagebox:show(sql); sqlcommand cmd = gcnew sqlcommand( sql,con); cmd-executenonquery(); messagebox:show(您已经成功删除+combobox1-text+ =+textbox1-text+的记录); catch( sqlexception ex) messagebox:show(数据的异常信息是:+ex-message,提示信息); finally if (con-state=connectionstate:open) con-close(); (8)在视图设计器下双击菜单栏里“退出”项便进入此按键的单击事件函数下编写代码。private: system:void 退出toolstripmenuitem_click(system:object sender, system:eventargs e) this-close(); (9)在视图设计器下双击 “导出到word文件”按钮便进入此按键的单击事件函数下编写代码,思路是将富文本框中的内容导出到word。private: system:void button6_click(system:object sender, system:eventargs e) if(savefiledialog1-showdialog()=windows:forms:dialogresult:ok) streamwriter str=file:createtext(savefiledialog1-filename); str-write(richtextbox1-text);/内容从richtextbox1写入到word str-close(); (10)在视图设计器下双击 “从word文件中导入”按钮便进入此按键的单击事件函数下编写代码。private: system:void button7_click(system:object sender, system:eventargs e) openfiledialog1-initialdirectory=e:;/设置了起始目录openfiledialog1-filter=富文本文件(*.rtf)|*.rtf|文本文件(*.doc)|*.doc;/选择文件类型 if (system:windows:forms:dialogresult:ok=openfiledialog1-showdialog() srtfilename=openfiledialog1-filename; streamreader sd=file:opentext(srtfilename); this-richtextbox1-text=; this-richtextbox1-text=sd-readtoend();/内容添加到richtextbox1中 sd-close(); 四窗体运行和调试按下启动调试(f5)编译成功,如果有错误则应根据错误信息和位置找到错误并改正。也可以利用设置断点的方式来进行排错。我在程序调试过程中就遇到以下问题:控件忘记添加,数据库的链接失败,对象引用有错误等。在编写从word中导入的功能时遇到导入的内容如果是汉字就有错误,我就改用数据流的方式成功解决问题。界面上的所有功能都可以使用达到了实验要求。五实验小结本次实验的内容是在学习完c+与windows窗体应用程序的基础知识数据库sql server之后一次各知识点结合的综合性实验。本次实验运用所学知识对一些概念,一些知识点的理解更进一步加深。能够通过老师讲的内容灵活运用多种控件,实现对简单数据库的维护,能够自行调试,显或保存实验结果。对更深入的掌握面向对象程序设计这门课程有很大的帮助。六代码清单form1.h#pragma oncenamespace 成语字典应用 using namespace system;using namespace system:componentmodel;using namespace system:collections;using namespace system:windows:forms;using namespace system:data;using namespace system:data:sqlclient;using namespace system:drawing;using namespace system:io;/ / form1 摘要/ public ref class form1 : public system:windows:forms:formpublic:string srtfilename;form1(void)initializecomponent();/todo: 在此处添加构造函数代码/con=gcnew sqlconnection();con-connectionstring = ldata source=.;initial catalog=成语字典库;integrated security=true;sqlconnection con;private: system:data:sqlclient:sqlconnection sqlconnection1;public: private: system:data:sqlclient:sqlcommand sqlselectcommand1;private: system:data:sqlclient:sqlcommand sqlinsertcommand1;private: system:data:sqlclient:sqlcommand sqlupdatecommand1;private: system:data:sqlclient:sqlcommand sqldeletecommand1;private: system:data:sqlclient:sqldataadapter sqldataadapter1;private: system:data:sqlclient:sqlcommand sqlcommand1;private: system:windows:forms:label label8;private: system:windows:forms:label label6;private: system:windows:forms:label label5;private: system:windows:forms:label label4;private: system:windows:forms:button button3;private: system:windows:forms:button button4;private: system:windows:forms:button button5;private: system:windows:forms:button button6;private: system:windows:forms:textbox textbox3;private: system:windows:forms:label label7;private: system:windows:forms:textbox textbox5;private: system:windows:forms:textbox textbox2;private: system:windows:forms:textbox textbox4;private: system:windows:forms:groupbox groupbox1;private: system:windows:forms:richtextbox richtextbox1;private: system:windows:forms:savefiledialog savefiledialog1;private: system:windows:forms:button button7;private: system:windows:forms:openfiledialog openfiledialog1; protected:/ / 清理所有正在使用的资源。/ form1()if (components)delete components;private: system:windows:forms:menustrip menustrip1;protected: private: system:windows:forms:toolstripmenuitem 浏览toolstripmenuitem;private: system:windows:forms:toolstripmenuitem 查看所有toolstripmenuitem;private: system:windows:forms:toolstripmenuitem 保存结果toolstripmenuitem;private: system:windows:forms:toolstripseparator toolstripmenuitem1;private: system:windows:forms:toolstripmenuitem 退出toolstripmenuitem1;private: system:windows:forms:toolstripmenuitem 查询toolstripmenuitem;private: system:windows:forms:toolstripmenuitem 精确查询toolstripmenuitem;private: system:windows:forms:toolstripmenuitem 模糊查询toolstripmenuitem;private: system:windows:forms:toolstripmenuitem 添加toolstripmenuitem;private: system:windows:forms:toolstripmenuitem 修改toolstripmenuitem;private: system:windows:forms:toolstripmenuitem 删除toolstripmenuitem;private: system:windows:forms:toolstripmenuitem 刷新toolstripmenuitem;private: system:windows:forms:toolstripmenuitem 退出toolstripmenuitem;private: system:windows:forms:label label1;private: system:windows:forms:label label2;private: system:windows:forms:label label3;private: system:windows:forms:datagridview datagridview1;private: system:windows:forms:button button1;private: system:windows:forms:button button2;private: system:windows:forms:combobox combobox1;private: system:windows:forms:textbox textbox1;private:/ / 必需的设计器变量。/ system:componentmodel:container components;#pragma region windows form designer generated code/ / 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ void initializecomponent(void)this-menustrip1 = (gcnew system:windows:forms:menustrip();this-浏览toolstripmenuitem = (gcnew system:windows:forms:toolstripmenuitem();this-查看所有toolstripmenuitem = (gcnew system:windows:forms:toolstripmenuitem();this-保存结果toolstripmenuitem = (gcnew system:windows:forms:toolstripmenuitem();this-toolstripmenuitem1 = (gcnew system:windows:forms:toolstripseparator();this-退出toolstripmenuitem1 = (gcnew system:windows:forms:toolstripmenuitem();this-查询toolstripmenuitem = (gcnew system:windows:forms:toolstripmenuitem();this-精确查询toolstripmenuitem = (gcnew system:windows:forms:toolstripmenuitem();this-模糊查询toolstripmenuitem = (gcnew system:windows:forms:toolstripmenuitem();this-添加toolstripmenuitem = (gcnew system:windows:forms:toolstripmenuitem();this-修改toolstripmenuitem = (gcnew system:windows:forms:toolstripmenuitem();this-删除toolstripmenuitem = (gcnew system:windows:forms:toolstripmenuitem();this-刷新toolstripmenuitem = (gcnew system:windows:forms:toolstripmenuitem();this-退出toolstripmenuitem = (gcnew system:windows:forms:toolstripmenuitem();this-label1 = (gcnew system:windows:forms:label();this-label2 = (gcnew system:windows:forms:label();this-label3 = (gcnew system:windows:forms:label();this-datagridview1 = (gcnew system:windows:forms:datagridview();this-button1 = (gcnew system:windows:forms:button();this-button2 = (gcnew system:windows:forms:button();this-combobox1 = (gcnew system:windows:forms:combobox();this-textbox1 = (gcnew system:windows:forms:textbox();this-sqlconnection1 = (gcnew system:data:sqlclient:sqlconnection();this-sqlselectcommand1 = (gcnew system:data:sqlclient:sqlcommand();this-sqlinsertcommand1 = (gcnew system:data:sqlclient:sqlcommand();this-sqlupdatecommand1 = (gcnew system:data:sqlclient:sqlcommand();this-sqldeletecommand1 = (gcnew system:data:sqlclient:sqlcommand();this-sqldataadapter1 = (gcnew system:data:sqlclient:sqldataadapter();this-sqlcommand1 = (gcnew system:data:sqlclient:sqlcommand();this-label8 = (gcnew system:windows:forms:label();this-label6 = (gcnew system:windows:forms:label();this-label5 = (gcnew system:windows:forms:label();this-label4 = (gcnew system:windows:forms:label();this-button3 = (gcnew system:windows:forms:button();this-button4 = (gcnew system:windows:forms:button();this-button5 = (gcnew system:windows:forms:button();this-button6 = (gcnew system:windows:forms:button();this-textbox3 = (gcnew system:windows:forms:textbox();this-label7 = (gcnew system:windows:forms:label();this-textbox5 = (gcnew system:windows:forms:textbox();this-textbox2 = (gcnew system:windows:forms:textbox();this-textbox4 = (gcnew system:windows:forms:textbox();this-groupbox1 = (gcnew system:windows:forms:groupbox();this-richtextbox1 = (gcnew system:windows:forms:richtextbox();this-savefiledialog1 = (gcnew system:windows:forms:savefiledialog();this-button7 = (gcnew system:windows:forms:button();this-openfiledialog1 = (gcnew system:windows:forms:openfiledialog();this-menustrip1-suspendlayout();(cli:safe_cast(this-datagridview1)-begininit();this-groupbox1-suspendlayout();this-suspendlayout();/ / menustrip1/ this-menustrip1-items-addrange(gcnew cli:array(7) this-浏览toolstripmenuitem, this-查询toolstripmenuitem, this-添加toolstripmenuitem, this-修改toolstripmenuitem, this-删除toolstripmenuitem, this-刷新toolstripmenuitem, this-退出toolstripmenuitem);this-menustrip1-location = system:drawing:point(0, 0);this-menustrip1-name = lmenustrip1;this-menustrip1-size = system:drawing:size(619, 25);this-menustrip1-tabindex = 0;this-menustrip1-text = lmenustrip1;/ / 浏览toolstripmenuitem/ this-浏览toolstripmenuitem-dropdownitems-addrange(gcnew cli:array(4) this-查看所有toolstripmenuitem, this-保存结果toolstripmenuitem, this-toolstripmenuitem1, this-退出toolstripmenuitem1);this-浏览toolstripmenuitem-name = l浏览toolstripmenuitem;this-浏览toolstripmenuitem-shortcutkeys = static_cast(system:windows:forms:keys:alt | system:windows:forms:keys:f);this-浏览toolstripmenuitem-size = system:drawing:size(58, 21);this-浏览toolstripmenuitem-text = l浏览(&f);/ / 查看所有toolstripmenuitem/ this-查看所有toolstripmenuitem-name = l查看所有toolstripmenuitem;this-查看所有toolstripmenuitem-shortcutkeys = static_cast(system:windows:forms:keys:control | system:windows:forms:keys:a);this-查看所有toolstripmenuitem-size = system:drawing:size(189, 22);this-查看所有toolstripmenuitem-text = l查看所有(m);this-查看所有toolstripmenuitem-click += gcnew system:eventhandler(this, &form1:查看所有toolstripmenuitem_click);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论