4

I would like to connect to SQL Server from windows mobile application. I tried to do that but it raises a SQL exception when the code tries to open the connection. I googled for this issue and I found article make some configuration changes at SQL Server and I followed it and it raise the same exception

: http://netcf2.blogspot.com/2005/12/accessing-sql-server-express-from.html

Tools :

  • SQL Server Management Studio 2008 R2
  • Windows Mobile 6.0
  • VS 2008
  • C# project and SQL server at the same machine and I used the same code to connect to SQL Server from desktop application and it was succeeded .

Code :

SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=DBNAME;Trusted_Connection=yes;");
conn.Open();

Your reply will be highly appreciated

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mohammed Thabet
  • 21,387
  • 7
  • 27
  • 43
  • Are you sure you want to directly access an SQL Server over the internet without any encryption and expose your credentials? This is usually done using a web service offering access to a limited set of operations. – CodeZombie Aug 16 '12 at 22:30
  • I'm pretty sure you have to indicate in the connection string where the server actually is - an IP address is common. A '.' isn't going to be enough. – ctacke Aug 17 '12 at 00:00
  • use server ip instead of (.). – SMK Aug 17 '12 at 05:22
  • The server IP is the local machine so I used 127.0.0.1 instead of (.) but I still have the same sql exception . I am sure that the problem not in connection string ,the problem at SQL server configurations . I test the application using emulator at the same machine of SQL server – Mohammed Thabet Aug 17 '12 at 11:18
  • what is the exception, and did you added a rule in firewall to allow sql to accept external connections? – Erdogan Kurtur Aug 31 '12 at 08:21

4 Answers4

2

Your connection string is most likely the source of the error:

Data Source=.;Initial Catalog=DBNAME;Trusted_Connection=yes;
           ****

This . (dot) means: you're trying to connect to a full-blown SQL Server instance on that very device! I highly doubt you've installed SQL Server onto your Windows Mobile device.....

Check out ConnectionStrings.com for a huge number of connection string samples and explanations. Basically, your connection string should most likely be something like:

Data Source=YourServerNameHere;Initial Catalog=DBNAME;Trusted_Connection=yes;

Put in the name (or IP address) of your machine where the SQL Server actually resides - it's definitely not on your mobile device....

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Thanks marc_s , I used the server IP for my local machine so I used 127.0.0.1 instead of (.) but I still have the same sql exception . I am sure that the problem not in connection string ,the problem at SQL server configurations . I test the application using emulator at the same machine of SQL server – Mohammed Thabet Aug 17 '12 at 11:18
  • @thabet084: if this answer helped you solve you problem, you should [**accept this answer**](http://meta.stackexchange.com/q/5234/153998). This will show your appreciation for the people who *spent their own time to help you*, and it will improve your chance of getting more help in the future, too! – marc_s Aug 17 '12 at 11:19
  • @thabet084: you should use the **actual** IP address of your machine - not `127.0.0.1`. Using that, the emulator will *again* try to find SQL Server **on the device** where it's definitely **not** located... – marc_s Aug 17 '12 at 11:20
  • The machine is offline so I have not IP address and I used 127.0.0.1 . I have not device I used emulator – Mohammed Thabet Aug 17 '12 at 11:24
  • 2
    Then use the **machine name** - 127.0.0.1 is the **local loopback** - it points back to the device (or emulator) that you're running on. It does **NOT** refer to the "hosting" machine where SQL Server is installed. – marc_s Aug 17 '12 at 11:30
  • I used machine name and still the same exception :(:( – Mohammed Thabet Aug 17 '12 at 11:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15461/discussion-between-thabet084-and-marc-s) – Mohammed Thabet Aug 17 '12 at 11:38
0

I have also the same problem.

I used this connection string

Data Source=My-PC;Initial Catalog=TestDB;Persist Security Info=True;User ID=Admin;Password=*****";

then I tried its IP

"Data Source=10.219.47.79;Initial Catalog=TestDB;Persist Security Info=True;User ID=Admin;Password=*****";

is that what you mean by IP? as I think we can run mobile applications on the same machine which the SQL server resides

WaelKamash
  • 11
  • 4
0

Ensure your mobile device (emulator or physical device) can physically reach the remote PC that your SQL Server is running on.

If your SQL Server is running on a PC named "AppServer", for example, then go into Mobile File Explorer

Start Screen

From File Explorer, go to the Menu button at the bottom and select Open Path

File Explorer

From here, enter the path to your SQL Server

Network Path

If you can get to it from there, you should be able to get to it from Visual Studio.

If you are NOT able to get to it from there (as I suspect), Windows Mobile should give you a more informative error message and you will know that this is not an issue with your project but rather a network connectivity issue.

[UPDATE] If you are not able to connect with your current settings, then you need to go into the Settings on your device and set up a Connection that your device uses to connect to your network.

Connections Screenshot

My emulator does not have a Wireless Card (as you can see in the image above), but you would need to configure your Wireless Card and your network's wireless router so that they can work together.

If you have not done this, then this question is answered and you need to work on getting your device connected to your network.

Here is a tutorial I found online: Texas A&M University Students: How to Connect to the Campus Wireless Network

  • thanks @jp2code and sorry for delay I did follow the instructions you stated but its failed and it showed the following message : "Cannot connect with current connection settings. To change your connection settings, tap settings." . Can you know what should I do with settings ? – Mohammed Thabet Sep 11 '12 at 23:28
  • That would depend on how your network is set up that your SQL Server is set to. See updated section in my post. –  Sep 12 '12 at 14:14
0

Try using the PPP IP address of the machine instead of the ".". Refer to this link to know how to find the PPP IP address.

Link: Get ip address of host pc from windows mobile when connected via ActiveSync

Community
  • 1
  • 1
prashanth
  • 2,059
  • 12
  • 13