C#从PHP函数返回字符串
您好,我的服务器上有一个脚本,它会返回一个简单的字符串;我正在尝试使用Silverlight C#从我的数据库返回一个字符串,这样我就可以将它绑定到我的网站。
我已经让PHP和Silverlight一起工作了;我只需要一种方法,这样我就可以从网站调用脚本并将返回的值存储在C#字符串中。
无论如何我能做到这一点吗? />
谢谢
Hello, I've got a script on my server which will return a simple string; I am trying to use Silverlight C# to return a string from my database so I can bind it into my site.
I've got PHP and Silverlight working together; I just need a way so I can call a script from a website and have the returned value get stored in a C# string.
Anyway I can do this?
Thanks
我遇到了像你这样的确切问题。
使用将SilverLight连接到MySQL的PHP脚本是我的第一种方法。
但后来,我发现了一种更方便/更简单的方法,使用C#Web服务。
我写了一篇文章,用插图详细说明了步骤。
>> 将SilverLight与Web服务连接MySQL [ ^ ]
简单说明:
1.您需要创建一个C#Web服务。
2.配置Web服务访问权限
(默认情况下,Web服务只允许localhost访问)
3.在SilverLight应用程序中,添加您创建的WebService的服务引用。 br />
在您的SilverLight中,您可以像执行方法一样访问MySQL。
示例:
SilverLight的代码
I have face the exact problem like you.
Using PHP script to connect SilverLight to MySQL is my first approach.
But later, I had discovered a more convenient/easier way, by using C# Web Services.
I have written an article explaining the steps in details with illustrations.
>> Connecting MySQL From SilverLight With Web Services[^]
Simple explanation:
1. You need to create a C# web services.
2. Configure the web service access permission
(by default, web service only allow to be access by localhost)
3. At SilverLight application, add a service reference of the WebService that you have created.
There, at your SilverLight, you access MySQL just as executing a method.
Example:
code at SilverLight
public partial class MainPage : UserControl
{
ServiceReference1.WebService1SoapClient myService;
public MainPage()
{
InitializeComponent();
myService = new ServiceReference1.WebService1SoapClient();
myService.ExecuteScalarCompleted += myService_ExecuteScalarCompleted;
}
void myService_ExecuteScalarCompleted(object sender, ServiceReference1.ExecuteScalarCompletedEventArgs e)
{
MessageBox.Show(e.Result);
}
private void button_ExecuteScalar_Click(object sender, RoutedEventArgs e)
{
myService.ExecuteScalarAsync(textBox1.Text);
}
}
网络服务代码
code at Web Service
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using MySql.Data.MySqlClient;
using System.Data;
namespace MyWebService
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string ExecuteScalar(string sql)
{
string result = "";
string constr = "server=localhost;user=root;pwd=1234;database=member;";
using (MySqlConnection conn = new MySqlConnection(constr))
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = sql;
result = cmd.ExecuteScalar() + "";
conn.Close();
}
return result;
}
}
}
上面让您了解SilverLight如何通过WebServices与MySQL通信。
我上面没有提到过一些细节。
你必须搜索谷歌以下详细信息:
1.如何创建C#Web服务。
2.如何在SilverLight添加Web服务参考
从上面的例子中,您可以看到SilverLight正在访问Web服务:
The above gives you an idea how SilverLight communicate MySQL through WebServices.
There are some details that I didn't mentioned above.
You have to search google on below details:
1. How to create a C# Web Service.
2. How to add a Web Service reference at SilverLight
From the above example, you can see that SilverLight is accessing the Web Service with this:
ServiceReference1.WebService1SoapClient myService;
ServiceReference1.WebService1SoapClient
是Visual Studio自动生成的类。我们只需要找到Web服务位置(例如:http://www.myweb.com/WebServices.asmx),然后就会自动执行数据绑定。
Web Services的文件扩展名为 .asmx
。
ServiceReference1.WebService1SoapClient
is an class automatically generated by Visual Studio. We only need to locate the Web Services location (example: http://www.myweb.com/WebServices.asmx), then data binding will be automatically performed.
Web Services has a file extension of ".asmx
".