Jeff Sanders Technical Blog

I am a Microsoft employee that has worked on all aspects of the Web Stack for a long time. I hope these blogs are useful to you! Use this information at your own risk.


<< Go Back

Attaching To A Windows Azure Mobile Services Database In Asp Net

- 12 Aug 2013

The preferred way to talk to the database you are using for you Windows Azure Mobile Services is through the REST APIs and by using the Clients we supply for you.  The reason you should go through the REST APIs is to ensure you are hitting the Scripts you defined in your Mobile Service.  I had a case where someone wanted to directly connect to the SQL Azure database in the backend to list data in a grid view.  The call would fail with this error: “invalid object name‘tablename’” where ‘tablename’ was the name of the table in the SQL Azure database.

The fix was very simple if you understand how database tables are created for Mobile Services.   Have you ever wondered for instance how you can have several Mobile Services with ‘TodoItem’ tables in them and they are all hosted in the same SQL Azure database?  The reason this works is because we use the schema property of the database to scope the table to the Mobile Service.  If I have two mobile services ‘JeffMobileFirst’ and ‘JeffMobileSecond’ both with a ‘TodoItem’ table and look in the SQL Azure database you will see tables ‘JeffMobileFirst.TodoItem’ and ‘JeffMobileSecond.TodoItem’.  To access the database table of either simple ensure you include the schema along with the table name.  For example:

string sql = “select * from JeffMobileFirst.TodoItem”;

SqlConnection connection = new SqlConnection(@”Server=tcp:x0etablf0pp.database.windows.net,1433;Database=jsanders_db;User <ID=dbadmin@x0etablf0pp;Password=mypassword;Trusted_Connection=False;Encrypt=True;Connection> Timeout=30;”);

 

Let me know if this helped you!

<< Go Back