About getting data from the database into the background, there are 4 steps: 1. Gets the string that connects to the database; 2. Create a SqlConnection object to open the database; 3. Create a SqlCommand object to perform an operation, set its properties, 4. Close the database connection. Complete this four-step process to get the data in the database.
//The first step stringConnStr ="Data Source=.;i Nitial Catalog=mydata; User Id=sa; password=123456"; //Step TwoSqlConnection conn =NewSqlConnection (CONNSTR); Conn. Open (); //Step threeSqlCommand cmd =NewSqlCommand (); Cmd. Connection=Conn; Cmd.commandtype=CommandType.Text; stringSQLText ="SELECT * from UserInfo"; Cmd.commandtext=SQLText; SqlDataReader Dr=cmd. ExecuteReader (); while(Dr. Read ()) {Txtbox1.text= dr["UserName"]. ToString (); } Dr. Close (); //Fourth StepConn. Close ();
By grasping this step, you can complete the operation of the database.
C # Background Get database data--ado.net