C# 无法与SQL Server建立SQL客户端连接
我正在使用Xamarin在制作移动应用程序时连接到SQL Server。该应用程序在iOS上运行。我正在使用sql客户端与服务器建立连接,但我遇到了奇怪的错误,并且用尽了我的大部分选择 请不要警告我有关直接连接的安全问题。这不是问题所在 我收到的错误是:提供程序:tcp提供程序,错误:40-无法打开到sql server的连接) 这是我的密码:C# 无法与SQL Server建立SQL客户端连接,c#,sql-server,xamarin,sqlconnection,sqlclient,C#,Sql Server,Xamarin,Sqlconnection,Sqlclient,我正在使用Xamarin在制作移动应用程序时连接到SQL Server。该应用程序在iOS上运行。我正在使用sql客户端与服务器建立连接,但我遇到了奇怪的错误,并且用尽了我的大部分选择 请不要警告我有关直接连接的安全问题。这不是问题所在 我收到的错误是:提供程序:tcp提供程序,错误:40-无法打开到sql server的连接) 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Data;
using System.Data.SqlClient;
namespace EmployeeSnapshot
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
void Button_Clicked(System.Object sender, System.EventArgs e)
{
var EmployeeID = UserInput.Text;
Console.WriteLine(EmployeeID);
string con = "Data Source ={Server}; Initial Catalog ={database}; Integrated Security = False; User ID ={username}; Password ={password}";
string matchingPerson;
using (SqlConnection connection = new SqlConnection(con))
{
string oString = "SELECT something FROM table WHERE employeeid = @employeeID";
SqlCommand command = new SqlCommand(oString, connection);
command.Parameters.AddWithValue("@employeeID", EmployeeID);
connection.Open();
Console.WriteLine("State: {0}", connection.State);
using (SqlDataReader reader = command.ExecuteReader())
{
matchingPerson = reader["name"].ToString();
}
command.Connection.Close();
}
Console.WriteLine(matchingPerson);
}
}
}
代码在Connection.open()行出错
我所尝试的:
- 更改了连接字符串,扰乱了集成安全性,确保服务器正常运行,等等
- 我正在MacBook上制作此应用程序。我有并行程序,所以我可以使用虚拟机进入windows。我决定利用这一点,复制我的连接字符串并将其粘贴到windows端的visual studio中。我执行了一个基本的select语句并检索到了数据,因此连接字符串似乎不是问题所在
- 我几乎觉得这和Xamarin或者我在MacBook上的事实有关