Help your classmates with a course design WinForm connection sqlserver2005
Specific methods:
. Add App. Config file
2. Adding nodes in the App. config file
<?XML version= "1.0" encoding= "Utf-8"?><Configuration><appSettings> <AddKey= "ConnString"value= "Data source=libl;initial CATALOG=HOVERTREESCJ; User Id=sa; Password=sa "></Add></appSettings></Configuration>
HOVERTREESCJ the name of the database
3. Add references in Project reference System.Configuration
Add reference using System.Configuration to the file;
Using System.Data.SqlClient;
4. Get the connection string in app. Config
public string connstring = system.configuration.configurationmanager.appsettings["ConnString"];
5. Connect to the database and read the data
SqlConnection conn =NewSqlConnection (connstring); stringstrSQL ="SELECT * from Basetb"; SqlCommand cmd=NewSqlCommand (); Cmd.commandtext=strSQL; Cmd. Connection=Conn; Conn. Open (); SqlDataReader Dr=cmd. ExecuteReader (); if(Dr. HasRows) { while(Dr. Read ()) { This. TextBox1.Text = dr[0]. ToString (); }} conn. Close ();/*Hovertree.top*/
Recommendation: http://www.cnblogs.com/roucheng/p/DataGridView.html
WinForm Connection Database (sqlserver2005)