C#程序设计论文.doc_第1页
C#程序设计论文.doc_第2页
C#程序设计论文.doc_第3页
C#程序设计论文.doc_第4页
C#程序设计论文.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

C#程序设计论文 每一个成功者都有一个开始。勇于开始,才能找到成功的路。 13 / 14目 录第一章 要求分析.3第二章 总体分析.3第三章 详细设计.3第四章 具体实验方法.4第五章 实验代码.7第六章 总结.12第 一 章 要 求 分 析1.编程将一文本文件内容按照指定格式逐行解析并存入指定格式数据库 此要求可以分拆为先将文本文件内容在C#中应用程序中打开,然后对其逐行分割,在指定位置进行分割,最后再连接并存到事先在SQL Server数据库中自己建的表格当中。但是在这之前当然要确保电脑上有SQL Server数据库,没有的话就要先安装,然后再数据库中建表。2.编程读取数据库中内容并生成文本文件,使前后两个文本文件内容完全一致 此要求主要涉及数据库的读取。3.编程比较前后两个文本文件内容 原数据和从数据库中读取的数据进行对比。第 二 章 总 体 设 计1.在windows Form中插入一个menuStrips,一个openFileDialog窗体和一个richTextBox,用于搜索所给的数据并将其显示在richTextBox中。2.在windows Form 中再增加5个button,分别表示连接数据库、关闭数据库、插入数据、生成文本文件、对比。3.在每个windows窗体下写上具体的代码,以实现其功能。达到要求。第 三 章 详 细 设 计Windows Form界面的设计如图所示3-1所示 图 3-1 windows Form界面设计 第 四 章 具 体 实 验 方 法1.通过openFileDialog打开搜索所给的文本文件string fName = openFileDialog1.FileName;string ext = Path.GetExtension(fName);richTextBox1.Clear();然后再通过if判断文件格式,选择导入。2.连接打开数据库 mSqlConnection = new SqlConnection(); mSqlConnection.ConnectionStringserver=.;DataBase=c#Exam;uid=sa;pwd=920416; mSqlConnection.Open(); if (mSqlConnection.State = ConnectionState.Open) MessageBox.Show(mSqlConnection.State.ToString(); 3.关闭数据库时先要判断数据库是否已经打开,如果没有打开当然就不用关闭了,使用一个if语句即可实现。代码如下: if (mSqlConnection.State = ConnectionState.Open) mSqlConnection.Close(); MessageBox.Show(mSqlConnection.State.ToString(); 4.插入数据时先要将上面读到richTextBox1中的数据进行分割,然后才能存到数据库中。分割时,先用索引找到指定位置,再用Substring进行分割。这时要注意了,文本文件同一行的同一指定位置的字符不一定是相同的,要得进行个别对待。利用两个for循环分割的代码如下: for (int i = 1; i 18; i+) if (i = 1) a1 = richTextBox1.Text.IndexOf(n, 0); b1 = richTextBox1.Text.Substring(0, a1); else ai = richTextBox1.Text.IndexOf(n, ai - 1 + 1); bi = richTextBox1.Text.Substring(ai - 1 + 1, ai - ai - 1); for (k = 1; k 8; k+) if (k = 1) c1 = bi.IndexOf(t, 0); d1 = bi.Substring(0, c1); else if (k = 2) c2 = bi.IndexOf( , c1 + 1); d2 = bi.Substring(c1 + 1, c2 - c1 - 1); else if (k = 7) dk = bi.Substring(ck - 1 + 1, bi.Length - ck - 1 -1); else ck = bi.IndexOf(t, ck - 1 + 1); dk = bi.Substring(ck - 1 + 1, ck - ck - 1 - 1); 5. 从数据库中读取数据并生成文本文件时,需要用到sqlcommand及其StreamWriter,要有上面分割的时候用的Substring函数,在分割时连同分割位置的字符一起切掉了,所以在读数据时要再加上相应的字符。6. 比较时用到StreamReader将两个文件文件读入并进行对比。7.实验结果截图为: 图4-1 C#程序设计论文数据原图 图4-2 c#程序论文生成数据图 图4-3 数据库中所存入的数据截图第 五 章 实 验 代 码 using System;using System.Data;using System.Data.SqlClient;using System.IO;using System.Windows.Forms;using System.Text.RegularExpressions;using System.Text;namespace 期末实验 public partial class Form1 : Form public Form1() InitializeComponent(); SqlConnection mSqlConnection; private void Form1_Load(object sender, EventArgs e) private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) private void 打开aToolStripMenuItem_Click(object sender, EventArgs e) if (openFileDialog1.ShowDialog() = DialogResult.OK) string fName = openFileDialog1.FileName; string ext = Path.GetExtension(fName); richTextBox1.Clear(); try if (ext = .txt) richTextBox1.LoadFile(fName,RichTextBoxStreamType.PlainText); /将打开的文件内容值赋给richTextBox1的Text值 else richTextBox1.LoadFile(fName, RichTextBoxStreamType.PlainText); catch (ArgumentException exception) MessageBox.Show(无法识别的文件类型, 打开出错, MessageBoxButtons.OK, MessageBoxIcon.Error); private void richTextBox1_TextChanged(object sender, EventArgs e) public SqlConnection connectionString get; set; private void button1_Click(object sender, EventArgs e) try int a = new int20; string b = new string20; int c = new int20; string d = new string20; string h = new string200; int k = 1; for (int i = 1; i 18; i+) if (i = 1) a1 = richTextBox1.Text.IndexOf(n, 0);/查找从0开始到第一次出现“n”字符的位置。 b1 = richTextBox1.Text.Substring(0, a1);/截取从0开始上面搜索到字符串的位置 else ai = richTextBox1.Text.IndexOf(n, ai - 1 + 1); bi = richTextBox1.Text.Substring(ai - 1 + 1, ai - ai - 1); /将数据分割成单个的行。 for (k = 1; k 0) / MessageBox.Show(插入成功!);/每插入一行数据,执行一次该语句。 catch (Exception ee) MessageBox.Show(请先连接数据库+ee.Message); private void button3_Click(object sender, EventArgs e) mSqlConnection = new SqlConnection(); mSqlConnection.ConnectionString = server=.;DataBase=c#Exam;uid=sa;pwd=920416; mSqlConnection.Open();/打开数据库 if (mSqlConnection.State = ConnectionState.Open) MessageBox.Show(mSqlConnection.State.ToString(); private void button2_Click(object sender, EventArgs e) if (mSqlConnection.State = ConnectionState.Open) mSqlConnection.Close();/关闭数据库 MessageBox.Show(mSqlConnection.State.ToString(); private void button4_Click(object sender, EventArgs e) try mSqlConnection = new SqlConnection(); mSqlConnection.ConnectionString = server=.;DataBase=c#Exam;uid=sa;pwd=920416; SqlCommand mSqlCommand = new SqlCommand(); mSqlCommand.Connection = mSqlConnection; mSqlCommand.CommandType = CommandType.Text; SqlCommand cmd = new SqlCommand(Select * from 数据表结构, mSqlConnection); StreamWriter w = new StreamWriter(论文生成数据Y.txt); mSqlConnection.Open(); SqlDataReader temp = cmd.ExecuteReader(); int f = 0; while (temp.Read() if (f != 2) w.Write(temp0 + t + temp1 + + temp2+ t + temp3 + t + temp4 + t + temp5 + t + temp6 + rn); else if (f=3) w.WriteLine(temp0 + t + temp1 + + temp2 + t + temp3 + t + temp4 + t + temp5 + t + temp6 + n); else w.WriteLine(temp0 + t + temp1 + + temp2 + t + temp3 + t + temp4 + t + temp5 + t + temp6 + rn); f+; /读取并生成文本文件 mSqlConnection.Close(); w.Close(); catch (SqlException hh) MessageBox.Show(hh.Message); private void richTextBox2_TextChanged(object sender, EventArgs e) private void button1_Click_1(object sender, EventArgs e) string a,b; StreamReader read1 = new StreamReader(论文生成数据Y.txt, System.Text.Encoding.GetEncoding(GB2312 ); a=read1.ReadToEnd(); read1.Close(); StreamReader read2 = new StreamReader(C: C#程序设计论文数据Y.txt,System.Text.Encoding.GetEncoding(GB2312 ); b=read2.ReadToEnd(); read2.Close(); if (a = b) MessageBox.Show(相同

温馨提示

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

评论

0/150

提交评论