Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone,

I have created a front end for my Silverlight Application and now starting the back end coding. I have been through hundreds of tutorials on linking the application to a database but I have had no success yet :(

Please can you suggest a way for me to implement this... All i need to do is link the app to the database and do some coding for my CRUD statements. The application basically has a login page which opens up to a form where the user adds details to the database.

Thanks in advance,
Kalvin
Posted

Silverlight cannot connect directly to database servers, you have to connect via web services or WCF etc.

If you feel adventurous you could try to compile the MySql drivers on Silverlight.
 
Share this answer
 
Comments
Kalvinp 17-Sep-11 6:17am    
I have tried using WCF Data services.... The tutorial however only showed how to retrieve data from the database and display in a grid. I know that you attach a .svc file to the project which holds all the database connection details and methods that bring back data but i can't figure out how to add more methods to the .svc file and call them from the app....

This is the code with the method to retrieve from the database:

//namespace SilverlightWcfService
namespace SilverlightWCFTutorial.Web
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "DatabaseService" in code, svc and config file together.
public class DatabaseService : IDatabaseService
{
string myConnectionString = "server=localhost; user id=root; password=password; database=silverlight1";
//string myConnectionString = "someConnectionString";
public string GetData(int value)
{
return string.Format("You entered {0}", value);
}
public List<string> GetNames()
{
List<string> list = new List<string>();
List<string> list2 = new List<string>();

using (MySqlConnection cn = new MySqlConnection(myConnectionString))
{


using (MySqlCommand cmd = cn.CreateCommand())
{
cmd.CommandText = "SELECT ID, FirstName, lastName, Age FROM person";
cmd.CommandType = CommandType.Text;

cn.Open();

using (MySqlDataReader dr = cmd.ExecuteReader())
{
string id;
string fname = null;
string lname = null;
string age;
while (dr.Read())
{
id = dr["ID"].ToString();
fname = dr["FirstName"].ToString();
lname = dr["LastName"].ToString();
age = dr["Age"].ToString();
list.Add(id);//(dr.GetString(0));
list2.Add(fname);
list.Add(lname);
list.Add(age);
}
}
}
}

return list;
}
}
}

===============================================================

How do i add another method to update or add information to the same database?... I am not sure as to where to place the code in this .svc file...
Mehdi Gholam 17-Sep-11 7:02am    
Add another method say SaveName(string name) etc and put your update sql code in that method and call it from silverlight.
Kalvinp 17-Sep-11 8:29am    
ok thats done. And so we hit my second problem. Now that we have more than one method in the scv file, how do call a specific method from silverlight?..
You can do this with web services. Read more:
Connecting MySQL From SilverLight With Web Services[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900