1

I`m creating a website using asp.net, and I need to use a local SQL server (using Microsoft SQL server). And I have created database and tables in it using the MS SQL Server Management Studio.

Now I successfully connect to the database and do some simple add/query using the following commands:

string connectionString = "data source=ABCD\\SQLEXPRESS;initial catalog=PMD;Trusted_Connection=yes;"; string sqlQuery = "INSERT INTO PMD (username, userID, userAddress)"; sqlQuery += " VALUES (@user, id, add)"; SqlConnection dataConnection = new SqlConnection(connectionString); SqlCommand dataCommand = new SqlCommand(sqlQuery, dataConnection); dataCommand.Parameters.AddWithValue("user", USER.Value); dataCommand.Parameters.AddWithValue("id", ID.Value); dataCommand.Parameters.AddWithValue("add", ADDRESS.Text); dataConnection.Open(); dataCommand.ExecuteNonQuery(); dataConnection.Close(); 

The command above can add one column to the table, with values stated. The query is done in a similar way. Compared with Linq, this is not very concise.

So I was wondering how can I change the code so I can use Linq.

The biggest question for me now is how to connect to the base. I already know all the syntax of Linq.

eg: var query=from c in db.username where c.Contain(“Micheal”) select c (or maybe db.PMD.username) 

How can I get the db to link with ABCD/SQLEXPRESS, table PMD?

3
  • are you using entity framework or linq to sql? Commented Oct 16, 2012 at 15:27
  • @Daniel I dont know what Im using right now. I searched "how to connect to sql, asp.net" and find the code and it`s working. Now I would like to change it so I can use linq to sql. Commented Oct 16, 2012 at 15:30
  • @DanielA.White OP is using neither. The above code is basic ADO.NET. Commented Oct 16, 2012 at 15:31

2 Answers 2

1

First you need an Object/Relational Mapper (O/RM). You can't just put LINQ on top of your old ADO.NET code.

Microsoft provides two: Linq2SQL and Entity Framework.

Linq2SQL has been discontinued. If I had to choose between the two, I'd go with Entity Framework.

Here you can find an introduction: http://www.asp.net/entity-framework

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Dennis, Thanks for the reply. So I cannot just add some code to it and use Linq to SQL?
No, you can't. You need a LINQ provider that translates LINQ queries to SQL. This is where Entity Framework comes in.
@user1739292, well, setting up an Entity Framework context from a database takes all of 5 minutes, so don't think it will take forever. It's pretty quick.
thanks for the above reply. I`m using Webmatrix to develop the website, not VS. so it is the same process?
Have a look at the first answer to this question: stackoverflow.com/questions/10626492/…
|
0

For example, install Entity Framework, then connect to sql server with entity framework

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.