在数据读取器问题上感到困惑和卡住SQL Server / .NET

gblwokeq  于 2023-01-08  发布在  SQL Server
关注(0)|答案(2)|浏览(128)

我试图在SQL Server指标的通用网页上工作,这些可以从查询/存储过程等,这些可能是在不同的数据库和表,我希望他们在一个漂亮的格式。
假设我有一个如下所示的URL

localhost:0000/Metric/framework?database=db1&table=t1&records=100
OR localhost:0000/Metric/framework?database=db1&table=t2&records=100
OR localhost:0000/Metric/framework?database=db2&table=t9&records=10
  • (完成)打开网页-代码从URL获取数据库/表/recordCount(动态)
  • (完成)创建到SQL的连接
  • (完成)运行查询或存储过程
  • (DONE)将返回的所有字段类型转换为TYPE STRING,这样我就不必担心将模型与SQL Server表匹配的麻烦。
  • (完成)然后显示查询/存储过程的结果
  • (完成)我甚至不需要在HTML方面做任何事情,这就是为什么我在模型中有NAME和VALUE,这样我就可以从模型中创建表格标题。

我试图尽可能简单地做,因为它只是几个脚本,我希望能够通过网页查询,这样我就可以点击一个URL,并有指标在我面前几秒钟,而不必登录到SQL运行脚本,更改数据库等.
我的工作方式是:我只是查看了一下我想要使用的字段的数量,然后将这些新行添加到模型_0 _1 _2 _3 _4 _5 _6 _7 _8 _9等,但是将类型X转换为String时出现了一些棘手的问题,对于10个字段,我必须有10个这样的部分。
示例网页1

1. int    ID           Casts to String
1. string Description  Casts to String
1. date   "2022-12-01" Casts to String

这将是_0 _1 _2字段模型
示例网页2

1. int      ID            Casts to String 
1. currency Price         Casts to String
1. currency Qty           Casts to String
1. date     "2022-12-01"  Casts to String
1. bool     Yes           Casts to String

