locked
Is there any way to write dynamic queries for select, insert, update instead of writing hard coded query in every page in asp.net c# RRS feed

  • Question

  • User703311911 posted

    I am new to asp.net. I just want to know that is there any way to write dynamic queries for select, insert, update etc instead of writing hard coded query in every page in asp.net c# so that we need not to change query in every page if needed. Please guide as i am new in asp.net.

    here is my code for insert. Connection string is imported from helper.cs file.

    public void insertmessages() {

    string query = "insert into emailtable( receiver,username,subject,message)" + "values(@receiver,@username,@subject,@message)";
    
            helper NewDB = new helper(); // db class is instantiated...
            SqlConnection newCon;
    
    
            newCon = NewDB.GetConn();
    
            newCon.Open();
            SqlCommand cmd = new SqlCommand(query, newCon);
    
            cmd.Parameters.AddWithValue("@receiver", SqlDbType.VarChar).Value = receiver1;
            cmd.Parameters.AddWithValue("@username", SqlDbType.VarChar).Value = username1;
            cmd.Parameters.AddWithValue("@subject", SqlDbType.VarChar).Value = subject1;
            cmd.Parameters.AddWithValue("@message", SqlDbType.VarChar).Value = message1;
    
            cmd.ExecuteNonQuery();
    
            newCon.Close();
    }
    Sunday, April 3, 2016 7:45 AM

Answers

All replies