【发布时间】:2019-06-25 21:47:24
【问题描述】:
我正在尝试使用 UWP 应用程序连接到现有 SQL Server 数据库并且正在使用 EF Core 2.2.4,但我收到以下错误:
System.Data.SqlClient.SqlException: '建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:TCP 提供者,错误:40 - 无法打开与 SQL Server 的连接)'
我曾尝试使用谷歌和浏览论坛在互联网上研究该问题,但到目前为止似乎没有任何东西可以为我指明解决方案。我确实发现了 '16 年的答复表格,其中关于 SQLite 是 EF 和 UWP 支持的唯一数据库选项,但那是 3 年前的事了。
我实际上只是想获取一个返回的表对象列表,以在我的 UWP 上的 ListView 中显示。 EF Core 位于一个名为“DataProject”的单独项目中,模型文件夹中的所有类都作为对我的 UWP 项目“UWPProject”的引用添加。一切编译正常,但在调试时,当它尝试使用 EF Core 从 SQL 数据库中提取数据时,我收到错误 40 消息。
UWPProject - MainPage.xaml File
<Page
x:Class="UWPProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:LocalPSInvestigation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:models="using:DataProject.Models"
Loaded="Page_Loaded"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="My Complaints/Requests" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="MS Reference Sans Serif" FontSize="24" Width="300"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox x:Name="idBox" Grid.Column="0" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="ID" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0" Width="320" />
<TextBox x:Name="dateBox" Grid.Column="1" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="Date" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
<TextBox x:Name="addrBox" Grid.Column="2" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="Address" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
<TextBox x:Name="countyBox" Grid.Column="3" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="County" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
<TextBox x:Name="cityBox" Grid.Column="4" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="City" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
<TextBox x:Name="phoneBox" Grid.Column="5" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="Phone" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
<TextBox x:Name="searchBox" Grid.Column="4" Grid.Row="1" TextWrapping="Wrap" Text="" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
<TextBlock x:Name="searchLabel" Grid.Column="3" Grid.Row="1" TextWrapping="Wrap" Text="Search:" VerticalAlignment="Center" HorizontalAlignment="Right" FontFamily="MS Reference Sans Serif" FontSize="12"/>
<ListView Name="Complaints" SelectionMode="Single" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.IsVerticalRailEnabled="True" ScrollViewer.VerticalScrollMode="Enabled"
ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.IsHorizontalRailEnabled="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" Grid.Row="2"
Grid.Column="0" Grid.ColumnSpan="6" Width="1920" Height="200">
<ListView.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="ID" TextAlignment="Center" Width="320" />
<TextBlock Text="Date" TextAlignment="Center" Width="320" />
<TextBlock Text="Business Name" TextAlignment="Center" Width="200" />
<TextBlock Text="Address" TextAlignment="Center" Width="320" />
<TextBlock Text="County" TextAlignment="Center" Width="320" />
<TextBlock Text="City" TextAlignment="Center" Width="240" />
<TextBlock Text="Phone" TextAlignment="Center" Width="200" />
</StackPanel>
</DataTemplate>
</ListView.HeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate x:DataType="models:TableObject">
<StackPanel Orientation="Horizontal">
<TextBlock Name="ComplaintId" TextAlignment="Center" Text="{x:Bind ProgramComplaintId}" Width="320"/>
<TextBlock Name="Date" TextAlignment="Center" Text="{x:Bind Date}" Width="320"/>
<TextBlock Name="BusinessName" TextAlignment="Center" Text="{x:Bind BusinessName}" Width="200"/>
<TextBlock Name="Address" TextAlignment="Center" Text="{x:Bind Address}" Width="320"/>
<TextBlock Name="County" TextAlignment="Center" Text="{x:Bind County}" Width="320"/>
<TextBlock Name="City" TextAlignment="Center" Text="{x:Bind City}" Width="240"/>
<TextBlock Name="Phone" TextAlignment="Center" Text="{x:Bind Phone}" Width="200"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</StackPanel>
</Page>
UWPProject - MainPage.xaml.cs File
------------------------------------------------------------------------------
private void Page_Loaded(object sender, RoutedEventArgs e)
{
using(var db = new dbContext())
{
ListViewName.ItemsSource = db.TableObject.ToList();
}
}
------------------------------------------------------------------------------
DataProject - DBContext.cs File
------------------------------------------------------------------------------
public DbContext()
{
}
public DbContext(DbContextOptions<DbContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer("Server=XXXXXXX;Database=XXXXX;user id=XXXXXXXXX;password=XXXXXXX");
}
}
------------------------------------------------------------------------------
我希望这会提取表对象列表并将其分配给项目源,但我收到了连接错误。我知道指定的用户名/密码有效,并且服务器和数据库存在,因为我使用了脚手架命令来生成类。
UWP 是否支持 SQL Server 与 EF Core 的连接?我似乎找不到任何说明这种情况的内容。
【问题讨论】:
-
UWP 确实支持与 EF Core 的 SQL 服务器连接。请按照thread 更改连接字符串以使用 TCP/IP 看看它是否可以工作。
-
我尝试了您的建议,但仍然收到上述错误。我已使用 TCP/IP 连接和凭据检查并连接到 SQL Server。我肯定不知所措,我什至按照另一个用户的建议激活了集成安全性。
标签: c# .net-core uwp-xaml ef-core-2.1