这将是_0_1_2_3_4场模型
一切正常,我可以为返回的字段创建一个模型,我可以将所有类型更改为String,将它们放入模型中,并在单独的页面上显示它们,所以这是一个胜利。
但一点也不优雅。
棘手的部分,我怎样才能使它有最小的转换编码?
我想迭代它们,但一直无法做到。
任何帮助都将是巨大的,非常感谢。

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.VisualBasic.FileIO;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace Metric_Tracker.Pages.Metric
{
    public class FrameworkModel : PageModel
    {
        public List<frameworkInfo> frameworks = new List<frameworkInfo>();
  
        public void OnGet()
        {
            try
            {            
                string database = HttpContext.Request.Query["database"].ToString();

                if (database == null || database == String.Empty)
                {
                    database = "database";
                }

                string table = HttpContext.Request.Query["table"].ToString();

                if (table == null || table == String.Empty)
                {
                    table = "table";
                }
                tablename = table;

                string records = HttpContext.Request.Query["records"].ToString();

                if (records == null || records == String.Empty)
                {
                    records = "10";
                }

                string connectionString = "Data Source=.;Initial Catalog=" + database + ";integrated security=True;TrustServerCertificate=True";

                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();

                    string sql = "SELECT TOP (" + records + ") * FROM " + table ;
                    string fieldtype = "";

                    using (SqlCommand command = new SqlCommand(sql, connection))
                    {
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                frameworkInfo framework = new frameworkInfo();
                                framework.fieldname_0 = (reader.GetName(0).ToString());
                                fieldtype = (reader.GetFieldType(0).ToString());
    
                                // Arrrggggghhhhh

                                if (fieldtype == "System.Int64") { framework.field_0 = (reader.GetInt64(0).ToString()); }
                                else if (fieldtype == "System.Int32") { framework.field_0 = (reader.GetInt32(0).ToString()); }
                                else if (fieldtype == "System.Int16") { framework.field_0 = (reader.GetInt16(0).ToString()); }
                                else if (fieldtype == "System.String") { framework.field_0 = (reader.GetString(0).ToString()); }
                                else if (fieldtype == "System.DateTime") { framework.field_0 = (reader.GetDateTime(0).ToString()); }
                                else if (fieldtype == "System.Char") { framework.field_0 = (reader.GetChar(0).ToString()); }
                                else if (fieldtype == "System.Decimal") { framework.field_0 = (reader.GetDecimal(0).ToString()); }
                                else if (fieldtype == "System.Double") { framework.field_0 = (reader.GetDouble(0).ToString()); }
                                else if (fieldtype == "System.Boolean") { framework.field_0 = (reader.GetString(0).ToString()); }
                                else if (fieldtype == "System.Byte") { framework.field_0 = (reader.GetByte(0).ToString()); }
                                else if (fieldtype == "System.UInt16") { framework.field_0 = (reader.GetInt16(0).ToString()); }

                                framework.fieldname_1 = (reader.GetName(1).ToString());
                                fieldtype = (reader.GetFieldType(1).ToString());
                                if (fieldtype == "System.Int64") { framework.field_1 = (reader.GetInt64(1).ToString()); }
                                else if (fieldtype == "System.Int32") { framework.field_1 = (reader.GetInt32(1).ToString()); }
                                else if (fieldtype == "System.Int16") { framework.field_1 = (reader.GetInt16(1).ToString()); }
                                else if (fieldtype == "System.String") { framework.field_1 = (reader.GetString(1).ToString()); }
                                else if (fieldtype == "System.DateTime") { framework.field_1 = (reader.GetDateTime(1).ToString()); }
                                else if (fieldtype == "System.Char") { framework.field_1 = (reader.GetChar(1).ToString()); }
                                else if (fieldtype == "System.Decimal") { framework.field_1 = (reader.GetDecimal(1).ToString()); }
                                else if (fieldtype == "System.Double") { framework.field_1 = (reader.GetDouble(1).ToString()); }
                                else if (fieldtype == "System.Boolean") { framework.field_1 = (reader.GetString(1).ToString()); }
                                else if (fieldtype == "System.Byte") { framework.field_1 = (reader.GetByte(1).ToString()); }
                                else if (fieldtype == "System.UInt16") { framework.field_1 = (reader.GetInt16(1).ToString()); }

                                ETC... 
                                frameworks.Add(framework);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //some exception
            }
        }
    }

    public class frameworkInfo
    {
        public string? fieldname_0;
        public string? field_0 = "";

        public string? fieldname_1;
        public string? field_1 = "";

        Etc...
    }
}
pdtvr36n

pdtvr36n1#

您可以简单地使用Convert.ToString,只需检查它前面的值是否不为空:

using (SqlCommand command = new SqlCommand(sql, connection))
{
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
           for (int i = 0; i < reader.FieldCount; i++)
                if (reader.GetValue(i) != DBNull.Value){
                   framework.field_1 = Convert.ToString(reader.GetValue(i)));
                 }
         }
    }
}
3htmauhk

3htmauhk2#

对不起,它不会让我张贴这位在上面,这是我如何输出的表。
正如您将看到的,我有来自我的模型的字段,这可能会使它更容易理解我试图做什么。

<table class="table">
    <thead>
        <tr>
            @foreach (var item in Model.framework)
        {
            <tr>
                <td> @item.fieldname_0 </td>
                <td> @item.fieldname_1 </td>
                <td> @item.fieldname_2 </td>
                <td> @item.fieldname_3 </td>
                <td> @item.fieldname_4 </td>
            </tr>
            break;
        }
        </tr>
    </thead>

    <tbody>
        @foreach (var item in Model.framework)
        {
            <tr>
                <td> @item.field_0 </td>
                <td> @item.field_1 </td>
                <td> @item.field_2 </td>
                <td> @item.field_3 </td>
                <td> @item.field_4 </td>
            </tr>
        }
    </tbody>
</table>

这一部分对我很有效,非常感谢你的帮助。这将保存我大量的剪切和粘贴:2行代码为每个字段和几乎没有开销的代码,我现在可以敲出尽可能多的这些页面需要为我的项目在大约2分钟。

frameworkInfo framework= new frameworkInfo();

framework.field_0 = Convert.ToString(reader.GetValue(0));
framework.fieldname_0 = (reader.GetName(0).ToString()); 

framework.field_1 = Convert.ToString(reader.GetValue(1));
framework.fieldname_1 = (reader.GetName(1).ToString());

framework.field_2 = Convert.ToString(reader.GetValue(2));
framework.fieldname_2 = (reader.GetName(2).ToString());

framework.field_3 = Convert.ToString(reader.GetValue(3));
framework.fieldname_3 = (reader.GetName(3).ToString());

framework.field_4 = Convert.ToString(reader.GetValue(4));
framework.fieldname_4 = (reader.GetName(4).ToString());

frameworks.Add(framework)

相关问题