コード例 #1
0
        } // End Sub WriteArray

        public static async System.Threading.Tasks.Task AnyDataReaderToJson(
            System.Data.Common.DbConnection cnn
            , string sql
            , RenderType_t format
            , Microsoft.AspNetCore.Http.HttpContext context
            , System.Text.Encoding encoding
            , object parameters = null
            , System.Data.IDbTransaction transaction = null
            , int?commandTimeout = null
            , System.Data.CommandType?commandType = null
            )
        {
            // DynamicParameters dbArgs = new DynamicParameters();


            using (System.IO.StreamWriter responseWriter = new System.IO.StreamWriter(context.Response.Body, encoding))
            {
                using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                           new Newtonsoft.Json.JsonTextWriter(responseWriter))
                {
                    if (format.HasFlag(RenderType_t.Indented))
                    {
                        jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                    }


                    try
                    {
                        using (System.Data.Common.DbDataReader dr = await cnn.ExecuteDbReaderAsync(sql, parameters, transaction, commandTimeout, commandType))
                        {
                            context.Response.StatusCode  = (int)System.Net.HttpStatusCode.OK;
                            context.Response.ContentType = "application/json; charset=" + encoding.WebName;

                            await jsonWriter.WriteStartObjectAsync();

                            await jsonWriter.WritePropertyNameAsync("tables");

                            await jsonWriter.WriteStartArrayAsync();

                            do
                            {
                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteStartObjectAsync();

                                    await jsonWriter.WritePropertyNameAsync("columns");

                                    if (format.HasFlag(RenderType_t.Columns_Associative))
                                    {
                                        await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                    }
                                    else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                    {
                                        await WriteComplexArray(jsonWriter, dr, format);
                                    }
                                    else // (format.HasFlag(RenderType_t.Array))
                                    {
                                        await WriteArray(jsonWriter, dr);
                                    }
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))


                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WritePropertyNameAsync("rows");
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))

                                await jsonWriter.WriteStartArrayAsync();

                                string[] columns = null;
                                if (format.HasFlag(RenderType_t.DataTable))
                                {
                                    columns = new string[dr.FieldCount];
                                    for (int i = 0; i < dr.FieldCount; i++)
                                    {
                                        columns[i] = dr.GetName(i);
                                    } // Next i
                                }     // End if (format.HasFlag(RenderType_t.DataTable))

                                while (await dr.ReadAsync())
                                {
                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteStartObjectAsync();
                                    }
                                    else
                                    {
                                        await jsonWriter.WriteStartArrayAsync();
                                    }

                                    for (int i = 0; i <= dr.FieldCount - 1; i++)
                                    {
                                        object obj = await dr.GetFieldValueAsync <object>(i);

                                        if (obj == System.DBNull.Value)
                                        {
                                            obj = null;
                                        }

                                        if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                        {
                                            await jsonWriter.WritePropertyNameAsync(columns[i]);
                                        }

                                        await jsonWriter.WriteValueAsync(obj);
                                    } // Next i

                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteEndObjectAsync();
                                    }
                                    else
                                    {
                                        await jsonWriter.WriteEndArrayAsync();
                                    }
                                } // Whend

                                await jsonWriter.WriteEndArrayAsync();

                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteEndObjectAsync();
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))

                                await jsonWriter.FlushAsync();

                                await responseWriter.FlushAsync();
                            } while (await dr.NextResultAsync());

                            await jsonWriter.WriteEndArrayAsync();

                            await jsonWriter.WriteEndObjectAsync();

                            await jsonWriter.FlushAsync();

                            await responseWriter.FlushAsync();
                        } // End Using dr
                    }
                    catch (System.Exception ex)
                    {
                        context.Response.StatusCode  = (int)System.Net.HttpStatusCode.InternalServerError;
                        context.Response.ContentType = "application/json; charset=" + encoding.WebName;

                        await jsonWriter.WriteStartObjectAsync();

                        await jsonWriter.WritePropertyNameAsync("Error");

                        await jsonWriter.WriteStartObjectAsync();

                        await jsonWriter.WritePropertyNameAsync("Message");

                        await jsonWriter.WriteValueAsync(ex.Message);

                        await jsonWriter.WritePropertyNameAsync("StackTrace");

                        await jsonWriter.WriteValueAsync(ex.StackTrace);

                        await jsonWriter.WriteEndObjectAsync();

                        await jsonWriter.WriteEndObjectAsync();
                    }

                    await jsonWriter.FlushAsync();

                    await responseWriter.FlushAsync();

                    await context.Response.CompleteAsync();
                } // End Using jsonWriter
            }     // End Using responseWriter
        }         // End Sub AnyDataReaderToJson
