Ejemplo n.º 1
0
 /// <summary>
 /// Use only for select where (a) return value(s) is/are expected and/or required
 /// </summary>
 /// <typeparam name="T"> Expected datacontext model return type, example: DataContext.User</typeparam>
 /// <param name="context"> The DataContext (YourDataModel) instance to be used in the transaction </param>
 /// <param name="action"> Linq to Entities action to perform</param>
 /// <returns> Returns an object that can be implicitly casted to List of T where T is the expected return type. Example: List of DataContext.User</returns>
 internal List <T> ExecuteSelect <T>(YourDataModel context, SqlCommandDelegate action)
 {
     using (context)
     {
         var retVal = action(); return(((ObjectQuery <T>)retVal).ToList());
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Use for updates and inserts where no return value is expected or required
 /// </summary>
 /// <param name="context"> The DataContext (YourDataModel) instance to be used in the transaction </param>
 /// <param name="action"> Linq to Entities action to perform</param>
 internal void ExecuteInsertOrUpdate(YourDataModel context, SqlCommandDelegate action)
 {
     using (context)
     {
         using (var transaction = context.BeginTransaction())
         {
             try
             { action(); context.SaveChanges(); transaction.Commit(); }
             catch (Exception)
             { transaction.Rollback(); throw; }
         }
     }
 }
    public void ProcessRequest(HttpContext context)
    {
        String json = String.Empty;

        // you have sent JSON to the server
        // read it into a string via the input stream
        using (StreamReader rd = new StreamReader(context.Request.InputStream))
        {
            json = rd.ReadToEnd();
        }

        // create an instance of YourDataModel from the
        // json sent to this handler
        YourDataModel data = null;
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(YourDataModel));

        using (MemoryStream ms = new MemoryStream())
        {
            byte[] utf8Bytes = Encoding.UTF8.GetBytes(json);
            ms.Write(utf8Bytes, 0, utf8Bytes.Length);
            ms.Position = 0;
            data        = serializer.ReadObject(ms) as YourDataModel;
        }

        // update the DB and
        // send back a JSON response
        int rowsUpdated = 0;

        using (SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["dboCao"].ConnectionString))
        {
            c.Open();

            String sql = @"
                    INSERT INTO TestTable 
                        (Column1, Column2) 
                      VALUES 
                        (@column1, @column2);";

            using (SqlCommand cmd = new SqlCommand(sql, c))
            {
                cmd.Parameters.Add("@column1", SqlDbType.VarChar, 50).Value = data.Col1;
                cmd.Parameters.Add("@column2", SqlDbType.VarChar, 50).Value = data.Column2;
                rowsUpdated = cmd.ExecuteNonQuery();
            }
        }

        context.Response.ContentType = "application/json";
        context.Response.Write("{ \"rows_updated\": " + rowsUpdated + " }");
    }
Ejemplo n.º 4
0
 public override JsonResult mustOverrideMethod([FromBody] YourDataModel yourDataModel)
 {
     //You can now use yourDataModel variable here.
     youDataModel.DoAnythingYouLike etc...
 }