微机接口与原理期末整理.docx
- 文档编号:25409933
- 上传时间:2023-06-08
- 格式:DOCX
- 页数:69
- 大小:49.76KB
微机接口与原理期末整理.docx
《微机接口与原理期末整理.docx》由会员分享,可在线阅读,更多相关《微机接口与原理期末整理.docx(69页珍藏版)》请在冰豆网上搜索。
微机接口与原理期末整理
C#中连接数据库的添加查找方法
usingSystem.Data.SqlClient;
namespaceWindowsApplication3
{publicpartialclassForm1:
Form
{publicForm1()
{InitializeComponent();}
privatevoidbutton1_Click(objectsender,EventArgse)//添加按钮
{SqlConnectionconn=newSqlConnection("server=.;database=st;IntegratedSecurity=SSPI");//连接对象st是表的名称
intSex=1;
if(radioButton2.Checked)
Sex=0;
StringsqlString=String.Format("insertintoStvalues('{0}','{1}',{2},'{3}')",textBox1.Text,textBox2.Text,Sex,textBox3.Text);
SqlCommandcmd=newSqlCommand(sqlString,conn);//
try
{conn.Open();//打开连接会出现异常
introw=cmd.ExecuteNonQuery();
if(row==0)
MessageBox.Show("success");
elseMessageBox.Show("fail");
}
catch(Exceptionex)
{MessageBox.Show(ex.Message.ToString());//捕捉异常}
finally
{conn.Close();//只要出现异常就关闭连接}
}
privatevoidbutton2_Click(objectsender,EventArgse)//查找按钮
{SqlConnectionconn=newSqlConnection("server=.;database=st;IntegratedSecurity=SSPI");
stringsqlString="select*fromstwherestu='"+textBox1.Text+"'";
SqlCommandcmd2=newSqlCommand(sqlString,conn);
conn.Open();
SqlDataReadersdr=cmd2.ExecuteReader();
if(sdr.HasRows)
{sdr.Read();
textBox2.Text=sdr["Name"].ToString();
textBox3.Text=sdr["birthday"].ToString();
if(bool.Parse(sdr["Sex"].ToString()))//转换成布尔类型判断
radioButton1.Checked=true;
elseradioButton2.Checked=true;
//sdr.Close();}
elseMessageBox.Show("没有查询到该学生");
conn.Close();
}
}
}
C#记事本部分代码
namespace_2
{publicpartialclassForm1:
Form
{publicForm1()
{InitializeComponent();}
privatevoidntsmenu_Click(objectsender,EventArgse)
{richTextBox1.Clear();//新建}
privatevoidopsmenu_Click(objectsender,EventArgse)
{if(openFileDialog1.ShowDialog()==DialogResult.OK&&openFileDialog1.FileName!
="")
{richTextBox1.LoadFile(openFileDialog1.FileName,RichTextBoxStreamType.PlainText);//打开}
}
privatevoidstsmenu_Click(objectsender,EventArgse)
{if(saveFileDialog1.ShowDialog()==DialogResult.OK&&saveFileDialog1.FileName!
="")
{richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);//保存}
}
privatevoidatsmennu_Click(objectsender,EventArgse)
{if(saveFileDialog1.ShowDialog()==DialogResult.OK&&saveFileDialog1.FileName!
="")
{richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);//另保存}
}
privatevoidxtsmenu_Click(objectsender,EventArgse)
{this.Close();//退出}
privatevoidutsmenu_Click(objectsender,EventArgse)
{//撤销richTextBox1.Undo();}
privatevoidrtsmenu_Click(objectsender,EventArgse)
{//重复}
privatevoidttsmenu_Click(objectsender,EventArgse)
{//剪切richTextBox1.Cut();}
privatevoidctsmenu_Click(objectsender,EventArgse)
{richTextBox1.Copy();//复制}
privatevoidptsmitem_Click(objectsender,EventArgse)
{////粘帖richTextBox1.Paste();}
privatevoidatsmenu_Click(objectsender,EventArgse)
{//全选richTextBox1.SelectAll();}
privatevoidtsmbady_Click(objectsender,EventArgse)
{//字体设置
fontDialog1.ShowColor=true;
if(fontDialog1.ShowDialog()==DialogResult.OK){
richTextBox1.Font=fontDialog1.Font;
richTextBox1.ForeColor=fontDialog1.Color;
}}
privatevoidtsmauto_Click(objectsender,EventArgse)
{//自动换行richTextBox1.WordWrap=false;}
privatevoidtime_Click(objectsender,EventArgse)
{richTextBox1.AppendText(DateTime.Now.ToLocalTime().ToString());//显示日期时间
richTextBox1.AppendText(DateTime.Now.DayOfWeek.ToString());//显示星期}
}
}
教务管理系统
namespace管理系统
{classProgram
{staticvoidMain(string[]args)
{Hashtableht=newHashtable();
stringkey;stringval;
Console.WriteLine("欢迎使用2009级学生管理系统");
Console.WriteLine("============================================");
Console.WriteLine("请选择如下操作:
");
Console.WriteLine("A;添加B:
查询C:
删除D:
修改E:
退出系统");
Console.WriteLine("********************************************");
stringr=Console.ReadLine();
while(r!
="e")
{switch(r)
{case"a":
Console.WriteLine("请输入学生学号:
");
key=Console.ReadLine();Console.Write("请输入学生姓名:
");
val=Console.ReadLine();ht.Add(key,val);break;
case"b":
Console.WriteLine("请输入要查询的学号:
");
key=Console.ReadLine();
if(ht.Contains(key))
Console.WriteLine("学生姓名为:
{0}",ht[key]);
elseConsole.WriteLine("您输入的学生学号不存在");
break;
case"c":
Console.WriteLine("请输入要删除的学号:
");
key=Console.ReadLine();ht.Remove(key);
break;
case"d":
Console.WriteLine("请输入要修改的学号:
");
key=Console.ReadLine();
Console.WriteLine("请输入修改后学生姓名:
");
ht[key]=Console.ReadLine();
break;}
Console.WriteLine("请选择如下操作:
");
Console.WriteLine("A;添加B:
查询C:
删除D:
修改E:
退出系统");
Console.WriteLine("********************************************");
r=Console.ReadLine();
}}}}
分割连接
namespaceSplitJoinExample
{///
///此示例演示使用Split()和Join()方法分割和连接字符串
///
classProgram
{staticvoidMain(string[]args)
{stringinputString;//输入的字符串
string[]splitStrings;//分割后的字符串数组
stringjoinString;//连接后的新字符串
//输入提示
Console.WriteLine("请输入一串字符串,用空格分隔单词:
");
inputString=Console.ReadLine();//接收输入
splitStrings=inputString.Split('');//以空格作为分隔符分割字符串
//将分割后的字符串输出
Console.WriteLine("\n分割后的字符串为:
");
foreach(stringsinsplitStrings)
{Console.WriteLine(s);}
//将分割后的字符串使用下划线连接在一起
joinString=string.Join("_",splitStrings);
//将连接后的字符串输出
Console.WriteLine("\n连接后的新字符串为:
{0}",joinString);
Console.ReadLine();}}}
BinarySerch方法反回其中的元素5的下标
//给定8个数,存在数组中,利用Array类的BinarySerch方法反回其中的元素5的下标
namespaceConsoleApplication5
{classProgram
{staticvoidMain(string[]args)
{int[]myArr={8,7,6,5,4,3,2,1};
Array.Sort(myArr);
inttarget=5;
intresult=Array.BinarySearch(myArr,target);
Console.WriteLine("{0}的下标为{1}",target,result);
Console.ReadLine();}}}
sort方法排序
//给定8个数,将这些数存在数组中,利用Array类的Sort方法按从小到达顺序输出
namespaceConsoleApplication4
{classProgram
{staticvoidMain(string[]args)
{int[]array=newint[]{8,7,6,5,4,3,2,1};
Console.WriteLine("原始数组:
");
for(inti=0;i Console.Write("{0}->",array[i]); Console.WriteLine();//换行 Array.Sort(array); Console.WriteLine("排序后的数组: "); for(inti=0;i Console.Write("{0}->",array[i]);}}} 冒泡排序法升序排列 //给定数组: ,要求用冒泡排序法将其升序排列 namespaceConsoleApplication7 {classProgram {staticvoidMain(string[]args) {int[]SortArray=newint[]{78,89,90,56,79,345,217,5,13,88}; Console.WriteLine("待排序序列: "); for(inti=0;i Console.Write("{0}",SortArray[i]); Console.WriteLine(); for(inti=SortArray.Length-1;i>=0;i--) for(intj=0;j {if(SortArray[j]>SortArray[j+1]) {inttemp=SortArray[j]; SortArray[j]=SortArray[j+1]; SortArray[j+1]=temp;}} Console.WriteLine("排序后序列: "); for(inti=0;i Console.Write("{0}",SortArray[i]);}}} 排序 //给定8个数: 87654321将这些数存在数组中,按从小到大输出 namespaceConsoleApplication3 {classProgram {staticvoidMain(string[]args) {inti,j,temp,m; int[]array=newint[]{8,7,6,5,4,3,2,1}; for(j=0;j {temp=j; for(j=0;j {if(array[i] temp=i;} if(temp! =j) {m=array[j]; array[j]=array[temp]; array[temp]=m;} } Console.WriteLine(""); for(j=0;j Console.WriteLine("{0}",array[j]); }}} 判断数组中是否含赵六 namespaceConsoleApplication6 {classProgram {staticvoidMain(string[]args) {string[]arrSname={"大宝","张三","李四","赵六","赵二","麻子"}; //判断是否含某值 stringtarget="赵六"; boolresult=((System.Collections.IList)arrSname).Contains(target); Console.WriteLine("包含{0}? {1}",target,result);//true}}} time类的构造函数及其重载 namespaceConsoleApplication13 {classTime {publicintnHour,nMinute,nSecond; publicTime()//构造函数 {nHour=nMinute=nSecond=0;} publicTime(intHour) {nHour=Hour; nMinute=nSecond=0;} publicTime(intHour,intMinute)//重载2 {nHour=Hour; nMinute=Minute; nSecond=0;} publicTime(intHour,intMinute,intSecond)//重载3 {nHour=Hour; nMinute=Minute; nSecond=Second;}} classText//程序入口 {staticvoidMain() {Timet1,t2,t3,t4; t1=newTime(); t2=newTime(10); t3=newTime(10,20); t4=newTime(10,20,30); Console.WriteLine("t1时间为: {0}时{1}分钟{2}秒",t1.nHour,t1.nMinute,t1.nSecond); Console.WriteLine("t2时间为: {0}时{1}分钟{2}秒",t2.nHour,t2.nMinute,t2.nSecond); Console.WriteLine("t3时间为: {0}时{1}分钟{2}秒",t3.nHour,t3.nMinute,t3.nSecond); Console.WriteLine("t4时间为: {0}时{1}分钟{2}秒",t4.nHour,t4.nMinute,t4.nSecond);}}} 创建类 namespaceConsoleApplication11 {//学生类 publicclassstudent {publicstringstrName; publicintnAge; publicvoidSetAge(intage) {this.nAge=age; } publicintGetAge() {returnthis.nAge; } } classProgram {staticvoidMain(string[]args) {students=newstudent(); s.SetAge(20);//赋值 Console.WriteLine(s.GetAge());//获取 }}} 方法重载 namespaceConsoleApplication1 {publicclassStudent {publicstringstrName; publicintnAge; //长了一岁 publicvoidGrow() {this.nAge++; } //长age岁 publicvoidGrow(intage) {this.nAge+=age; } classProgram {staticvoidMain(string[]args) {Students=newStudent(); s.strName="张三"; s.nAge=20; s.Grow(); Console.WriteLine(s.nAge); s.Grow (2); Console.WriteLine(s.nAge); }}} 类的继承 namespaceConsoleApplication12 {//学生类 publicclassStudent {publicstringstrName; publicintnAge; } publicclassCollegeStudent: Student {publicstringxi; } publicclassProgram {staticvoidMain(string[]args) {Students=newStudent(); s.strName="小明"; s.nAge=18; Console.WriteLine("姓名: {0},年龄: {1}",s.strName,s.nAge); CollegeStudentc=newCollegeStudent(); c.strName="孙立娟"; c.nAge=22; c.xi="信息学院"; Console.WriteLine("姓名: {0},年龄: {1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 微机 接口 原理 期末 整理