コード例 #2
0
ファイル: __AsJSON.cs プロジェクト: ststeiger/AnySqlWebAdmin
        } // End Sub WriteArray

        public static async System.Threading.Tasks.Task <System.Exception> AsJSON(
            this IDbConnection cnn
            , System.IO.TextWriter output
            , string sql
            , RenderType_t format
            , object param = null
            , IDbTransaction transaction = null
            , int?commandTimeout         = null
            , CommandType?commandType    = null)
        {
            try
            {
                using (System.Data.Common.DbDataReader dr = cnn.ExecuteDbReader(sql, param, transaction, commandTimeout, commandType))
                {
                    using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                               new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output)
                    {
                        if (format.HasFlag(RenderType_t.Indented))
                        {
                            jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                        }


                        // https://stackoverflow.com/questions/15305203/what-to-do-with-errors-when-streaming-the-body-of-an-http-request
                        // https://tools.ietf.org/html/rfc2616#section-14.40
                        // https://stackoverflow.com/questions/50299300/how-to-write-malformed-http-response-to-guarantee-something-akin-to-http-500
                        // https://stackoverflow.com/questions/50298999/write-http-trailer-headers-manually
                        // How do I write a useful trail header to the response that can be displayed well by the browser?
                        // You don’t. Mainstream Web browsers do not care about trailers.
                        // context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                        // context.Response.ContentType = "application/json";


                        await jsonWriter.WriteStartObjectAsync();

                        await jsonWriter.WritePropertyNameAsync("tables");

                        await jsonWriter.WriteStartArrayAsync();

                        do
                        {
                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WriteStartObjectAsync();

                                await jsonWriter.WritePropertyNameAsync("columns");

                                if (format.HasFlag(RenderType_t.Columns_Associative))
                                {
                                    await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                }
                                else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                {
                                    await WriteComplexArray(jsonWriter, dr, format);
                                }
                                else // (format.HasFlag(RenderType_t.Array))
                                {
                                    await WriteArray(jsonWriter, dr);
                                }
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))


                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WritePropertyNameAsync("rows");
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))

                            await jsonWriter.WriteStartArrayAsync();

                            string[] columns = null;
                            if (format.HasFlag(RenderType_t.DataTable))
                            {
                                columns = new string[dr.FieldCount];
                                for (int i = 0; i < dr.FieldCount; i++)
                                {
                                    columns[i] = dr.GetName(i);
                                } // Next i
                            }     // End if (format.HasFlag(RenderType_t.DataTable))

                            while (await dr.ReadAsync())
                            {
                                if (format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteStartObjectAsync();
                                }
                                else
                                {
                                    await jsonWriter.WriteStartArrayAsync();
                                }

                                for (int i = 0; i <= dr.FieldCount - 1; i++)
                                {
                                    object obj = await dr.GetFieldValueAsync <object>(i);

                                    if (obj == System.DBNull.Value)
                                    {
                                        obj = null;
                                    }

                                    if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WritePropertyNameAsync(columns[i]);
                                    }

                                    await jsonWriter.WriteValueAsync(obj);
                                } // Next i

                                if (format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteEndObjectAsync();
                                }
                                else
                                {
                                    await jsonWriter.WriteEndArrayAsync();
                                }
                            } // Whend

                            await jsonWriter.WriteEndArrayAsync();

                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WriteEndObjectAsync();
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))
                        } while (await dr.NextResultAsync());

                        await jsonWriter.WriteEndArrayAsync();

                        await jsonWriter.WriteEndObjectAsync();

                        await jsonWriter.FlushAsync();

                        await output.FlushAsync();
                    } // jsonWriter
                }     // End Using dr
            }         // End Try
            catch (System.Exception ex)
            {
                return(ex);
            } // End Catch

            return(null);
        } // End Task AsJSON
