Silverlight使用WCF操纵数据库(SQL Server)指南
2012-06-18 17:01
351 查看
Silverlight作为WPF的Web版,精简了许多内容,作为Web的客户端程序,Silverlight无法直接操纵数据库,这点与WPF的功能有相当大的差别。经过搜索及研究,Silverlight可以通过WCF服务连接远程服务器的数据库,并进行相关数据操作。
Windows Communication Foundation (WCF) 是由微软发展的一组数据通信的应用程序开发接口,它是.NET框架的一部分,由 .NET Framework 3.0 开始引入,与 Windows Presentation Foundation 及 Windows Workflow Foundation 并行为新一代 Windows 操作系统以及 WinFX 的三个重大应用程序开发类库。因此微软重新查看了这些通信方法,并设计了一个统一的程序开发模型,对于数据通信提供了最基本最有弹性的支持,这就是 Windows Communication Foundation。因此微软重新查看了这些通信方法,并设计了一个统一的程序开发模型,对于数据通信提供了最基本最有弹性的支持,这就是 Windows Communication Foundation。WCF 由于集合了几乎由 .NET Framework 所提供的通信方法。
OK,下面我们开始进行Silverlight的访问数据库之旅。(此前请配置好Silverlight的开发环境和了解开发基本知识)
Silverlight4.0开发时包含了启用了Silverlight的WCF服务,大大简化了WCF服务的添加。
注意webconfig文件中要把mexHttpBinding改成wsHttpBinding
参考文档: http://www.cnblogs.com/lyj/archive/2008/04/17/1158346.html http://www.silverlightchina.net/html/tips/2010/1122/3576.html?1290928538
下面引用操纵MYSQL的示例:
*********************************************************
1. 建立一个新项目"SilverlightDBDemo",
![](http://dev.yesky.com/uploadImages/2009/365/51T134HU0C28.jpg)
2. 在MainPage中建立简单的登录界面,如下:
![](http://dev.yesky.com/uploadImages/2009/365/PS49T2TNAJT0.jpg)
3. 在Web项目中添加新选项
![](http://dev.yesky.com/uploadImages/2009/365/69YZDT9RUV2U.jpg)
4. 添加一个简单的用户信息类Users,作为WCF的契约成员,当我们从数据库中读取信息后,将赋值给该类的契约成员,方便客户端进行调用;
![](http://dev.yesky.com/uploadImages/2009/365/N5BJMZHYZO84.jpg)
VS2008将自动生成Users类代码,在类命名前添加数据契约属性[DataContract()]。 为了能够使绑定数据返回修改通知,这里需要继承INotifyPropertyChanged接口,该步骤不添加对本教程也没有影响,为了以后例程代码完整性,这里我继承了该接口。在接口上点击右键,生成代码。
![](http://dev.yesky.com/uploadImages/2009/365/805S80E3MYAU.jpg)
代码如下:
namespace SilverlightDBDemo.Web
{
[DataContract()]
public class Users : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}
5. 在Users类中,添加契约成员
private string userName;
[DataMember()]
public string UserName
{
get { return userName; }
set { userName = value;}
}
private string password;
[DataMember()]
public string Password
{
get { return password; }
set { password = value; }
}
6. 建立构造函数 public Users(string sUserName,string sPassword),传递用户名和密码给契约成员;
using System;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace SilverlightDBDemo.Web
{
[DataContract()]
public class Users : INotifyPropertyChanged
{
private string userName;
[DataMember()]
public string UserName
{
get { return userName; }
set { userName = value;}
}
private string password;
[DataMember()]
public string Password
{
get { return password; }
set { password = value; }
}
public Users(string sUserName,string sPassword)
{
UserName = sUserName;
Password = sPassword;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
7. 添加"Silverlight-enabled WCF Service",修改服务名字为 DBService.svc,需要注意的是,WCF service对于Silverlight仅支持BasicHttpBinding,而VS2008自动生成是customBinding,很多朋友说使用了"Silverlight-enabled WCF Service",链接数据库仍旧失败,无法找到远程服务器,是因为没有使用BasicHttpBinding进行通讯,造成的失败。
后文我将讲述如何修改。
![](http://dev.yesky.com/uploadImages/2009/365/OO1A21LKVCE3.jpg)
8. 添加后,在Web服务器端会有DBService.svc和DBService.svc.cs文件出现,VS2008将自动更新Web项目的类库引用;
![](http://dev.yesky.com/uploadImages/2009/365/A0P9JZ581B28.jpg)
9. 双击进入DBService.svc.cs文件,可以看到以下代码:
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
namespace SilverlightDBDemo.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DBService
{
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
// Add more operations here and mark them with [OperationContract]
}
}
这里我们没有修改ServiceContract命名空间,所以保持默认为空,AspNet的兼容需求模式我们保持默认。在正式项目中,我们习惯将所有的[OperationContract]函数放入一个接口文件中,这样方便扩展以及维护,在本例,为了方便大家理解,就不把[OperationContract]放入接口文件。 在VS2008自动生成代码下面直接添加数据库访问代码。
10. 在添加服务器端数据库访问代码前,需要修改Web.Config文件。和Asp.Net项目一样,在链接数据库前,我们首先需要在Web.Config中配置数据库连接字符串,请自行替换数据库登录ID和密码
<appSettings>
<add key="DbServiceConnectionString" value="Data Source=(Local);
Initial Catalog=SilverlightDemo;
User Id=dev;Password=dev;"/>
</appSettings>
11. 前文已经说过,Silverlight仅支持使用BasicHttpBinding通过WCF service进行通讯,而VS2008自动生成的代码是customBinding,所以,我们也需要在Web.Config中进行修改.下面是VS2008自动生成的Web.Config部分代码,划线部分是下面要修改的部分。
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="SilverlightDBDemo.Web.DBServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="customBinding0">
<binaryMessageEncoding />
<httpTransport />
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="SilverlightDBDemo.Web.DBServiceBehavior"
name="SilverlightDBDemo.Web.DBService">
<endpoint address="" binding="customBinding" bindingConfiguration="customBinding0"
contract="SilverlightDBDemo.Web.DBService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
-->这里我们需要修改以下几个地方:
首先删除customBinding,从上面代码,第10行,到17行,使用下面代码替换:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDataService"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<readerQuotas
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxDepth="2147483647"
maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
其中那些2147483647之类的属性可以删除,但是如果读取数据库中的大型表格,就需要设置缓冲池之类的尺寸了。这里,我们已经使用了basicHttpBinding. Binding name我使用了BasicHttpBinding_DBService,大家可以随意更换,下面将用到。
然后修改22行和23行的代码,将endpoint中的binding,内容修改为basicHttpBinding,bindingConfiguration的内容修改为BasicHttpBinding_DBService。
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_DBService"
contract="SilverlightDBDemo.Web.DBService" />
12. 现在我们可以在DBService.svc.cs中添加存取数据库代码,对用户名和密码进行简单匹配,这里不再着重讲述如何条件匹配登录信息。这里演示了如何调用数据库存储过程。完成存取数据库代码后,成功编译Web项目。代码有点长,这里折叠起来。
private string connectionString = WebConfigurationManager.AppSettings["DbServiceConnectionString"];
[OperationContract]
public bool GetUser(string cUserName, string cPassword)
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("Login", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", cUserName);
try
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
if (reader.Read())
{
Users user = new Users((string)reader["cUserName"],
(string)reader["cPassword"]);
if (user.Password == cPassword)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
finally
{
conn.Close();
}
}
13. 在SilverlightDBDemo客户端,点击右键添加服务引用
![](http://dev.yesky.com/uploadImages/2009/365/1Z6R4726W872.jpg)
14. 在弹出窗口中,点击"Discover",查找本地WCF service。在地址栏会自动搜索到本地的Service引用,在Services树形框中我们可以看到,在服务器端建立的DBService.svc,双击打开,可以看到,我们建立的GetUser函数,以及默认的DoWork函数。修改下面的命名空间为"DBService",方便调用。
![](http://dev.yesky.com/uploadImages/2009/365/OQA127Q752C9.jpg)
15. 点击"Advanced.."高级按钮,确认选中"Reuse types in referenced assembiles",如下图,
![](http://dev.yesky.com/uploadImages/2009/365/43660496XW2U.jpg)
16. 然后,点击确定,会在客户端中生成DBService服务引用。
![](http://dev.yesky.com/uploadImages/2009/365/U4V6L5LUB87U.jpg)
17. 在生成DBService服务引用后,VS2008会自动生成一个ServiceReferences.ClientConfig文件。
我们需要留意查看一下该文件内容。其中,bindings信息是basicHttpBinding,而endpoint内容和Web.Config中的内容相同。这里我们不需要修改任何代码。
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_DBService" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None">
<transport>
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/SilverlightDBDemo.Web/DBService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_DBService"
contract="DBService.DBService" name="BasicHttpBinding_DBService" />
</client>
</system.serviceModel>
</configuration>
18. 下面我们将在客户端调用该服务引用,获取数据库的返回值,根据返回值,我们将简单判断登录是否成功。
进入MainPage.xaml.cs中,建立GetUser方法。该代码中EndpointAddress是最重要的,出现没有发现远程服务器错误,和这里设置也有关系。在client_GetUserCompleted中,e.Result代表了数据库返回值。可以接受任何值,大家可以根据需要进行值类型转换。每次,用户点击登陆按钮,Silverlight客户端都会向服务器端请求验证,返回结果会在提示信息栏显示。
private void GetUser()
{
EndpointAddress address = new EndpointAddress(new Uri(Application.Current.Host.Source, "/SilverlightDBDemo.Web/DBService.svc"));
DBServiceClient client = new DBServiceClient(new BasicHttpBinding(), address);
client.GetUserCompleted += client_GetUserCompleted;
client.GetUserAsync(txtUsername.Text, pbPassword.Password);
}
private void client_GetUserCompleted(object sender, GetUserCompletedEventArgs e)
{
try
{
if (e.Result)
{
tbMessage.Text = "登录成功!";
}
else
{
tbMessage.Text = "登录失败!";
}
}
catch (Exception error)
{
tbMessage.Text = error.ToString();
}
}
private void btLogin_Click(object sender, RoutedEventArgs e)
{
GetUser();
}
登录成功如下图:
![](http://dev.yesky.com/uploadImages/2009/365/X83G128Z38GM.jpg)
到这里为止,我想你已经学会了如何使用WCF存取MSSQL数据库了
Windows Communication Foundation (WCF) 是由微软发展的一组数据通信的应用程序开发接口,它是.NET框架的一部分,由 .NET Framework 3.0 开始引入,与 Windows Presentation Foundation 及 Windows Workflow Foundation 并行为新一代 Windows 操作系统以及 WinFX 的三个重大应用程序开发类库。因此微软重新查看了这些通信方法,并设计了一个统一的程序开发模型,对于数据通信提供了最基本最有弹性的支持,这就是 Windows Communication Foundation。因此微软重新查看了这些通信方法,并设计了一个统一的程序开发模型,对于数据通信提供了最基本最有弹性的支持,这就是 Windows Communication Foundation。WCF 由于集合了几乎由 .NET Framework 所提供的通信方法。
OK,下面我们开始进行Silverlight的访问数据库之旅。(此前请配置好Silverlight的开发环境和了解开发基本知识)
Silverlight4.0开发时包含了启用了Silverlight的WCF服务,大大简化了WCF服务的添加。
注意webconfig文件中要把mexHttpBinding改成wsHttpBinding
参考文档: http://www.cnblogs.com/lyj/archive/2008/04/17/1158346.html http://www.silverlightchina.net/html/tips/2010/1122/3576.html?1290928538
下面引用操纵MYSQL的示例:
*********************************************************
1. 建立一个新项目"SilverlightDBDemo",
![](http://dev.yesky.com/uploadImages/2009/365/51T134HU0C28.jpg)
2. 在MainPage中建立简单的登录界面,如下:
![](http://dev.yesky.com/uploadImages/2009/365/PS49T2TNAJT0.jpg)
3. 在Web项目中添加新选项
![](http://dev.yesky.com/uploadImages/2009/365/69YZDT9RUV2U.jpg)
4. 添加一个简单的用户信息类Users,作为WCF的契约成员,当我们从数据库中读取信息后,将赋值给该类的契约成员,方便客户端进行调用;
![](http://dev.yesky.com/uploadImages/2009/365/N5BJMZHYZO84.jpg)
VS2008将自动生成Users类代码,在类命名前添加数据契约属性[DataContract()]。 为了能够使绑定数据返回修改通知,这里需要继承INotifyPropertyChanged接口,该步骤不添加对本教程也没有影响,为了以后例程代码完整性,这里我继承了该接口。在接口上点击右键,生成代码。
![](http://dev.yesky.com/uploadImages/2009/365/805S80E3MYAU.jpg)
代码如下:
namespace SilverlightDBDemo.Web
{
[DataContract()]
public class Users : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}
5. 在Users类中,添加契约成员
private string userName;
[DataMember()]
public string UserName
{
get { return userName; }
set { userName = value;}
}
private string password;
[DataMember()]
public string Password
{
get { return password; }
set { password = value; }
}
6. 建立构造函数 public Users(string sUserName,string sPassword),传递用户名和密码给契约成员;
using System;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace SilverlightDBDemo.Web
{
[DataContract()]
public class Users : INotifyPropertyChanged
{
private string userName;
[DataMember()]
public string UserName
{
get { return userName; }
set { userName = value;}
}
private string password;
[DataMember()]
public string Password
{
get { return password; }
set { password = value; }
}
public Users(string sUserName,string sPassword)
{
UserName = sUserName;
Password = sPassword;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
7. 添加"Silverlight-enabled WCF Service",修改服务名字为 DBService.svc,需要注意的是,WCF service对于Silverlight仅支持BasicHttpBinding,而VS2008自动生成是customBinding,很多朋友说使用了"Silverlight-enabled WCF Service",链接数据库仍旧失败,无法找到远程服务器,是因为没有使用BasicHttpBinding进行通讯,造成的失败。
后文我将讲述如何修改。
![](http://dev.yesky.com/uploadImages/2009/365/OO1A21LKVCE3.jpg)
8. 添加后,在Web服务器端会有DBService.svc和DBService.svc.cs文件出现,VS2008将自动更新Web项目的类库引用;
![](http://dev.yesky.com/uploadImages/2009/365/A0P9JZ581B28.jpg)
9. 双击进入DBService.svc.cs文件,可以看到以下代码:
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
namespace SilverlightDBDemo.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DBService
{
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
// Add more operations here and mark them with [OperationContract]
}
}
这里我们没有修改ServiceContract命名空间,所以保持默认为空,AspNet的兼容需求模式我们保持默认。在正式项目中,我们习惯将所有的[OperationContract]函数放入一个接口文件中,这样方便扩展以及维护,在本例,为了方便大家理解,就不把[OperationContract]放入接口文件。 在VS2008自动生成代码下面直接添加数据库访问代码。
10. 在添加服务器端数据库访问代码前,需要修改Web.Config文件。和Asp.Net项目一样,在链接数据库前,我们首先需要在Web.Config中配置数据库连接字符串,请自行替换数据库登录ID和密码
<appSettings>
<add key="DbServiceConnectionString" value="Data Source=(Local);
Initial Catalog=SilverlightDemo;
User Id=dev;Password=dev;"/>
</appSettings>
11. 前文已经说过,Silverlight仅支持使用BasicHttpBinding通过WCF service进行通讯,而VS2008自动生成的代码是customBinding,所以,我们也需要在Web.Config中进行修改.下面是VS2008自动生成的Web.Config部分代码,划线部分是下面要修改的部分。
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="SilverlightDBDemo.Web.DBServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="customBinding0">
<binaryMessageEncoding />
<httpTransport />
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="SilverlightDBDemo.Web.DBServiceBehavior"
name="SilverlightDBDemo.Web.DBService">
<endpoint address="" binding="customBinding" bindingConfiguration="customBinding0"
contract="SilverlightDBDemo.Web.DBService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
-->这里我们需要修改以下几个地方:
首先删除customBinding,从上面代码,第10行,到17行,使用下面代码替换:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDataService"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<readerQuotas
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxDepth="2147483647"
maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
其中那些2147483647之类的属性可以删除,但是如果读取数据库中的大型表格,就需要设置缓冲池之类的尺寸了。这里,我们已经使用了basicHttpBinding. Binding name我使用了BasicHttpBinding_DBService,大家可以随意更换,下面将用到。
然后修改22行和23行的代码,将endpoint中的binding,内容修改为basicHttpBinding,bindingConfiguration的内容修改为BasicHttpBinding_DBService。
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_DBService"
contract="SilverlightDBDemo.Web.DBService" />
12. 现在我们可以在DBService.svc.cs中添加存取数据库代码,对用户名和密码进行简单匹配,这里不再着重讲述如何条件匹配登录信息。这里演示了如何调用数据库存储过程。完成存取数据库代码后,成功编译Web项目。代码有点长,这里折叠起来。
private string connectionString = WebConfigurationManager.AppSettings["DbServiceConnectionString"];
[OperationContract]
public bool GetUser(string cUserName, string cPassword)
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("Login", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", cUserName);
try
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
if (reader.Read())
{
Users user = new Users((string)reader["cUserName"],
(string)reader["cPassword"]);
if (user.Password == cPassword)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
finally
{
conn.Close();
}
}
13. 在SilverlightDBDemo客户端,点击右键添加服务引用
![](http://dev.yesky.com/uploadImages/2009/365/1Z6R4726W872.jpg)
14. 在弹出窗口中,点击"Discover",查找本地WCF service。在地址栏会自动搜索到本地的Service引用,在Services树形框中我们可以看到,在服务器端建立的DBService.svc,双击打开,可以看到,我们建立的GetUser函数,以及默认的DoWork函数。修改下面的命名空间为"DBService",方便调用。
![](http://dev.yesky.com/uploadImages/2009/365/OQA127Q752C9.jpg)
15. 点击"Advanced.."高级按钮,确认选中"Reuse types in referenced assembiles",如下图,
![](http://dev.yesky.com/uploadImages/2009/365/43660496XW2U.jpg)
16. 然后,点击确定,会在客户端中生成DBService服务引用。
![](http://dev.yesky.com/uploadImages/2009/365/U4V6L5LUB87U.jpg)
17. 在生成DBService服务引用后,VS2008会自动生成一个ServiceReferences.ClientConfig文件。
我们需要留意查看一下该文件内容。其中,bindings信息是basicHttpBinding,而endpoint内容和Web.Config中的内容相同。这里我们不需要修改任何代码。
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_DBService" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None">
<transport>
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/SilverlightDBDemo.Web/DBService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_DBService"
contract="DBService.DBService" name="BasicHttpBinding_DBService" />
</client>
</system.serviceModel>
</configuration>
18. 下面我们将在客户端调用该服务引用,获取数据库的返回值,根据返回值,我们将简单判断登录是否成功。
进入MainPage.xaml.cs中,建立GetUser方法。该代码中EndpointAddress是最重要的,出现没有发现远程服务器错误,和这里设置也有关系。在client_GetUserCompleted中,e.Result代表了数据库返回值。可以接受任何值,大家可以根据需要进行值类型转换。每次,用户点击登陆按钮,Silverlight客户端都会向服务器端请求验证,返回结果会在提示信息栏显示。
private void GetUser()
{
EndpointAddress address = new EndpointAddress(new Uri(Application.Current.Host.Source, "/SilverlightDBDemo.Web/DBService.svc"));
DBServiceClient client = new DBServiceClient(new BasicHttpBinding(), address);
client.GetUserCompleted += client_GetUserCompleted;
client.GetUserAsync(txtUsername.Text, pbPassword.Password);
}
private void client_GetUserCompleted(object sender, GetUserCompletedEventArgs e)
{
try
{
if (e.Result)
{
tbMessage.Text = "登录成功!";
}
else
{
tbMessage.Text = "登录失败!";
}
}
catch (Exception error)
{
tbMessage.Text = error.ToString();
}
}
private void btLogin_Click(object sender, RoutedEventArgs e)
{
GetUser();
}
登录成功如下图:
![](http://dev.yesky.com/uploadImages/2009/365/X83G128Z38GM.jpg)
到这里为止,我想你已经学会了如何使用WCF存取MSSQL数据库了
相关文章推荐
- Silverlight使用WCF操纵数据库(SQL Server)指南
- Silverlight + WCF使用Linq to SQL以及ADO.NET Entity Data Model更新数据库子表方法
- SilverLight使用WCF RIA SERVICE实现对数据库的操作 (添加,删除,更新)
- Silverlight知识随手记[2]:Silverlight使用LINQ、WCF访问MSSQL数据库
- Silverlight中,使用ADO.NET Entity,WCF RIA,动态加载指定数据库的实现。
- MySQLdb for Python使用指南/Python的数据库操作
- Android创建和使用数据库详细指南
- (转)[Python]MySQLdb for Python使用指南/Python的数据库操作【http://www.cnblogs.com/czh-liyu/archive/2008/04/13/1151758.html】
- 使用 InstallShield 安装和卸载SQL Server 数据库[收藏]
- 转:使用fn_dblog解析SQL SERVER 数据库日志方法
- SQL Server跨数据库访问及分布式事务的使用及配置
- 使用JDBC连接SQL Server 2005数据库
- SQL Server 数据库使用备份还原造成的孤立用户和对象名‘xxx’无效的错误的解决办法
- 使用VS2010连接SQL Server 2008数据库混合编程以实现对数据库的操作
- 我正在使用的一个SQL Server 2000/2005/2008 数据库访问类-SqlCommon(C#)
- Android Realm数据库使用指南
- SQL Server之——使用windows身份验证,jdbc连接数据库
- 玩转Android之数据库框架greenDAO3.0使用指南
- 使用PowerDesigner 设计SQL Server 数据库
- 复制数据库时,必须要先停止Sql Server服务,才能复制.否则提示:无法复制,文件正在被另一个人或程序使用.