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?

Malachi
  • 3,205
  • 4
  • 29
  • 46

2 Answers2

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

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • Hi Dennis, Thanks for the reply. So I cannot just add some code to it and use Linq to SQL? – user1739292 Oct 16 '12 at 15:36
  • No, you can't. You need a LINQ provider that translates LINQ queries to SQL. This is where Entity Framework comes in. – Dennis Traub Oct 16 '12 at 15:37
  • @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. – Gromer Oct 16 '12 at 15:37
  • thanks for the above reply. I`m using Webmatrix to develop the website, not VS. so it is the same process? – user1739292 Oct 16 '12 at 15:43
  • Have a look at the first answer to this question: http://stackoverflow.com/questions/10626492/linq-to-sql-for-webmatrix-web-pages – Dennis Traub Oct 16 '12 at 15:47
  • @DennisTraub Thanks, I have a feeling it would be very useful. I don`t have enough points to "up" though, sorry about that, haha. – user1739292 Oct 16 '12 at 16:13
  • @user1739292 You can accept the answer by using the checkmark. And welcome to stackoverflow. – Dennis Traub Oct 16 '12 at 16:19
0

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

Community
  • 1
  • 1
Gábor Plesz
  • 1,203
  • 1
  • 17
  • 28