コード例 #3
0
        } // End Sub WriteArray 


        public static async System.Threading.Tasks.Task AnyDataReaderToJson(
            string sql
            , System.Collections.Generic.Dictionary<string, object> pars
            , Microsoft.AspNetCore.Http.HttpContext context
            , RenderType_t format)
        {
            SqlService service = (SqlService) context.RequestServices.GetService(typeof(SqlService));

            using (System.Data.Common.DbConnection con = service.Connection)
            {
                using (System.Data.Common.DbCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = sql;
                    // cmd.CommandText = "SELECT  * FROM T_Benutzer; SELECT * FROM T_Benutzergruppen;";

                    service.AddParameterList(pars, cmd);
                    // cmd.ExecuteNonQueryAsync
                    // cmd.ExecuteReaderAsync
                    // cmd.ExecuteScalarAsync

                    using (System.Data.Common.DbDataReader dr = await cmd.ExecuteReaderAsync(
                        System.Data.CommandBehavior.SequentialAccess
                        | System.Data.CommandBehavior.CloseConnection))
                    {
                        using (System.IO.StreamWriter output = new System.IO.StreamWriter(context.Response.Body))
                        {
                            using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                                new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output)
                            {
                                if (format.HasFlag(RenderType_t.Indented))
                                    jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;


                                context.Response.StatusCode = (int) System.Net.HttpStatusCode.OK;
                                context.Response.ContentType = "application/json";


                                await jsonWriter.WriteStartObjectAsync();

                                await jsonWriter.WritePropertyNameAsync("tables");
                                await jsonWriter.WriteStartArrayAsync();

                                do
                                {
                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteStartObjectAsync();
                                        await jsonWriter.WritePropertyNameAsync("columns");

                                        if (format.HasFlag(RenderType_t.Columns_Associative))
                                        {
                                            await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                        }
                                        else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                        {
                                            await WriteComplexArray(jsonWriter, dr, format);
                                        }
                                        else // (format.HasFlag(RenderType_t.Array))
                                        {
                                            await WriteArray(jsonWriter, dr);
                                        }
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only)) 


                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WritePropertyNameAsync("rows");
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only))
                                    
                                    await jsonWriter.WriteStartArrayAsync();
                                    
                                    string[] columns = null;
                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        columns = new string[dr.FieldCount];
                                        for (int i = 0; i < dr.FieldCount; i++)
                                        {
                                            columns[i] = dr.GetName(i);
                                        } // Next i 
                                    } // End if (format.HasFlag(RenderType_t.DataTable)) 

                                    while (await dr.ReadAsync())
                                    {
                                        if (format.HasFlag(RenderType_t.DataTable))
                                            await jsonWriter.WriteStartObjectAsync();
                                        else
                                            await jsonWriter.WriteStartArrayAsync();

                                        for (int i = 0; i <= dr.FieldCount - 1; i++)
                                        {
                                            object obj = await dr.GetFieldValueAsync<object>(i);
                                            if (obj == System.DBNull.Value)
                                                obj = null;

                                            if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                            {
                                                await jsonWriter.WritePropertyNameAsync(columns[i]);
                                            }

                                            await jsonWriter.WriteValueAsync(obj);
                                        } // Next i 

                                        if (format.HasFlag(RenderType_t.DataTable))
                                            await jsonWriter.WriteEndObjectAsync();
                                        else
                                            await jsonWriter.WriteEndArrayAsync();
                                    } // Whend 

                                    await jsonWriter.WriteEndArrayAsync();

                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteEndObjectAsync();
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only)) 
                                } while (await dr.NextResultAsync());

                                await jsonWriter.WriteEndArrayAsync();
                                await jsonWriter.WriteEndObjectAsync();

                                await jsonWriter.FlushAsync();
                                await output.FlushAsync();
                            } // jsonWriter 
                        } // output 
                    } // dr 
                } // End Using cmd 

                if (con.State != System.Data.ConnectionState.Closed)
                    con.Close();
            } // con 
        } // End Sub WriteArray 
