C#程序设计论文.docx
- 文档编号:29398912
- 上传时间:2023-07-23
- 格式:DOCX
- 页数:15
- 大小:393.18KB
C#程序设计论文.docx
《C#程序设计论文.docx》由会员分享,可在线阅读,更多相关《C#程序设计论文.docx(15页珍藏版)》请在冰豆网上搜索。
C#程序设计论文
C#程序设计论文
每一个成功者都有一个开始。
勇于开始,才能找到成功的路。
目录
第一章要求分析....................................................................................................3
第二章总体分析....................................................................................................3
第三章详细设计....................................................................................................3
第四章具体实验方法............................................................................................4
第五章实验代码....................................................................................................7
第六章总结............................................................................................................12
第一章要求分析
1.编程将一文本文件内容按照指定格式逐行解析并存入指定格式数据库
此要求可以分拆为先将文本文件内容在C#中应用程序中打开,然后对其逐行分割,在指定位置进行分割,最后再连接并存到事先在SQLServer数据库中自己建的表格当中。
但是在这之前当然要确保电脑上有SQLServer数据库,没有的话就要先安装,然后再数据库中建表。
2.编程读取数据库中内容并生成文本文件,使前后两个文本文件内容完全一致
此要求主要涉及数据库的读取。
3.编程比较前后两个文本文件内容
原数据和从数据库中读取的数据进行对比。
第二章总体设计
1.在windowsForm中插入一个menuStrips,一个openFileDialog窗体和一个richTextBox,用于搜索所给的数据并将其显示在richTextBox中。
2.在windowsForm中再增加5个button,分别表示连接数据库、关闭数据库、插入数据、生成文本文件、对比。
3.在每个windows窗体下写上具体的代码,以实现其功能。
达到要求。
第三章详细设计
WindowsForm界面的设计如图所示3-1所示
图3-1windowsForm界面设计
第四章具体实验方法
1.通过openFileDialog打开搜索所给的文本文件
stringfName=openFileDialog1.FileName;
stringext=Path.GetExtension(fName);
richTextBox1.Clear();
然后再通过if判断文件格式,选择导入。
2.连接打开数据库
mSqlConnection=newSqlConnection();
mSqlConnection.ConnectionString"server=.;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(inti=1;i<18;i++)
{
if(i==1)
{
a[1]=richTextBox1.Text.IndexOf('\n',0);
b[1]=richTextBox1.Text.Substring(0,a[1]);
}
else
{
a[i]=richTextBox1.Text.IndexOf('\n',a[i-1]+1);
b[i]=richTextBox1.Text.Substring(a[i-1]+1,a[i]-a[i-1]);
}
for(k=1;k<8;k++)
{
if(k==1)
{
c[1]=b[i].IndexOf('\t',0);
d[1]=b[i].Substring(0,c[1]);
}
elseif(k==2)
{
c[2]=b[i].IndexOf('',c[1]+1);
d[2]=b[i].Substring(c[1]+1,c[2]-c[1]-1);
}
elseif(k==7)
{
d[k]=b[i].Substring(c[k-1]+1,b[i].Length-c[k-1]-1);
}
else
{
c[k]=b[i].IndexOf('\t',c[k-1]+1);
d[k]=b[i].Substring(c[k-1]+1,c[k]-c[k-1]-1);
}
}
5.从数据库中读取数据并生成文本文件时,需要用到sqlcommand及其StreamWriter,要有上面分割的时候用的Substring函数,在分割时连同分割位置的字符一起切掉了,所以在读数据时要再加上相应的字符。
6.比较时用到StreamReader将两个文件文件读入并进行对比。
7.实验结果截图为:
图4-1C#程序设计论文数据原图
图4-2c#程序论文生成数据图
图4-3数据库中所存入的数据截图
第五章实验代码
usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.IO;
usingSystem.Windows.Forms;
usingSystem.Text.RegularExpressions;
usingSystem.Text;
namespace期末实验
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
SqlConnectionmSqlConnection;
privatevoidForm1_Load(objectsender,EventArgse)
{
}
privatevoidmenuStrip1_ItemClicked(objectsender,ToolStripItemClickedEventArgse)
{}
privatevoid打开aToolStripMenuItem_Click(objectsender,EventArgse)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
stringfName=openFileDialog1.FileName;
stringext=Path.GetExtension(fName);
richTextBox1.Clear();
try
{
if(ext==".txt")
{{richTextBox1.LoadFile(fName,RichTextBoxStreamType.PlainText);
//将打开的文件内容值赋给richTextBox1的Text值
}
else
{
richTextBox1.LoadFile(fName,RichTextBoxStreamType.PlainText);
}
}
catch(ArgumentExceptionexception)
{
MessageBox.Show("无法识别的文件类型","打开出错,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
privatevoidrichTextBox1_TextChanged(objectsender,EventArgse)
{}
publicSqlConnectionconnectionString{get;set;}
privatevoidbutton1_Click(objectsender,EventArgse)
{
try
{
int[]a=newint[20];
string[]b=newstring[20];
int[]c=newint[20];
string[]d=newstring[20];
string[]h=newstring[200];
intk=1;
for(inti=1;i<18;i++)
{
if(i==1)
{
a[1]=richTextBox1.Text.IndexOf('\n',0);//查找从0开始到第一次出现“\n”字符的位置。
b[1]=richTextBox1.Text.Substring(0,a[1]);//截取从0开始上面搜索到字符串的位置
}
else
{
a[i]=richTextBox1.Text.IndexOf('\n',a[i-1]+1);
b[i]=richTextBox1.Text.Substring(a[i-1]+1,a[i]-a[i-1]);
}
//将数据分割成单个的行。
for(k=1;k<8;k++)
{
if(k==1)
{
c[1]=b[i].IndexOf('\t',0);
d[1]=b[i].Substring(0,c[1]);
}
elseif(k==2)
{
c[2]=b[i].IndexOf('',c[1]+1);
d[2]=b[i].Substring(c[1]+1,c[2]-c[1]-1);
}
elseif(k==7)
{
d[k]=b[i].Substring(c[k-1]+1,b[i].Length-c[k-1]-1);
}
else
{
c[k]=b[i].IndexOf('\t',c[k-1]+1);
d[k]=b[i].Substring(c[k-1]+1,c[k]-c[k-1]-1);
}
//将每一行的数据在特定位置进行分割,分割成单块的
}
SqlCommandmSqlCommand=newSqlCommand();
mSqlCommand.Connection=mSqlConnection;
mSqlCommand.CommandType=CommandType.Text;
mSqlCommand.CommandText="insertinto数据表结构(数据元素标识符,数据元名称,重复次数,定义,数据源类型,标识格式,数据元允许值)values('"+d[1]+"','"+d[2]+"','"+d[3]+"','"+d[4]+"','"+d[5]+"','"+d[6]+"','"+d[7]+"')";
if(mSqlCommand.ExecuteNonQuery()>0)
{
//MessageBox.Show("插入成功!
");//每插入一行数据,执行一次该语句。
}
}
}
catch(Exceptionee)
{
MessageBox.Show("请先连接数据库"+ee.Message);
}
}
privatevoidbutton3_Click(objectsender,EventArgse)
{
mSqlConnection=newSqlConnection();
mSqlConnection.ConnectionString="server=.;DataBase=c#Exam;uid=sa;pwd=920416";
mSqlConnection.Open();//打开数据库
if(mSqlConnection.State==ConnectionState.Open)
{
MessageBox.Show(mSqlConnection.State.ToString());
}
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
if(mSqlConnection.State==ConnectionState.Open)
{
mSqlConnection.Close();//关闭数据库
MessageBox.Show(mSqlConnection.State.ToString());
}
}
privatevoidbutton4_Click(objectsender,EventArgse)
{
try
{
mSqlConnection=newSqlConnection();
mSqlConnection.ConnectionString="server=.;DataBase=c#Exam;uid=sa;pwd=920416";
SqlCommandmSqlCommand=newSqlCommand();
mSqlCommand.Connection=mSqlConnection;
mSqlCommand.CommandType=CommandType.Text;
SqlCommandcmd=newSqlCommand("Select*from数据表结构",mSqlConnection);
StreamWriterw=newStreamWriter("论文生成数据Y.txt");
mSqlConnection.Open();
SqlDataReadertemp=cmd.ExecuteReader();
intf=0;
while(temp.Read())
{
if(f!
=2)
{
w.Write(temp[0]+"\t"+temp[1]
+""+temp[2]+"\t"+temp[3]+"\t"
+temp[4]+"\t"+temp[5]+"\t"+temp[6]+"\r\n");
}elseif(f==3)
{
w.WriteLine(temp[0]+"\t"+temp[1]+""
+temp[2]+"\t"+temp[3]+"\t"+temp[4]+"\t"+temp[5]+"\t"+temp[6]+"\n");
}
else
{
w.WriteLine(temp[0]+"\t"+temp[1]+""
+temp[2]+"\t"+temp[3]+"\t"+temp[4]+"\t"+temp[5]+"\t"+temp[6]+"\r\n");
}
f++;
}
//读取并生成文本文件
mSqlConnection.Close();
w.Close();
}
catch(SqlExceptionhh)
{
MessageBox.Show(hh.Message);
}
}
privatevoidrichTextBox2_TextChanged(objectsender,EventArgse)
{
}
privatevoidbutton1_Click_1(objectsender,EventArgse)
{
stringa,b;
StreamReaderread1=newStreamReader("论文生成数据Y.txt",System.Text.Encoding.GetEncoding("GB2312"));
a=read1.ReadToEnd();
read1.Close();
StreamReaderread2=newStreamReader("C:
\\C#程序设计论文数据Y.txt",System.Text.Encoding.GetEncoding("GB2312"));
b=read2.ReadToEnd();
read2.Close();
if(a==b)
{
MessageBox.Show("相同!
");
}
else
{
MessageBox.Show("不同!
");
}
}
}
}
第六章总结
1.通过C#代码实验与数据库的连接并存入数据到数据库中,要熟练掌握对数据库的操作。
2.索引的使用方法。
3.用Substring分割数据时,应注意Substring连同索引的字符串一同截取了。
所以读的时候要补上。
4.注意书写代码过程中输入法问题,要一直是英文半角,一旦出现问题是很难发现的。
5.异常处理的熟练掌握也显得特别重要。
6.此次实验主要是考察我们对数据库的操作,以及C#程序设计的掌握情况,windows窗体的应用。
其实MicrosoftVistualStudio是相当智能的,以后还得多多学习。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 程序设计 论文