How do I connect to a desktop SQL Server database?
Locked
-
Tuesday, March 02, 2010 7:27 AMModeratorHow do I connect to a desktop SQL Server database?
Answers
-
Tuesday, March 02, 2010 7:30 AMModerator
The way to connect to a desktop SQL Server database from a smart device project is different from connecting to a local SQL Server Compact database. It is more similar to a desktop Windows Forms project, as demonstrated in the following steps:
1. Create a C# smart device project in Visual Studio.
2. Add a reference to System.Data.SqlClient.
3. Add the following sample code to somewhere in your project (e.g. Form.Load):
using System.Data.SqlClient;
… …
string connString = "Data Source=<server’s address>,<port if necessary>;Initial Catalog=SqlTestDb;User Id=id;Password=pwd;";
string cmdText = "select Id, Name from Customers";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(cmdText, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
System.Diagnostics.Debug.WriteLine(string.Format("{0}, {1}", reader[0], reader[1]));
}
reader.Close();
}
4. You may need to install the correct SQL Client CAB on some devices. You can find the CAB in one of the following locations on your computer:
%Program Files%\Microsoft Visual Studio 9.0\SmartDevices\SDK\SQL Server\Client
%Program Files%\Microsoft SQL Server Compact Edition\v3.5\Devices\Client\
If you deploy the project directly to the device, Visual Studio shall do this step for you.5. Make sure the device has network connectivity. Deploy the project to the device and then run it.
For more FAQs about .NET Compact Framework, please see .NET Compact Framework FAQ.- Marked As Answer by warrentangModerator Tuesday, March 02, 2010 7:31 AM

