[英]Confused and stuck on a datareader problem SQL Server / .NET
我正在嘗試在 SQL 服務器指標的通用網頁上工作,這些指標可以來自查詢/存儲過程等。它們可能位於不同的數據庫和表中,我希望它們采用良好的格式。
假設我有一個如下所示的 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
(完成)打開 Web 頁面 - 代碼從 URL(動態)獲取數據庫/表/recordCount
(完成)創建到 SQL 的連接
(完成)運行查詢或存儲過程
(完成)將所有返回的字段類型轉換為類型字符串,這樣我就不必擔心將我的 model 與 SQL 服務器表匹配。
(完成)然后顯示查詢/存儲過程的結果
(已完成)我什至不必弄亂 HTML 方面的事情,這就是為什么我在 model 中有名稱和值,這樣我就可以從 Model 開始制作表格標題。
我試圖盡可能簡單地做到這一點,因為它只是我希望能夠通過 web 頁面查詢的幾個腳本,這樣我就可以單擊 URL 並在幾秒鍾內在我面前獲得指標,而無需必須登錄到 SQL 運行腳本、更改數據庫等。
我的工作方式是:我只查看要使用的字段數,然后將這些新行添加到 model _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 等,但棘手的一點是Type 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 字段 model
示例網頁 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 字段 model
一切正常,我可以將返回的字段制作成 model,我可以將所有類型更改為字符串,將它們放入 Model 並將它們顯示在單獨的頁面上,所以這是一個勝利。
但一點也不優雅。
棘手的部分,我怎樣才能讓它具有最少的轉換編碼?
我想遍歷它們,但一直無法這樣做。
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...
}
}
這就是我輸出表格的方式。 正如您將看到的,我有我的 model 中的字段,這可能會讓您更容易理解我正在嘗試做什么。
<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)
您可以簡單地使用Convert.ToString
,只需檢查它之前的值是否不是 null :
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)));
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.