Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2012/5.0 > C# 2012/5.0 General Discussion
| Search | Today's Posts | Mark Forums Read
C# 2012/5.0 General Discussion Discussions about the C# 2012 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2012/5.0 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2014, 08:30 PM
Authorized User
Points: 132, Level: 2
Points: 132, Level: 2 Points: 132, Level: 2 Points: 132, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Question How do you insert a record into an SQL database using LINQ

Hi,

I looked all over but, I can't find any resources that tells me how to insert a record into a SQL Database using LINQ syntax. Could someone please help me?

Thanks,
Truck35
 
Old June 21st, 2014, 01:16 AM
Wrox Author
Points: 1,163, Level: 13
Points: 1,163, Level: 13 Points: 1,163, Level: 13 Points: 1,163, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2010
Location: Austria
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

The LINQ syntax is a query syntax. Use Entity Framework or other technologies for inserting records.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old June 21st, 2014, 02:41 PM
Authorized User
Points: 132, Level: 2
Points: 132, Level: 2 Points: 132, Level: 2 Points: 132, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Question Need Code to insert record into SQL Database

What is the syntax used to extract data from a relational database and display it into a text box using c#. I already know how to establish a connection with a database but, I want to take the data and display it into a textbox. How do you do that?

If anyone could help me, I would appreciate it.

Thanks,
Truck35
 
Old April 24th, 2015, 10: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.
   }
}




Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't insert a DateTime into my database with Linq to sql chobo2 ASP.NET 3.5 Basics 3 August 15th, 2009 05:55 PM
Insert data using LINQ SQL - page 471 LoriBarber BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 0 March 1st, 2009 08:12 PM
errro trying to insert record to sql server method ASP.NET 1.0 and 1.1 Basics 1 May 20th, 2005 08:24 PM
Insert a record into a Database ersp Classic ASP Basics 3 May 9th, 2004 08:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.