繁体   English   中英

在 Visual Studio 2008 中连接到 SQL Server 2012 数据库

[英]Connecting to a SQL Server 2012 database in Visual Studio 2008

我目前正在为我的团队开发基于 Windows CE 的移动应用程序,以跟踪资产并将其存储在我们现有的数据库中。

我能够成功开发和部署软件,但是我似乎无法从 Visual Studio 2008 连接到我的 SQL Server 2012 数据库。当我尝试从 Visual Studio 2017 连接时,它工作正常。

这是我的测试代码,不是我真正的资产跟踪器代码,因此它不会有我为资产跟踪器应用程序构建的 UI。

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 ! ");
            }
        }
    }
}

当我尝试连接到我的数据库时,出现此错误:

调试器捕获的错误详细信息

当我将断点放在 catch 语句时,这是来自调试器

我有两件事:

  • 您的连接字符串包括Integrated Security=SSPI; ,但您还包括User ID=username;Password=123456这就是您正在使用集成安全性(例如,以Windows用户身份登录该程序正在作为Windows 2000用户)在两个CE设备上以域或相同的工作组凭据登录和SQL Server),还是尝试使用SQL Server身份验证(通过指定用户ID和密码)? 不能同时使用。
  • 错误SQL Server does not exist or access denied意味着您的连接被拒绝,但没有说明原因。 如果不是凭据,则可能是连接本身。 您是否检查过可以从设备远程访问服务器? 它们在同一本地网络上吗? 如果在WiFi上使用物理设备,它是否有权与网络上的其他设备通信(某些WiFi路由器,尤其是公司路由器,可以选择仅允许流量进入网关,并拒绝对本地地址的请求)。

我决定不使用 sqlClient class 使用直接服务器连接。相反,我将在我的服务器上托管一个 SOAP 服务,该服务将通过 HTTP 链接以 XML 格式接收我的输入。

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;

    }

这将适用于所有 windows 移动版本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM