




已阅读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_
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中信银行绍兴市上虞区2025秋招笔试性格测试题专练及答案
- 兴业银行佛山市高明区2025秋招笔试专业知识题专练及答案
- 民生银行天津市河东区2025秋招结构化面试经典题及参考答案
- 平安银行西安市莲湖区2025秋招笔试专业知识题专练及答案
- 广发银行上海市黄浦区2025秋招结构化面试经典题及参考答案
- 2025国家电网招聘考试模拟试题【A卷】附答案详解
- 2025年嘉兴市检察机关聘用制书记员招录22人笔试备考题库及答案详解一套
- 2025年度中国地质调查局自然资源综合调查指挥中心招聘社会在职人笔试备考试题及答案详解一套
- 民生银行烟台市莱州市2025秋招信息科技岗笔试题及答案
- 光大银行莆田市涵江区2025秋招面试典型题目及参考答案
- 2025秋新教材统编版八年级上册道德与法治第十一课 军强才能国安 教案(共2课时)
- 电梯安全总监培训记录课件
- 2025四川省水电投资经营集团有限公司所属电力公司员工招聘6人备考模拟试题及答案解析
- 房地产中介居间服务合同5篇
- 童话中的英雄勇敢的小矮人作文10篇范文
- 康复科的科室介绍
- 公安校园欺凌课件大纲
- 2025年江苏省南京市中考历史真题卷含答案解析
- 2025-2026学年浙教版小学劳动技术一年级上册教学计划及进度表
- 甲状腺疾病课件
- 数控滚齿机操作指导手册
评论
0/150
提交评论