locked
plz help me LinQ to ado.net RRS feed

  • Question

  • User50679144 posted

    hi everybody i have something trouble

    this is my code i cant delete designation rows

    please help me thx

    private void button3_Click(object sender, EventArgs e)
    {
    var q = (from n in db.LoginTest
    select n).FirstOrDefault();

    db.LoginTest.Remove(q);
    db.SaveChanges();
    Refreshdata();
    }

    Wednesday, August 26, 2015 8:18 AM

Answers

  • User281315223 posted

    So you want to take this existing LINQ code and translate it to ADO.NET?

    Doing something like that might look like the following :

    private void button3_Click(object sender, EventArgs e)
    {
         // Open your data connection
         using(var connection = new SqlConnection("Your Connection String"))
         {
                // Build your query (delete the first element from a table)
                var query = "DELETE TOP 1 FROM YourLoginTestTable"
    
                // Build a command to execute your query 
                using(var command = new SqlCommand(query, connection))
                {
                      // Open your connection
                      connection.Open();
                      // Execute the query
                      command.ExecuteNonQuery();
    
                      // Refresh your contents here
                      Refreshdata();
                }
         }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 26, 2015 8:25 AM
  • User281315223 posted

    No problem.

    You would just need to build a WHERE clause :

    private void button3_Click(object sender, EventArgs e)
    {
         // Define the ID that you want to delete
         var id = "Your ID to Delete";
    
         // Open your data connection
         using(var connection = new SqlConnection("Your Connection String"))
         {
                // Build your query (delete the first element from a table)
                var query = "DELETE TOP 1 FROM YourLoginTestTable WHERE Id = @Id"
    
                // Build a command to execute your query 
                using(var command = new SqlCommand(query, connection))
                {
                      // Open your connection
                      connection.Open();
    
                      // Add your parameter to delete
                      command.Parameters.AddWithValue("@Id",id);
    
                      // Execute the query
                      command.ExecuteNonQuery();
    
                      // Refresh your contents here
                      Refreshdata();
                }
         }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 26, 2015 9:36 AM

All replies

  • User281315223 posted

    So you want to take this existing LINQ code and translate it to ADO.NET?

    Doing something like that might look like the following :

    private void button3_Click(object sender, EventArgs e)
    {
         // Open your data connection
         using(var connection = new SqlConnection("Your Connection String"))
         {
                // Build your query (delete the first element from a table)
                var query = "DELETE TOP 1 FROM YourLoginTestTable"
    
                // Build a command to execute your query 
                using(var command = new SqlCommand(query, connection))
                {
                      // Open your connection
                      connection.Open();
                      // Execute the query
                      command.ExecuteNonQuery();
    
                      // Refresh your contents here
                      Refreshdata();
                }
         }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 26, 2015 8:25 AM
  • User50679144 posted

    u are great but my english is so poor

    i want delete designation rows and user LinQ to entity gammar with datagridview

    this is my code and this is windowform

    DatabaseDBContext db = new DatabaseDBContext();

    private void button3_Click(object sender, EventArgs e)
    {
    var q = (from n in db.LoginTest
    where n.Id ==<===============???? 
    select n).FirstOrDefault();

    db.LoginTest.Remove(q);
    db.SaveChanges();
    Refreshdata();
    }

    Wednesday, August 26, 2015 8:43 AM
  • User281315223 posted

    No problem.

    You would just need to build a WHERE clause :

    private void button3_Click(object sender, EventArgs e)
    {
         // Define the ID that you want to delete
         var id = "Your ID to Delete";
    
         // Open your data connection
         using(var connection = new SqlConnection("Your Connection String"))
         {
                // Build your query (delete the first element from a table)
                var query = "DELETE TOP 1 FROM YourLoginTestTable WHERE Id = @Id"
    
                // Build a command to execute your query 
                using(var command = new SqlCommand(query, connection))
                {
                      // Open your connection
                      connection.Open();
    
                      // Add your parameter to delete
                      command.Parameters.AddWithValue("@Id",id);
    
                      // Execute the query
                      command.ExecuteNonQuery();
    
                      // Refresh your contents here
                      Refreshdata();
                }
         }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 26, 2015 9:36 AM
  • User50679144 posted

    thx you verymuch

    Wednesday, August 26, 2015 10:15 PM