Search code examples
c#visual-studio-2008visual-studio-2008-sp1

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:

error details captured by the debugger

This is from the debugger when I put the breakpoint at the catch statement


Solution

  • 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