none
LINQ to SQL RRS feed

  • Question

  • I am a newbie at LINQ and would appreciate a little guidance into how to use the DataContxt.

    I created a datacontext like this

     Dim conn As String = "Data Source=.\sqlexpress;Initial Catalog=trips;Integrated Security=SSPI; "
        Dim db As System.Data.Linq.DataContext = New System.Data.Linq.DataContext(conn)
        db.Connection.Open()
    

    I have only one table in this database which is named TripSheet.  It contains a number of columns which are all text values.

    All the examples I have seen have shown that to access the table, all I need is a simple

    db.trips.InsertOnSubmit(<class>)

    when I try that, VS says that trips is not a member of db.

    How do I make the associate between the actual table in the database to the db object so that my error goes away?

    BTW, reading MSDN for examples doesn't help me at all.   I really need a real person to help me understand how all this LINQ stuff works.

    Thanks all for answers received.

    Friday, August 12, 2011 2:43 PM

Answers

  • Hi Deucy;

    You state the following :

    created a datacontext like this
     
    Dim conn As String = "Data Source=.\sqlexpress;Initial Catalog=trips;Integrated Security=SSPI; "
    Dim db As System.Data.Linq.DataContext = New System.Data.Linq.DataContext(conn)
    db.Connection.Open()

    Did you add a "Linq to SQL Classes" object by going to Solution Explorer and right clicking on the project name and selecting, Add => New Item, from the context menu then and then selecting "Linq to SQL Classes". Because if you did the name given to the DataContext class would have been DataClass1DataContext if you did not change the name at the bottom of the Add New Item dialog box to something else in which case the name of the DataContext class would have been SomethingelseDataContext. So I am not sure you created the data context class correctly.

    Assuming you did create the DataContext correctly you do not need to explicitly give a connection string because the DataContext will use the connection string given to the database which you used to create the DataContext class when you dragged the data table from the Server Explorer on to the DBML design surface which opened up after clicking on the OK button when creating the "Linq to SQL Classes" above. The only time you need to give a connection string is if you wanted to point to a different SQL server. Also you do not need to open a db connection Linq to SQL will take care of that for you but if you do the open command make sure to close because if Linq to SQL sees the connection is open it will NOT close when it is done.

    You stated, "db.trips.InsertOnSubmit(<class>)", you only use the InsertOnSubmit to add new records to the data context which will be sent to the server on an execution of save command.

    How to: Add LINQ to SQL Classes to a Project (O/R Designer)

    Please let me know where you are at so that I may help you along.

    The following should work once you have created the Linq to SQL Classes:

    ' Using the default data context class name and connection string
    Dim db As New DataClass1DataContext()

    Dim query = From t in db.TripSheets _
                Select t
               
    For Each trip in query
        ' Access the trip record here
    Next           

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, August 12, 2011 6:12 PM