[英]C# Read multiple rows from different columns sharing same Foreign key SQL
我想要一種有效的方法來檢索表中共享相同外鍵的所有信息,並將數據存儲在列表/數組中。
我可以從一列中讀取幾行:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
int idForeignKey = inputIdFkey //Implemented on the WebPage for testing purposes
List<string> result = new List<string>();
string oString = "Select Column from Table where foreignKey = @fKey";
conn.Open();
SqlCommand oCmd = new SqlCommand(oString, conn);
oCmd.Parameters.AddWithValue("@fKey", idForeignKey);
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{
result.Add(oReader.GetString(0));
}
}
conn.Close();
而且,如果我針對某一特定行,則可以閱讀幾列:
int sqlData1;
int sqlData2;
int sqlData3;
string oString = "Select * from Table where TableID = @tId";
SqlCommand oCmd = new SqlCommand(oString, conn);
oCmd.Parameters.AddWithValue("@tId", 1001);
conn.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{
sqlData1 = oReader["Row1"].ToString();
sqlData2 = oReader["Row2"].ToString();
sqlData3 = oReader["Row3"].ToString();
}
}
conn.Close();
但我希望能夠讀取具有相同外鍵的所有/特定數據。 因此,我希望能夠檢索幾行,將它們保存到列表中,並從共享相同外鍵的不同列中檢索其他幾行數據。
我想這是這樣的:
int idForeignKey = inputIdFkey //Implemented on the WebPage for testing purposes
List<int> intList = new List<int>();
List<string> stringList = new List<string>();
List<DateTime> dateList = new List<DateTime>();
string oString = "Select * from Table where ForeignKey = @fKey";
conn.Open();
SqlCommand oCmdSleep = new SqlCommand(oString, conn);
oCmdSleep.Parameters.AddWithValue("@fKey", idForeignKey);
using (SqlDataReader oReader = oCmdSleep.ExecuteReader())
{
while (oReader.Read())
{
intList.Add(oReader["Column1"].GetDateTime(0));
dstringList.Add(oReader["Column3"].GetDateTime(0));
dateList.Add(oReader["Column4"].GetDateTime(0));
}
}
conn.Close();
但這不起作用...請告訴我
如果使用類似Dapper的工具 ,它將簡化將查詢結果映射到List<T>
。
使用nuget將Dapper添加到您的項目中。
Install-Package Dapper -Version 1.50.5
在正在運行查詢的類頂部,為Dapper添加using。
using Dapper;
添加一個與查詢結果的結構匹配的類。 有一些使用腳本或實用程序應用程序執行此操作的方法。 這是一個應用程序。
public class MyClass
{
public int MyId { get; set; }
public string MyName { get; set; }
public DateTime MyDateTime { get; set; }
}
然后在運行查詢的地方這樣做。
using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
var MyList = conn.Query<MyClass>(@"select * from Table where ForeignKey = @fKey",
new { fKey = "SomeKey" }).ToList();
}
查詢運行后,您便可以遍歷MyList。
foreach (var myItem in MyList)
{
// Do something with myItem
}
如果要綁定結果,只需從查詢末尾刪除.ToList()
,因為其默認值為IObservable<T>
。
無需給出列名,只需使用已經建立的索引:
int idForeignKey = inputIdFkey //Implemented on the WebPage for testing purposes
List<int> intList = new List<int>();
List<string> stringList = new List<string>();
List<DateTime> dateList = new List<DateTime>();
string oString = "Select * from Table where ForeignKey = @fKey";
conn.Open();
SqlCommand oCmdSleep = new SqlCommand(oString, conn);
oCmdSleep.Parameters.AddWithValue("@fKey", idForeignKey);
using (SqlDataReader oReader = oCmdSleep.ExecuteReader())
{
while (oReader.Read())
{
intList.Add(oReader.GetDateTime(0));
dstringList.Add(oReader.GetDateTime(3));
dateList.Add(oReader.GetDateTime(4));
}
}
conn.Close();
這是一個細分:
listVariable.Add(oReader.GetDataType("Index of column"));
這樣,您就可以檢索共享外鍵的所有行數據,並可以選擇所需的任意列數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.