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
{
[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;
}
}
}
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
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
".