none
SqlDependency class does not support LINQ to SQL query RRS feed

  • Question

  • I am using linq to sql query but the SqlDependency class does not support it.

    how to get rid of this problem?

    here is the code which i am using:

    public void getNames()
        {
            if (!doesUserHavePermission())
                return;
    
            using(DataClassesDataContext Context=new DataClassesDataContext())
            {
                string connectionString = Context.Connection.ConnectionString.ToString();
    
                SqlDependency.Start(connectionString);
                SqlDependency.Stop(connectionString);
    
                List<get_data> populate = (from a in Context.infos
                                           select new get_data()
                                           {
                                               Id = a.id,
                                               Name = a.name,
                                               Address = a.address
                                           }).ToList<get_data>();
    
    
                SqlDependency dep = new SqlDependency(populate); <----- here it underlines 'populate' red.
    
            }
         }

    Monday, December 22, 2014 1:53 AM

Answers

  • For logic, you must remove this statement "SqlDependency.Stop(connectionString);

    " or put it under SqlDependency dep = new SqlDependency(populate);

    -----------------------------------------------

    Hope above help !

    Doanh

    Monday, December 22, 2014 8:42 AM

All replies

  • I am using linq to sql query but the SqlDependency class does not support it.

    how to get rid of this problem?

    here is the code which i am using:

    public void getNames()
        {
            if (!doesUserHavePermission())
                return;
    
            using(DataClassesDataContext Context=new DataClassesDataContext())
            {
                string connectionString = Context.Connection.ConnectionString.ToString();
    
                SqlDependency.Start(connectionString);
                SqlDependency.Stop(connectionString);
    
                List<get_data> populate = (from a in Context.infos
                                           select new get_data()
                                           {
                                               Id = a.id,
                                               Name = a.name,
                                               Address = a.address
                                           }).ToList<get_data>();
    
    
                SqlDependency dep = new SqlDependency(populate); <----- here it underlines 'populate' red.
    
            }
         }

    • Merged by Kristin Xie Tuesday, December 23, 2014 5:46 AM the same issue
    Monday, December 22, 2014 1:04 AM
  • Hi,

    Sqldependency does not have that "List" type as parameter in the constructor.  You need to change what you are going to pass.  see below.

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldependency(v=vs.110).aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 22, 2014 2:30 AM
  • For logic, you must remove this statement "SqlDependency.Stop(connectionString);

    " or put it under SqlDependency dep = new SqlDependency(populate);

    -----------------------------------------------

    Hope above help !

    Doanh

    Monday, December 22, 2014 8:42 AM
  • Hello Haider,

    Here is an example which shows how to use this class:

    void Initialization()
    {
        // Create a dependency connection.
        SqlDependency.Start(connectionString, queueName);
    }
    
    void SomeMethod()
    {
        // Assume connection is an open SqlConnection.
    
        // Create a new SqlCommand object.
        using (SqlCommand command=new SqlCommand(
            "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers", 
            connection))
        {
    
            // Create a dependency and associate it with the SqlCommand.
            SqlDependency dependency=new SqlDependency(command);
            // Maintain the refence in a class member.
    
            // Subscribe to the SqlDependency event.
            dependency.OnChange+=new
               OnChangeEventHandler(OnDependencyChange);
    
            // Execute the command.
            using (SqlDataReader reader = command.ExecuteReader())
            {
                // Process the DataReader.
            }
        }
    }
    
    // Handler method
    void OnDependencyChange(object sender, 
       SqlNotificationEventArgs e )
    {
      // Handle the event (for example, invalidate this cache entry).
    }
    
    void Termination()
    {
        // Release the dependency.
        SqlDependency.Stop(connectionString, queueName);
    }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 23, 2014 2:01 AM
    Moderator