SQL Connection Windows Phone VS2015-VBForums
Results 1 to 7 of 7

Thread: SQL Connection Windows Phone VS2015

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    167

    SQL Connection Windows Phone VS2015

    Morning Guys,

    Last week I installed Visual Studio 2015, its a bit of a learning curve and I feel like a fish out of water at the moment...

    I'm wanting to write an app for my windows 10 phone that links to my SQL database but I can't for the life of me work how to import the SQL namespace, ie in VB 2010 you'd simply:

    Code:
    Imports System.Data.SqlClient
    with something like :

    Code:
    Dim csb As New SqlConnectionStringBuilder
    Dim conn As SqlConnection
    csb.DataSource = "DBServer"
    csb.InitialCatalog = "database"
    csb.UserID = "Username"
    csb.Password = "Password"
    conn = New SqlConnection(csb.ConnectionString)
    then using a SQLdataAdaptor pass your query and your data is returned...

    I've read a few 'how tos' on the web but most of them just seem to create the sql connection

    Code:
    Dim conn As New SqlConnection(str)
    Am I missing something in VS 2015, is it achieved in a completely different way...?

    Thanks

    Dave

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,004

    Re: SQL Connection Windows Phone VS2015

    It's got nothing to do with VS 2015 specifically but rather the type of application you're creating. If you create a Windows Forms application project or an ASP.NET project then you can connect to a SQL Server database just as you've shown in VS 2015, just like earlier versions.

    I've done very little development for Windows Phone or Windows 10 Mobile but, when I did, I think the only data access technology that could be used directly was LINQ to SQL. Even that may not be available in a UWP app project.

    I would expect that connecting to a SQL Server database would require that you create a web service and publish that to a web server somewhere, then make calls to that service from your app. If you want to create such a web service then you'd probably use Web API, which is basically MVC without a UI, and in that you can connect to the database just as you've shown.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,004

    Re: SQL Connection Windows Phone VS2015

    If I'm wrong and you can use ADO.NET directly in a UWP app then you would have to first make sure that you've referenced System.Data.dll, which is where all the SqlClient types are defined. When you think about it though, it doesn't really make sense to do that because your phone would have to be connected to the same network as your SQL Server in order to be able to see it. That's why a web service makes sense: because you can always see the web, no matter how you're connected to the internet.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    167

    Re: SQL Connection Windows Phone VS2015

    Hiya,

    Thanks for your reply. Sorry, yes creating a UWP app. I've just been trying a couple of things, it doesn't look like I can connect directly to SQL. Thinking about it logically what you've said would make perfect sense, why would MS make it available when its designed for a mobile device?!

    Your idea of using a web service makes perfect sense, unfortunatly I don't quite understand how this world work....

    So the mobile app would request data from a web server & the web server would request data from the SQL Server...? Would the web server effectlvely have stored procedures that the mobile app would use? Any pointers as to how to start writing the web service...?

    Sorry for all the questions...

    Thanks

    Dave

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,103

    Re: SQL Connection Windows Phone VS2015

    Quote Originally Posted by QuattroDave View Post
    So the mobile app would request data from a web server & the web server would request data from the SQL Server...? Would the web server effectlvely have stored procedures that the mobile app would use?
    Yes, the web service would provide various functions (that can be called from the phone), and those functions call stored procedures and return data etc as appropriate.

    Any pointers as to how to start writing the web service...?
    There are links to 'old' examples in our Database Development FAQs/Tutorials (at the top of the Database Development forum), but I would recommend following the earlier suggestion of Web API... unfortunately I don't know any good sources for that, but a web search should find them.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,004

    Re: SQL Connection Windows Phone VS2015

    Quote Originally Posted by QuattroDave View Post
    Your idea of using a web service makes perfect sense, unfortunatly I don't quite understand how this world work....

    So the mobile app would request data from a web server & the web server would request data from the SQL Server...? Would the web server effectlvely have stored procedures that the mobile app would use? Any pointers as to how to start writing the web service...?
    Like I said, you can think of a web service as a web site without a UI. Whatever operations a web site would perform, including data access, a web service can perform in exactly the same way. The difference is that, while the web site generates HTML containing the appropriate data that is then sent to the client, i.e. the browser, a web service sends the data itself to the client, which can be any application, to then do with whatever it wants.

    As I said, Web API is probably the way to go .NET developers these days. You can start learning about Web API with ASP.NET or ASP.NET Core here:

    https://www.asp.net/learn
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    167

    Re: SQL Connection Windows Phone VS2015

    Thanks for you advice guys, I've started investigating how to write a Web API using the video here:

    https://www.asp.net/web-api/videos/g...aspnet-web-api

    I'm struggeling to understand the security aspect of this, the only way I could get the example to work was to change <Authorize> to <Allow Anonymous> in the values controller.

    From reading up on the different ways to authenticate I think I want to authenticate with a SQL Server Database but really confused as to how this would be achieved. Also if i'm using a mobile app, how would I send a username & password to the ASP Web App...?

    Thanks

    Dave
    Last edited by QuattroDave; Nov 9th, 2016 at 04:49 AM. Reason: typo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.