コード例 #4
0
        } // End Sub WriteArray

        public static async System.Threading.Tasks.Task AnyDataReaderToJson(
            string sql
            , System.Collections.Generic.Dictionary <string, object> pars
            , SqlService service
            , WebSocketTextWriter output
            , RenderType_t format)
        {
            using (System.Data.Common.DbConnection con = service.Connection)
            {
                using (System.Data.Common.DbCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = sql;

                    service.AddParameterList(pars, cmd);
                    // cmd.ExecuteNonQueryAsync
                    // cmd.ExecuteReaderAsync
                    // cmd.ExecuteScalarAsync

                    using (System.Data.Common.DbDataReader dr = await cmd.ExecuteReaderAsync(
                               System.Data.CommandBehavior.SequentialAccess
                               | System.Data.CommandBehavior.CloseConnection))
                    {
                        using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                                   new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output)
                        {
                            if (format.HasFlag(RenderType_t.Indented))
                            {
                                jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                            }


                            // context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                            // context.Response.ContentType = "application/json";


                            await jsonWriter.WriteStartObjectAsync();

                            await jsonWriter.WritePropertyNameAsync("tables");

                            await jsonWriter.WriteStartArrayAsync();

                            do
                            {
                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteStartObjectAsync();

                                    await jsonWriter.WritePropertyNameAsync("columns");

                                    if (format.HasFlag(RenderType_t.Columns_Associative))
                                    {
                                        await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                    }
                                    else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                    {
                                        await WriteComplexArray(jsonWriter, dr, format);
                                    }
                                    else // (format.HasFlag(RenderType_t.Array))
                                    {
                                        await WriteArray(jsonWriter, dr);
                                    }
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))



                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WritePropertyNameAsync("rows");
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))


                                await output.TransmitAsync();

                                await jsonWriter.WriteStartArrayAsync();

                                if (dr.HasRows)
                                {
                                    string[] columns = null;
                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        columns = new string[dr.FieldCount];
                                        for (int i = 0; i < dr.FieldCount; i++)
                                        {
                                            columns[i] = dr.GetName(i);
                                        } // Next i
                                    }     // End if (format.HasFlag(RenderType_t.DataTable))

                                    int rowCount = 0;

                                    while (await dr.ReadAsync())
                                    {
                                        if (format.HasFlag(RenderType_t.DataTable))
                                        {
                                            await jsonWriter.WriteStartObjectAsync();
                                        }
                                        else
                                        {
                                            await jsonWriter.WriteStartArrayAsync();
                                        }

                                        for (int i = 0; i <= dr.FieldCount - 1; i++)
                                        {
                                            object obj = await dr.GetFieldValueAsync <object>(i);

                                            if (obj == System.DBNull.Value)
                                            {
                                                obj = null;
                                            }

                                            if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                            {
                                                await jsonWriter.WritePropertyNameAsync(columns[i]);
                                            }

                                            await jsonWriter.WriteValueAsync(obj);
                                        } // Next i

                                        if (format.HasFlag(RenderType_t.DataTable))
                                        {
                                            await jsonWriter.WriteEndObjectAsync();
                                        }
                                        else
                                        {
                                            await jsonWriter.WriteEndArrayAsync();
                                        }

                                        rowCount++;

                                        await jsonWriter.FlushAsync();

                                        if (rowCount % 5 == 0)
                                        {
                                            await output.TransmitAsync();
                                        }
                                        else
                                        {
                                            await output.FlushAsync();
                                        }
                                    } // Whend

                                    await jsonWriter.FlushAsync();

                                    await output.TransmitAsync();
                                } // End if (dr.HasRows)

                                await jsonWriter.WriteEndArrayAsync();

                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteEndObjectAsync();
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))
                            } while (await dr.NextResultAsync());

                            await jsonWriter.WriteEndArrayAsync();

                            await jsonWriter.WriteEndObjectAsync();

                            try
                            {
                                await jsonWriter.FlushAsync();

                                await output.TransmitAsync();
                            }
                            catch (System.Exception ex)
                            {
                                System.Console.WriteLine(ex.Message);
                            }
                        } // jsonWriter
                    }     // dr
                }         // End Using cmd

                if (con.State != System.Data.ConnectionState.Closed)
                {
                    con.Close();
                }
            } // con
        }     // End Sub WriteArray