Get started with LINQ

Thursday, 19 June 2008 07:35 by Nick

This walkthrough explains how to connect to your SQL Server database and read from it using LINQ. 

LINQ is Microsoft's latest solution to the problem of manipulating data from within .NET 3.5. If you are using an application to generate a Data-Access Layer for you, or you are implementing your own database calls using ADO.NET, you can now use LINQ to do those things for you, very quickly and efficiently and without any third-party components. It's fast, and it's pre-installed in Visual Studio 2008. Here's what you do to use LINQ in Visual Studio 2008:

Problem

You want to read something from your SQL Server database using LINQ. Make sure you have Visual Studio 2008 and a populated SQL Server database ready.

Solution

> Create a connection to the database:

 1. Start Visual Studio 2008.

 2. Start a new Web application (or a project type of your choice).

 3. From the menu select View > Server Explorer (Ctrl+Alt+S)

 4. In the Server Explorer pane, Right-Click Data Connections and select Add Connection. 

 5. In the 'Choose Data Source' dialog, choose 'SQL Server' and press Continue.

 6. In the 'Add connection' dialog, enter your SQL Server name and login credentials, and then choose a database and press OK.

> Tell LINQ about the database:

 7. From the menu select View > Add New Item (Ctrl+Shift+A)

 8. From the list of templates, choose "LINQ To SQL Classes" and add a new instance (it's in the Data cateogry if you can't see it!)

 9. In Solution Explorer, you'll now have a strange new file called DataClasses1.dbml and you'll see a big empty window as it opens.

 11. Go back to the Server Explorer and pop open the new data connection you made in step 6. Open the "tables" node and then select every table (shift + mouse click).

 12. Drag the whole lot onto the big empty tab called DataClasses1.dbml (which should still be open).

 13. You'll see the Database schema appear in the window. Save it!

> Read something from the database using LINQ:

Your database is now visible to intellisense, via the DataClasses1DataContext object. and you are now ready to write your LINQ code which will read from the database!  

14. Now to code some LINQ. In this example, I have used a table called tbCustomers, so you need to substitute your own table name. The references to AccountBalance and FirstName are fields in the tbCustomers table. 

> Open default.aspx.vb and paste this into your Page_Load() event: 

Dim db As New DataClasses1DataContext

Dim custs = From c In db.tbCustomers _            
Where c.AccountBalance > 0 _                         
Select c

For Each cust In custs

   Response.Write(cust.FirstName)

Next

Run the project and you should see your data being written to the page.

 

Currently rated 4.0 by 2 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   LINQ | Windows
Actions:   E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Remove whitespace in Word Documents

Thursday, 12 June 2008 02:53 by Jeff

Problem

When cutting and pasting screenshots into Microsoft Word 2003 from a telnet terminal emulator I am left with whitespace at the end of each line.

Solution

Using find and replace set find to ^w^p and replace to ^p.

Then click replace all.

This works in Word 2003, 2008 and probably 97, 2000 and XP.

Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   , , , ,
Categories:   Office
Actions:   E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Windows XP Slow Boot Time Due to svchost.exe using 100% CPU

Monday, 2 June 2008 20:43 by Jeff

Problem

Boot time of Windows XP Pro was up to 11-13 minutes.

Solution 

To help track down what was causing this, download Process Explorer from http://technet.microsoft.com/en-gb/sysinternals/default.aspx and put the .exe in the Windows Startup directory. SVCHOST.exe in conjunction with WUAUCLT.exe used 100% CPU for about 5 minutes of my boot time.

The were many suggestions on how to resolve this but the one that worked was eventually found on page 9 of this forum post http://episteme.arstechnica.com/eve/forums/a/tpc/f/12009443/m/786004271831/p/1

Follow the steps below :

1. Click Start->Run, type "services.msc" (without quotation marks) in the open box and click OK.
2. Double click the service "Automatic Updates".
3.Click on the Log On tab, please ensure the option "Local Systemaccount" is selected and the option "Allow service to interact withdesktop" is unchecked.

4. Check if this service has been enabled on the listed Hardware Profile. If not, please click the Enable button to enable it.
5.Click on the tab "General "; make sure the "Startup Type" is"Automatic". Then please click the button "Start" under "ServiceStatus" to start the service.
6. Repeat the above steps with the other service: Background Intelligent Transfer Service (BITS)

Re-register Windows Update components and Clear the corrupted Windows Update temp folder
================================

1. Click on Start and then click Run,
2. In the open field type "REGSVR32 WUAPI.DLL" (without quotation marks) and press Enter.
3. When you receive the "DllRegisterServer in WUAPI.DLL succeeded" message, click OK.
4. Please repeat these steps for each of the following commands:

REGSVR32 WUAUENG.DLL
REGSVR32 WUAUENG1.DLL
REGSVR32 ATL.DLL
REGSVR32 WUCLTUI.DLL
REGSVR32 WUPS.DLL
REGSVR32 WUPS2.DLL
REGSVR32 WUWEB.DLL

Afterthe above steps are finished. Since temporary folder of Windows Updatemay be corrupted. We can refer to the following steps to rename thisfolder that

1. Click Start, Run, type: cmd and press Enter. Please run the following command in the opened window.

net stop WuAuServ

(note, you might need to reboot before the net stop command will work)

2. Click Start, Run, type: %windir% and press Enter.
3. In the opened folder, rename the folder SoftwareDistribution to SDold.
4. Click Start, Run, type: cmd and press Enter. Please run the following command in the opened window.

net start WuAuServ

 

As suggested by the guy who posted the instructions, I also believe that getting xp to rebuild the SoftwareDistribution directory is a big part of this.

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   , , ,
Categories:   Windows
Actions:   E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed