'Connecting to a SQL Server 2012 database in Visual Studio 2008
I am currently developing a mobile application based on Windows CE for my team to track out assets and store them in our existing database.
I am able to develop and deploy the software successfully however I can't seem to connect to my SQL Server 2012 database from Visual Studio 2008. When I try to connect from Visual Studio 2017, it works just fine.
This is my test code only not my real asset tracker code so it wont have the UI I built for the asset tracker app.
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace test_smart_device
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += new EventHandler(button_connect);
}
private void button_connect(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection cnn ;
connetionString = "Data Source=172.16.206.20;Initial Catalog=IBusinessTest;Integrated Security=SSPI;User ID=username;Password=123456";
cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show ("Connection Open ! ");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}
}
}
When I try to connect to my database, I get this error:
This is from the debugger when I put the breakpoint at the catch statement
Solution 1:[1]
I've decided that I won't use direct server connection using sqlClient class. Instead I will host a SOAP service on my server that will take my input in XML format via a HTTP link.
using System.Net;
using System.Xml;
public static void Execute_soap(string l_mat, decimal l_qty, string l_batch)
{
HttpWebRequest request = CreateWebRequest();
request.Credentials = CredentialCache.DefaultCredentials;
XmlDocument soapEnvelopeXml = new XmlDocument();
NewMethod(soapEnvelopeXml, l_mat, l_qty, l_batch);
request.AllowWriteStreamBuffering = true;
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
using (WebResponse response = request.GetResponse())
{
//Console.WriteLine(((HttpWebResponse)response).StatusDescription);
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
using (StreamWriter writer = new StreamWriter(@"C:\Users\ikhsan\Desktop\xml_file.txt"))
{
writer.WriteLine(soapResult);
}
Console.WriteLine(soapResult);
Console.WriteLine("Done!");
//Console.ReadKey();
}
}
}
private static void NewMethod(XmlDocument soapEnvelopeXml, string mat, decimal qty, string batch)
{
string xml_str = @"<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">" +
@"<soap12:Body>" +
@"<PCB_SCAN_EMS xmlns=""http://tempuri.org/"">" +
@"<part_num>" + mat + "</part_num>" +
@"<qty>" + qty.ToString() + "</qty>" +
"<batch>" + batch + "</batch>" +
"<ems_loc>2106</ems_loc>" +
"</PCB_SCAN_EMS>" +
"</soap12:Body>" +
"</soap12:Envelope>";
soapEnvelopeXml.LoadXml(xml_str);
}
public static HttpWebRequest CreateWebRequest()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://172.16.206.19/PART_INFO/get_part_quantity_from_bin.asmx");
webRequest.ContentType = @"application/soap+xml;charset=UTF-8";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
This will work in all windows mobile version
Solution 2:[2]
Two things stick out to me:
- You connection string includes
Integrated Security=SSPI;
, but you also includeUser ID=username;Password=123456
- so which is it, are you using Integrated Security (ie. logging in with the Windows user that the program is running as - domain or identical workgroup credentials on both the CE device and SQL Server), or are you trying to use SQL Server auth (by specifying a user ID and password)? You can't use both. - The error
SQL Server does not exist or access denied
implies that your connection was rejected, but doesn't say why. If it's not credentials then it could be the connection itself. Have you checked that the server is remotely accessible from the device? Are they on the same local network? If using a physical device on WiFi, does it have permission to communicate with other devices on the network (some WiFi routers, especially corporate ones, have the option of only allowing traffic to the gateway, and rejecting requests to local addresses).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user3308595 |
Solution 2 | pcdev |