【IT168 技术】基于安全原因的考虑,Silverlight是不允许直接访问数据库的。但还是有很多方法来间接实现Silverlight对数据库的访问。比如以下的三种方式:1 RESTful API;2 在Silverlight下借助JavaScript来实现访问数据库;3 利用.NET Web服务模板——Silverlight-enabled。本文将主要介绍一下后俩种方法。
利用JavaScript实现Silverlight与数据库的交互
只要在Silverlight事件处理的后面添加调用以下简单的JavaScript代码即可。其中”callWebService”是JavaScript的函数。
protected void btnSubmit(object sender,EventArgs e)
{
System.Windows.Browser.HtmlPage.Window.Invoke("callWebService", null);
}
{
System.Windows.Browser.HtmlPage.Window.Invoke("callWebService", null);
}
关于JavaScript的调用可以参照一下内容:
通过ScriptServiceAttribute添加
[ScriptService]
public class UserValidationService: System.Web.Services.WebService
{
[WebMethod]
public bool ValidateUserName(string strInput)
{
return !GetUserByUserName(strInput); //If user exists return false indicates the name is no longer availabe.
}
private bool GetUserByUserName(string strUserName)
{
bool blnIsUserExists = false;
//Call database API to see if the username is availabe, set blnIsUserExists to true if exists.
return blnIsUserExists;
}
}
public class UserValidationService: System.Web.Services.WebService
{
[WebMethod]
public bool ValidateUserName(string strInput)
{
return !GetUserByUserName(strInput); //If user exists return false indicates the name is no longer availabe.
}
private bool GetUserByUserName(string strUserName)
{
bool blnIsUserExists = false;
//Call database API to see if the username is availabe, set blnIsUserExists to true if exists.
return blnIsUserExists;
}
}
通过页面添加ScriptMananger控件
<asp:ScriptManager runat="server" ID="scriptManagerId">
<Services>
<asp:ServiceReference Path="UserValidationService.asmx" />
</Services>
</asp:ScriptManager>
<Services>
<asp:ServiceReference Path="UserValidationService.asmx" />
</Services>
</asp:ScriptManager>
像调用JavaScript局部函数一样调用Webservice
<script type="text/javascript">
function validateUserName()
{
var userName = document.getElementById("txtUserName").value;
UserValidationService.ValidateUserName(userName,showValidateResult,validateUserNameError);
}
function validateUserNameError(result)
{
//Do nothing if any error, ideally, we should log this error to database.
}
function showValidateResult(result)
{
//Since it is only a boolean value, no need to get result.d, if result contains .net object,
// use result.length and result.d to retrieve the object.
if(!result)
{
//Not available
}else
{
//Username is still available
}
}
</script>
function validateUserName()
{
var userName = document.getElementById("txtUserName").value;
UserValidationService.ValidateUserName(userName,showValidateResult,validateUserNameError);
}
function validateUserNameError(result)
{
//Do nothing if any error, ideally, we should log this error to database.
}
function showValidateResult(result)
{
//Since it is only a boolean value, no need to get result.d, if result contains .net object,
// use result.length and result.d to retrieve the object.
if(!result)
{
//Not available
}else
{
//Username is still available
}
}
</script>
利用Silverlight-enabled Webservice实现Silverlight与数据库的交互
首先通过模板创建一个Silverlight-enabled Webservice,然后将数据库和其他Server-related添加到服务里。接下来,将此服务添加引用到Silverlight 应用中。最后利用如下代码从Silverlight 应用中调用webservice。
using MySilver.MyService;
......
.....
private void btnSend_Click(object sender, RoutedEventArgs e)
{
if (!String.IsNullOrEmpty(txtMessage.Text.Trim()))
{
lstHisotryMessage.Items.Add("Gene: " + txtMessage.Text.Trim());
GeneMessage message = new GeneMessage();
message.Body = txtMessage.Text.Trim();
MyServiceclient = new MyService();
client.SendMessageCompleted += new EventHandler<SendMessageCompletedEventArgs>(client_SendMessageCompleted);
client.SendMessageAsync(message);
}
else
{
MessageBox.Show("You cannot send empty message!");
}
}
protected void client_SendMessageCompleted(object sender, SendMessageCompletedEventArgs e)
{
txtMessage.Text = e.Result.MessageID.ToString();
}
......
.....
private void btnSend_Click(object sender, RoutedEventArgs e)
{
if (!String.IsNullOrEmpty(txtMessage.Text.Trim()))
{
lstHisotryMessage.Items.Add("Gene: " + txtMessage.Text.Trim());
GeneMessage message = new GeneMessage();
message.Body = txtMessage.Text.Trim();
MyServiceclient = new MyService();
client.SendMessageCompleted += new EventHandler<SendMessageCompletedEventArgs>(client_SendMessageCompleted);
client.SendMessageAsync(message);
}
else
{
MessageBox.Show("You cannot send empty message!");
}
}
protected void client_SendMessageCompleted(object sender, SendMessageCompletedEventArgs e)
{
txtMessage.Text = e.Result.MessageID.ToString();
}