资源描述:
《C#窗体连接数据库》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、publicstaticStringstrConn="DataSource=.\Sqlexpress;Database=mydatabase;integratedsecurity=SSPI";publicstaticSqlConnectionconn=newSqlConnection(strConn);usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem
2、.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespaceWindowsFormsApplication1{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){StringstrConn="DataSource=.\Sqlexpress;Database=
3、mydatabase;integratedsecurity=SSPI";SqlConnectionconn=newSqlConnection(strConn);try{DBClass.conn.Open();DataSetdsmydatabase=newDataSet();SqlDataAdapterdabaseinfrom=newSqlDataAdapter("select*fromclientinfrom",DBClass.conn);dabaseinfrom.Fill(dsmydatabase,"baseinfrom");dataG
4、ridView1.DataSource=dsmydatabase.Tables["baseinfrom"];DBClass.conn.Close();}catch(Exceptionex){MessageBox.Show(ex.Message.ToString()+"打开数据库失败");}}privatevoidbutton5_Click(objectsender,EventArgse){StringstrConn="DataSource=.\Sqlexpress;Database=mydatabase;integratedsecuri
5、ty=SSPI";SqlConnectionconn=newSqlConnection(strConn);Application.Exit();DBClass.conn.Close();}privatevoidbutton6_Click(objectsender,EventArgse){StringstrConn="DataSource=.\Sqlexpress;Database=mydatabase;integratedsecurity=SSPI";SqlConnectionconn=newSqlConnection(strConn)
6、;try{//inti1,i2;DBClass.conn.Open();SqlCommandcmd=newSqlCommand();cmd.Connection=DBClass.conn;cmd.CommandText="insertclientinfrom(airlineno,name,sex,age,certificatetype,"+"certificateno,seatno)values('"+textBox1.Text+"','"+textBox2.Text+"','"+comboBox1.Text+"','"+int.Pars
7、e(textBox3.Text)+"','"+comboBox2.Text+"','"+textBox4.Text+"','"+textBox5.Text+"')";cmd.ExecuteNonQuery();DataSetdsmydatabase=newDataSet();SqlDataAdapterdabaseinfrom=newSqlDataAdapter("select*fromclientinfrom",DBClass.conn);dabaseinfrom.Fill(dsmydatabase,"baseinfrom");data
8、GridView1.DataSource=dsmydatabase.Tables["baseinfrom"];MessageBox.Show("恭喜您您已经成功添加记录");DBClass.c