none
Using LINQ in Device Application RRS feed

  • Question

  • I have a VB2008 Device Application containing a SQL CE database (software.sdf). I used SQLMetal to create a Software.dbml. The dbml opens in designer view.

    I tried the following code, but it gives a "Software not defined error" on the second DIM line.

     

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim strCON As String = "Data Source=|DataDirectory|Software.sdf"
      Dim DB As New software(strCON)
    End Sub

    Wednesday, October 7, 2009 5:11 PM

Answers

  • When you say you are writing a device application do you mean you are targetting .NET Compact Edition.

    If so LINQ to SQL is not available on that platform.

    [)amien
    • Marked as answer by AirForce9797 Friday, October 16, 2009 11:12 AM
    Thursday, October 15, 2009 3:17 PM
    Moderator

All replies

  • Hi,

     

    To use LINQ to SQL on SQLCE, we need to pass a SqlCeConnection to the DataContext constructor. 

    ==================================================================
        Dim cs As String = “Data Source=|DataDirectory|Software.sdf”

        Using conn As SqlCeConnection = New SqlCeConnection(cs)

            Dim db As New MyDataContext(conn)

        End Using
    ==================================================================

     

    Please note:  There are some limitations when using LINQ to SQL on SQLCE, like the limited support on connection pooling.  To improve the connection performance, there is a workaround here:

    http://blogs.msdn.com/matt/archive/2008/09/09/sql-ce-3-5-with-linq-to-sql.aspx
    http://blogs.msdn.com/matt/archive/2008/09/26/sql-ce-3-5-with-linq-to-sql-revisited.aspx

     

     

    If you have any problems, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, October 8, 2009 2:50 AM
    Moderator
  • Hi,

     

    I am writing to check the status of the issue on your side.  Would you mind letting me know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, October 12, 2009 12:38 PM
    Moderator
  • Sorry it's take me a while to reply. I've been on another project.

    Do I need to create, in code, a "MyDataContext"? I also used SQLMetal to create a Software.DBML. Can I use it?
    Monday, October 12, 2009 3:46 PM
  • Hi,

    Do you mean could we use SQLMetal to generate DataContext codes?  Yes, it can help us to generate LINQ to SQL DataContext, data classes codes. 

    Any further questions, please feel free to let me know.


    Have a nice day!


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, October 12, 2009 4:29 PM
    Moderator
  • Using the following I code I get a "Type 'SoftwareData' is not defined" error on the DIM DB line.

    In my project I have a Form1, SoftwareData.dbml (Created by SQLMetal), Software.xsd, and Software.sdf

    Imports

     

    System
    Imports System.Linq
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Data.SqlServerCe

    Class

     

    Form1

     

     

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim cs As String = "Data Source=|DataDirectory|SoftwareData.sdf"
            Using CONN As SqlCeConnection = New SqlCeConnection(cs)
                Dim DB As New SoftwareData(CONN)
            End Using
        End Sub
    End
    Class

    Monday, October 12, 2009 5:57 PM
  • Hi,

     

    The .dbml and the certain generated codes (.cs or .vb) cannot be generated together with SqlMetal.exe.  We can first use such commands to generate the .dbml and then the corresponding VB.NET codes:

    =======================================================================
    SqlMetal /dbml:d:\SoftwareData.dbml d:\SoftwareData.sdf

     

    SqlMetal /code:d:\SoftwareData.vb d:\SoftwareData.sdf
    =======================================================================

    If we add both the file SoftwareData.dbml into your VB.NET project.  By default, Visual Studio will use custom tool MSLinqToSQLGenerator to auto-generate source codes named SoftwareData.Designer.vb of the SoftwareData.dbml.   Then if you want to use the source code generated by SqlMetal, we can remove the MSLinqToSQLGenerator in the .dbml file properties window and add the SoftwareData.vb file.  Otherwise, there will be duplicate definition for the DataContext and data classes.  

     

    Then we can use the SqlCeConnection to initialize the DataContext.  The default name of the DataContext should be the same as the .sdf file name.  Also, we need to pay attention if there is some namespace on the DataContext class.  The definition of the DataContext should be in the SoftwareData.vb or SoftwareData.Designer.vb. 

     

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, October 13, 2009 8:41 AM
    Moderator
  • I'm going to be In and out for the next week and then gone until the 7th of december so when I returnand get back to this project I'll let you know how it works out.  I used a GUI to run SQLMetal and created the dbml and vb fles and added them to the project. Thanks for the help!! When I get back I'll continue with the reply you posted above and fight my way through the 40 errors that I now have.

    Can I email you the project as it currently exists?
    Wednesday, October 14, 2009 11:26 AM
  • Hi,

    Yes, of course.  My email address is v-micsun @ microsoft.com. 

    For the 40 errors, I suspect that’s because the duplicate declarations for the DataContext and data classes because the file SoftwareData.Designer.vb and SoftwareData.vb both have the DataContext class and the other data classes definitions. 

    Please follow the methods in my last post.  If you want to use Visual Studio to auto generate the source codes, please remove the SoftwareData.vb which is generated by SqlMetal.exe. 

    Or if you want to use the SoftwareData.vb, please remove the value MSLinqToSQLGenerator in the properties window of the SoftwareData.dbml.  Then the auto-generated file named SoftwareData.Designer.vb will be removed automatically.

    Hope I explain the problem clearly.  J


    Have a nice day!


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, October 14, 2009 12:09 PM
    Moderator
  • When I select the SoftwareData.dbml, both the "Custom Tool" and "Custom Tool Namespace" properties are blank.

    I'll zip and email the project in a few minutes. Thanks for the help. I'll check when I return.
    Thursday, October 15, 2009 11:42 AM
  • When you say you are writing a device application do you mean you are targetting .NET Compact Edition.

    If so LINQ to SQL is not available on that platform.

    [)amien
    • Marked as answer by AirForce9797 Friday, October 16, 2009 11:12 AM
    Thursday, October 15, 2009 3:17 PM
    Moderator
  •  Hi,

     

    Damien is right.   Based on the project that you sent me, it is targeting the .NET Compact Framework on Windows Mobile platform.  As Damien said, LINQ to SQL is not available on this platform. 

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, October 16, 2009 7:27 AM
    Moderator
  • Thanks. I was afraid that might be the case.

    Perhaps in VB2010.......
    Friday, October 16, 2009 11:13 AM