example for Database.SQL method (.net library)

example for Database.SQL method (.net library)

montoyammontoyam Posts: 568Questions: 136Answers: 5

I found forum posts talking about executing a sql statement:
https://editor.datatables.net/docs/1.7.3/net/html/71a95e9f-39f9-0573-37f7-37396382e934.htm

but I am not able to find any full examples of how to do this. I am looking to execute an update command with some passed parameters.

I have used code like this to call a stored procedure (not using the Editor libraries), but if I could just execute a raw sql statement then I don't need to create a stored procedure.

    public class RequestHeaderApproveSimpleController : ApiController
    {
        [HttpGet]
        [HttpPost]
        public string RequestHeaderApproveSimple(int headerID, string userID)
        {
            var request = HttpContext.Current.Request;
            var settings = System.Configuration.ConfigurationManager.ConnectionStrings["msSqlDW"];
            var data = request.QueryString;
            string strCon = settings.ConnectionString;
            SqlConnection DbConnection = new SqlConnection(strCon);
            DbConnection.Open();

            SqlCommand command = new SqlCommand("UserRequests.proc_RequestHeader_Approve", DbConnection);
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@requestID", headerID));
            command.Parameters.Add(new SqlParameter("@userID", userID));

            try
            {
                int i = command.ExecuteNonQuery();
                DbConnection.Close();
                return "1";
            }
            catch (Exception ex)
            {
                DbConnection.Close();
                return "* " + ex.ToString();
            }
        }
    }
Sign In or Register to comment.