DEVBUZZ Homepage Accessing a remote SQL Server using SQLClient
 
Web www.devbuzz.com
  HOME PAGE
  All Articles
  Advertise
  Consulting

 Development
  Discuss - Forums
  Still in the box?
  .Compact Framework
  Code Snippets
  SQL Server CE
  Database
  MS Resources
 Stores
  Developer Controls
  Pocket PC Hardware
  Pocket PC Software
  Pocket PC Books
  .NET CF Books
  Book Reviews
  SPB SW Discounts
  RESCO SW Discounts
 DEVBUZZ Info
  About Us
  Help
  Join our email list
  Links & Ratings
  Press & Comments
  Pocket PC version
  Software Reviews
  Hardware Reviews
 Authors
  Authors
  Article Guide
  Competitions
 Resources
  Developers
  Register
  Login

  SPB Discounts!
 Columnists
  Rick Winscot
 Past Blast
  Personal Media Ctr
  Gizmobility
  eVB Legacy
  Old news
  Hosted Software
  Wireless
  Newsletters
  Carl Davis
  Upton Au

 Pocket PC Registry
  Join the registry
  View current list
 Current Poll
Are you converting to .NET Compact Framework?
Yes, it has changed my life!
No, I'm sticking with eVB
.NET CF what's that`?

Current results
3431 votes so far
 Recent Forum Threads [goto forums]

Get Computername
read... (67 hits)


Great aid to development productivity
read... (82 hits)


ThreadingTimer sample code
read... (143 hits)


Multithreading with .NET CF
read... (194 hits)


Moving from eMbedded Visual Basic to Visual Basic .NET
read... (166 hits)


.NET Compact Framework 2.0 Service Pack 2
read... (226 hits)


Transfer Data from SQL Server 2000 to SQL Server Compact Edition
read... (298 hits)


This protocol version is not supported
read... (236 hits)


Converting Lowercase to uppercase wont work
read... (203 hits)


Direct access to MS SQL Server 2000
read... (374 hits)


Creating SDF file in Desktop
read... (513 hits)


Winsock in CF.NET
read... (316 hits)


Using Pocket Outlook to submit HTML page form with MAILTO action
read... (420 hits)


Missing file "System.Data.PocketPC.asmmeta.dll"
read... (268 hits)


HP iPAQ hw6915 Serial Port Issue
read... (309 hits)


Info on the recent forum changes
read... (341 hits)


SqlServer tools from Redgate
read... (383 hits)


Arrow keys and Hardware navigation button
read... (393 hits)


O2 XDA lls pin sync cable to comport
read... (322 hits)


Creating dynamic folders on Pocket PC OS
read... (299 hits)

Custom Windows Mobile software development.
LBS Challenge 2007
LBS Challenge Eight previous NAVTEQ Global LBS Challenge® participants have received venture capital funding and nine past LBS Challenge winners have launched commercial applications on major wireless carriers. Register your non-commercial LBS application in the 2007 NAVTEQ Global LBS Challenge in one of three regions: Americas, Europe-Middle East-Africa (EMEA) or Asia-Pacific(APAC). You could win a share of $2 million in prizes. This could be your year.
Dream. Develop. Win.

Development | .NET Compact Framework

Accessing a remote SQL Server using SQLClient
Written by Pete Vickers  [author's bio]  [read 35926 times]
Edited by Derek

Download the code   .NET Compact Framework   

Page 1  Page 2  Page 3 

Accessing a SQL Server Database using SqlClient

Introduction

The Compact Framework has the ability to access SQL Server 2000 databases from the handheld device. This is a very powerful tool for developers, as it allows real-time access to corporate data, in a secure environment. The handheld can be connected to the network using a wireless Ethernet connection, or using a GPRS connection. This means that corporate data can be made available to authorized employees wherever they can get access. A salesman ‘on the road’ can connect to the office, and get the latest stock figures to ensure he has enough stock to fulfill an order.

This article will show how to connect to a remote database, and retrieve data, using Visual Basic .NET to demonstrate access.

SqlClient - Connecting

Accessing remote SQL Server databases uses the System.Data.SqlClient namespace. To add the references you need, in Visual Studio, click on Project>Add Reference, and add the following references

System.Data.SqlClient
System.Data.Common

In our code, we then have to import the namespaces. As we will be checking that we are connected to a remote system, we will also need the System.Net namespace.

Imports System.Net
Imports System.Data.SqlClient

Ideally, when accessing a remote database, we need to ensure that we have a connection, prior to trying any access. We can do this by checking the IP address of the device. If it returns 127.0.0.1, then it is not connected. If it returns 192.168.55.101 then the device is in the cradle, otherwise the device is connected to a network. In our sample application, we use a function for this:-

Public Function Connected_To_Network() As Boolean
Dim localEndPoint As New IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList(0), 0)
Try
If localEndPoint.Address.ToString = "127.0.0.1" Then
Return False
Else
Return True
End If
Catch ex As Exception
Return False
End Try
End Function

You also have to define a connection. This will be declared as a ‘form’ variable.

Private sqlConn As SqlConnection

There will be a button on the form, which will serve the dual purpose of connecting the application to the database, and closing the database. When trying to open the database, we will first ensure we have a connection, and if so, we will set up the connection string, and try to connect to the database.

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
If Not Connected_To_Network() Then
MessageBox.Show("You are not connected to the network!", "No Connection")
Exit Sub
End If

Once we have confirmed we have a connection, we can now set up the connection string. A typical connection string is
"Persist Security Info=False;Integrated Security=False;Server=delldesktop,39250;initial catalog=Pubs;user id=sa;password=;"

Table1. Connection string options and descriptions

Name

Description

Persist Security Info

When set to false or no (strongly recommended), security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open state. Resetting the connection string resets all connection string values including the password. Recognized values are true, false, yes, and no.

Integrated Security
-or-Trusted_Connection

When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.

Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.

Data Source
-or-
Server
-or-

Address
-or-
Addr
-or-
Network Address

The name or network address of the instance of SQL Server to which to connect. In the above example, the 39250 is the port SQL Server is using. The standard port is 1433, but due to security, sites tend to use a different port.

Initial Catalog
-or-
Database

The name of the database.

User ID

The SQL Server login account (Not recommended. To maintain the highest level of security, it is strongly recommended that you use the Integrated Security or Trusted_Connection keyword instead.).

Password
-or-
Pwd

The password for the SQL Server account logging on (Not recommended. To maintain the highest level of security, it is strongly recommended that you use the Integrated Security or Trusted_Connection keyword instead.).

Once we have set up the connection string, we can try to connect.

Try
sqlConn = New SqlConnection
sqlConn.ConnectionString = "Persist Security Info=False;Integrated Security=False;Server=delldesktop,39250;initial catalog=Pubs;user id=sa;password=;"
sqlConn.Open()
btnOpen.Text = "Close 'Pubs'"
pnlFunctions.Visible = True
Catch Ex As SqlClient.SqlException
DisplaySQLErrors(ex, "Open", True)
End Try

Next Page 

Back to .NET Compact Framework | [Article Index]

 

Back to the top of the page.
Franson GPS tools for the Pocket PC Chris De Herrera's Windows CE Website Windows CE News & Information Source RESCO Software discounts
Copyright ©2000-2008 by DEVBUZZ.COM, Inc., TX. USA.