我在用 桌面WPF连接数据库展示数据时正常,
用Web WPF连接数据库展示数据时出错,提示'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 类型的权限已失败'错误。
请有碰过这个问题的各位达人解答一下
附代码:
桌面WPF项目的XAML文件
Code
<Window x:Class="Wpf_binding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="陈优章对wpf的测试" Height="662" Width="827">
<Grid>
<Button Height="23" HorizontalAlignment="Left" Margin="22,11,0,0" Name="bt_test" VerticalAlignment="Top" Width="75" Click="bt_test_Click">test</Button>
<Label Height="28" HorizontalAlignment="Left" Margin="121,10.657,0,0" Name="l1" VerticalAlignment="Top" Width="200"></Label>
<ListView Name="myListBox" ItemsSource="{Binding Path= EmployeeTable }" Margin="54,66,223,155" RenderTransformOrigin="0.5,0.5" BorderThickness="2,2,2,0" OpacityMask="{x:Null}" ForceCursor="True" OverridesDefaultStyle="False" Background="#7273C0EC" BorderBrush="#00FFFFFF" Foreground="#FFFFFFFF">
<ListView.View>
<GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Employee Information">
<GridViewColumn DisplayMemberBinding=
"{Binding Path=EmployeeID}" Width="100" Header="Employee ID"/>
<GridViewColumn DisplayMemberBinding=
"{Binding Path=FirstName}"
Header="First Name" Width="100"/>
<GridViewColumn DisplayMemberBinding=
"{Binding Path=LastName}"
Header="Last Name" Width="100"/>
<GridViewColumn DisplayMemberBinding=
"{Binding Path=Title}"
Header="Title" Width="100"/>
</GridView>
</ListView.View>
<ListView.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0.723" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="-23.513" Y="0"/>
</TransformGroup>
</ListView.RenderTransform>
</ListView>
<TextBox Height="23" HorizontalAlignment="Left" Margin="270,10.657,0,0" Name="text" VerticalAlignment="Top" Width="120" />
<Button Height="23" HorizontalAlignment="Right" Margin="0,13,305,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Select</Button>
</Grid>
</Window>
桌面WPF项目的CS文件
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
namespace Wpf_binding
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
DataSet myDataSet;
//OleDbConnection acc_conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\\cap_cyz\\学习演练\\ASP.NET\\Wpf_Data\\Wpf_binding\\Wpf_binding\\App_Data\\NorthWind.mdb");
public Window1()
{
InitializeComponent();
}
private void bt_test_Click(object sender, RoutedEventArgs e)
{
l1.Content = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=App_Data\\NorthWind.mdb";//数据库路径自已调整
OleDbConnection conn = new OleDbConnection(connString);
string OleDb;
if (text.Text.Trim() != "")
{
OleDb = "SELECT * FROM Employees where EmployeeID=" + text.Text + "";
}
else
{
OleDb = "SELECT * FROM Employees";
}
OleDbDataAdapter adapter = new OleDbDataAdapter(OleDb, conn);
myDataSet = new DataSet();
adapter.Fill(myDataSet, "EmployeeTable");
myListBox.DataContext = myDataSet;
}
}
}
Web Wpf 项目的XAML文件
Code
<Page x:Class="Wpf_Binding_Web.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Page1">
<Grid Width="594">
<Button Height="23" HorizontalAlignment="Left" Margin="22,11,0,0" Name="bt_test" VerticalAlignment="Top" Width="75" Click="bt_test_Click">test</Button>
<Label Height="28" Margin="121,10.657,269,0" Name="l1" VerticalAlignment="Top"></Label>
<ListView Name="myListBox" ItemsSource="{Binding Path= EmployeeTable }" Margin="54,66,79,67" RenderTransformOrigin="0.5,0.5" BorderThickness="2,2,2,0" OpacityMask="{x:Null}" ForceCursor="True" OverridesDefaultStyle="False" Background="#7273C0EC" BorderBrush="#00FFFFFF" Foreground="#FFFFFFFF">
<ListView.View>
<GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Employee Information">
<GridViewColumn DisplayMemberBinding=
"{Binding Path=EmployeeID}" Width="100" Header="Employee ID"/>
<GridViewColumn DisplayMemberBinding=
"{Binding Path=FirstName}"
Header="First Name" Width="100"/>
<GridViewColumn DisplayMemberBinding=
"{Binding Path=LastName}"
Header="Last Name" Width="100"/>
<GridViewColumn DisplayMemberBinding=
"{Binding Path=Title}"
Header="Title" Width="100"/>
</GridView>
</ListView.View>
<ListView.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0.723" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="-23.513" Y="0"/>
</TransformGroup>
</ListView.RenderTransform>
</ListView>
<TextBox Height="23" HorizontalAlignment="Right" Margin="0,10.657,129,0" Name="text" VerticalAlignment="Top" Width="120" />
<Button Height="23" HorizontalAlignment="Right" Margin="0,11,23,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Select</Button>
</Grid>
</Page>
Web WPF项目的CS文件
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
namespace Wpf_Binding_Web
{
/// <summary>
/// Page1.xaml 的交互逻辑
/// </summary>
public partial class Page1 : Page
{
DataSet myDataSet;
public Page1()
{
InitializeComponent();
}
private void bt_test_Click(object sender, RoutedEventArgs e)
{
l1.Content = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=App_Data\\NorthWind.mdb";//数据库路径自已调整
OleDbConnection conn = new OleDbConnection(connString);
string OleDb;
if (text.Text.Trim() != "")
{
OleDb = "SELECT * FROM Employees where EmployeeID=" + text.Text + "";
}
else
{
OleDb = "SELECT * FROM Employees";
}
OleDbDataAdapter adapter = new OleDbDataAdapter(OleDb, conn);
myDataSet = new DataSet();
adapter.Fill(myDataSet, "EmployeeTable"); //测试通不过,提示'请求“System.Data.OleDb.OleDbPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”类型的权限已失败'错误
myListBox.DataContext = myDataSet;
}
}
}