Home > front end >  Give two sql queries in asp.net controller
Give two sql queries in asp.net controller

Time:04-09

This is function from my ASP.NET mvc structure and i want to give two queries in same function.My task is to delete row from 'class' table also when row is deleted from teacher table where id="teacheid". How can i right query for that in this function.

 public void DeleteTeacher(int id)
    {
        //Create an instance of a connection
        MySql.Data.MySqlClient.MySqlConnection Conn = Blog.AccessDatabase();

        //Open the connection between the web server and database
        Conn.Open();

        //Establish a new command (query) for our database
        MySqlCommand cmd = Conn.CreateCommand();

        //SQL QUERY
        cmd.CommandText = "Delete from teachers where teacherid=@id";
        cmd.Parameters.AddWithValue("@id", id);
        /*cmd.CommandText = query;*/
        cmd.Prepare();
        

        cmd.ExecuteNonQuery();

        Conn.Close();


    }

CodePudding user response:

You could create a store procedure that executes both actions. Otherwise you could execute your new query before closing the connection and after you executed the first query.

 public void DeleteTeacher(int id)
    {
        //Create an instance of a connection
        MySql.Data.MySqlClient.MySqlConnection Conn = Blog.AccessDatabase();

        //Open the connection between the web server and database
        Conn.Open();

        //Establish a new command (query) for our database
        MySqlCommand cmd = Conn.CreateCommand();

        //SQL QUERY
        cmd.CommandText = "Delete from teachers where teacherid=@id";
        cmd.Parameters.AddWithValue("@id", id);
        cmd.Prepare();
        cmd.ExecuteNonQuery();

        // EXECUTE YOUR NEW QUERY

        Conn.Close();

    }

CodePudding user response:

I guess you use wrong database. It should be schooldb.sql Try to check ur database again.

CodePudding user response:

try cross checking the database connection or database name.

  • Related