ImageVerifierCode 换一换
格式:DOCX , 页数:56 ,大小:85.08KB ,
资源ID:9808374      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9808374.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(综合设计实验成语词典查询系统设计.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

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

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

2、3)完成对成语的精确和模糊查询(4)完成对成语词典查询结果保存为Word文档及其导入成语词典。(5)为完成上述功能,还需运用菜单、工具条等多种控件三、实验步骤:1. 创建数据库:打开SQL Server Management Studio,创建数据库”成语字典库“然后新建一个名为成语字典表的表,如图1所示。图12. 创建项目:在Microsoft Visual Studio 2010中创建一个名为“成语字典应用”的Windows窗体应用程序项目。3. 设计界面:在空白窗体中添加菜单Button、Label、TextBox、ComboBox、RichTextBox、DataGridView、Me

3、nuStrip、SqlConnection、SqlDataAdapter、SqlCommand、SaveFileDialog、OpenFileDialog控件,对控件的属性进行修改,如表1所示。并且对菜单栏上的选项设置快捷键和访问键。控件名称属性属性值MenuStripItems浏览(查看所有、保存结果、退出)、查询(精确查询、模糊查询)、添加、删除、修改、退出Label1Text选择查询方式:GroupBox1Text操作界面Label2Text设置查询值:Label3Text显示界面:Label4Text拼音:Label5Text成语:Label6Text备注:Label7Text拼音简写

4、:Label8Textlabel8button1Text精确查询button2Text模糊查询button3Text添加button4Text修改button5Text删除button6Text导出到Word文件Button7Text从Word文件中导入comboBox1Items拼音、成语、备注、拼音简写表1控件属性及属性值图二 图二为进行整体布局后窗体效果图。4. 在Form1.h 编写菜单、按钮、标签的事件(单击事件)。(1)在窗体的头文件中引用System:Data:SqlClient命名空间后,才可以使用该命名空间内定义的如SqlConnection等类的对象。因此需要在窗体的头文件

5、中添加如下语句: 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=

6、成语字典库;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字符串,

7、其内容为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 成语字

8、典表; 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,

9、提示信息); (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(); SqlDataReaderr

10、d; 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成语-ToSt

11、ring()+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-Data

12、Source=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: Syste

13、m: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+); M

14、essageBox: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 修改Tool

15、StripMenuItem_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(ourt

16、able); 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(删除对象的内容

17、不能为空); 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 SqlComma

18、nd( 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 退出ToolStripMenu

19、Item_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:Create

20、Text(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=富文本文件(*.rt

21、f)|*.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

22、)编译成功,如果有错误则应根据错误信息和位置找到错误并改正。也可以利用设置断点的方式来进行排错。我在程序调试过程中就遇到以下问题:控件忘记添加,数据库的链接失败,对象引用有错误等。在编写从word中导入的功能时遇到导入的内容如果是汉字就有错误,我就改用数据流的方式成功解决问题。 界面上的所有功能都可以使用达到了实验要求。五实验小结 本次实验的内容是在学习完C+与Windows窗体应用程序的基础知识数据库SQL Server之后一次各知识点结合的综合性实验。本次实验运用所学知识对一些概念,一些知识点的理解更进一步加深。能够通过老师讲的内容灵活运用多种控件,实现对简单数据库的维护,能够自行调试,显

23、或保存实验结果。对更深入的掌握面向对象程序设计这门课程有很大的帮助。六代码清单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 Syst

24、em:Drawing; using namespace System:IO; / / Form1 摘要 / public ref class Form1 : public System:Windows:Forms:Form public: String srtFileName; Form1(void) InitializeComponent(); / /TODO: 在此处添加构造函数代码 / con=gcnew SqlConnection(); con-ConnectionString = LData Source=.;Initial Catalog=成语字典库;Integrated Secu

25、rity=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:Da

26、ta: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

27、: 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:For

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1