资源描述:
《微机接口与原理期末整理》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、C#中连接数据库的添加查找方法usingSystem.Data.SqlClient;namespaceWindowsApplication3{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse)//添加按钮{SqlConnectionconn=newSqlConnection("server=.;database=st;Integrate
2、dSecurity=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();//打开连接会
3、出现异常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)//查找按钮{Sql
4、Connectionconn=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
5、();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#记事本部分代码na
6、mespace_2{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidntsmenu_Click(objectsender,EventArgse){richTextBox1.Clear();//新建}privatevoidopsmenu_Click(objectsender,EventArgse){if(openFileDialog1.ShowDialog()==DialogResult.OK&&openFil
7、eDialog1.FileName!=""){richTextBox1.LoadFile(openFileDialog1.FileName,RichTextBoxStreamType.PlainText);//打开}}privatevoidstsmenu_Click(objectsender,EventArgse){if(saveFileDialog1.ShowDialog()==DialogResult.OK&&saveFileDialog1.FileName!=""){richTextBox1.SaveFil
8、e(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);//保存}}privatevoidatsmennu_Click(objectsender,EventArgse){if(saveFileDialog1.ShowDialog()==DialogResult.OK&&saveFileDialog1.File