![](/img/trans.png)
[英]connecting database through sql server management studio 2008 but not through visual studio 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.