Gaurav Raj

Gaurav Raj

  • NA
  • 475
  • 83.4k

how can convert this code into linq format

Oct 25 2018 1:24 AM
  1. //Here we will add a function for register notification (will add sql dependency)  
  2.  public void RegisterNotification(DateTime currentTime)  
  3.  {  
  4.  string conStr = ConfigurationManager.ConnectionStrings["sqlConString"].ConnectionString;  
  5.  string sqlCommand = @"SELECT [ContactID],[ContactName],[ContactNo] from [dbo].[Contacts] where [AddedOn] > @AddedOn";  
  6.  //you can notice here I have added table name like this [dbo].[Contacts] with [dbo], its mendatory when you use Sql Dependency  
  7.  using (SqlConnection con = new SqlConnection(conStr))  
  8.  {  
  9.  SqlCommand cmd = new SqlCommand(sqlCommand, con);  
  10.                 cmd.Parameters.AddWithValue("@AddedOn", currentTime);  
  11.  if (con.State != System.Data.ConnectionState.Open)  
  12.  {  
  13.                     con.Open();  
  14.  }  
  15.                 cmd.Notification = null;  
  16.  SqlDependency sqlDep = new SqlDependency(cmd);  
  17.                 sqlDep.OnChange += sqlDep_OnChange;  
  18.  //we must have to execute the command here  
  19.  using (SqlDataReader reader = cmd.ExecuteReader())  
  20.  {  
  21.  // nothing need to add here now  
  22.  }  
  23.  }  

Answers (4)