View Single Post
  #4 (permalink)  
Old April 24th, 2015, 11:30 AM
svb svb is offline
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Depending on your database, you may want to change the type of connection. The example below is to insert into an access database and retrieve value from the same database and display into a text box. You may want to change it to SqlConnection or SqlCommand if you are using SQL server.

Code:
using(OledbConnection con= new OledbConnection(connectionString)
{
   con.Open();
   string sql="Enter your sql query here";
   using(OledbCommand cmd= new OledbCommand(sql, con);
   {
// Use only one of the below in one using block.
      cmd.ExecuteNonQuery(); // For inserting into DB.
      txtbox1.Text= Convert.ToString(cmd.ExecuteScalar()); // For returning value to text box.
   }
}
Reply With Quote