我想知道如何使用C#最灵活地从多个表中收集数据。 我不太习惯C#和数据库管理:我听说过ADO.NET和LINQ以及使用表的简单性,但我还没有时间阅读它。
所以收集数据的方法非常复杂:
int id = 43;
var collectedData = new System.Collections.Generic.Dictionary<string, string>(); // this will later be all my collected data
// Load from first table
string select1 = "SELECT * FROM table1 WHERE ID = " + id;
DataRow dataRow1= SqlHelper.Execute.SingleRow(select1); // my helperclass returning one single data row of a table
collectedData["Surname"] = dataRow1["Surname"];
collectedData["Name"] = dataRow1["Name"];
collectedData["Age"] = dataRow1["Age"];
// Load from second table
string select2 = "SELECT * FROM table2 WHERE ID = " + id;
DataRow dataRow2= SqlHelper.Execute.SingleRow(select2);
collectedData["Mother"] = dataRow2["Mother"];
collectedData["Father"] = dataRow2["Father"];
collectedData["Job"] = dataRow2["Job"];
// ...
那么C#和.NET专业人员将如何解决这个问题呢?我期待着您的代码示例!
答案 0 :(得分:2)
如果您正在使用SQL Server,那么ADO.NET + LINQ-to-SQL绝对是您的选择。
LINQ的原因:
不使用LINQ的原因:
对于您的示例,您将需要创建表示数据库实体的类(POCO)。然后,您将使用LINQ查询表并将数据加载到新的实体对象中。我更喜欢在我的域/实体对象上有一个构造函数,它将LINQ生成的持久数据库对象转换为客户端使用的域层对象。
public class Person
{
public int ID { get; set; }
public string Surname { get; set; }
public string Name { get; set; }
public short Age { get; set; }
public Person()
{
}
public Person( Persistence.Person src )
{
this.ID = src.ID;
this.Surname = src.surname;
this.Name = src.name;
this.Age = src.age;
}
}
...
public List<Domain.Person> LoadPeople()
{
using( var context = this.CreateContext() )
{
var personQuery = from p in context.Persons
select new Domain.Person( p );
return personQuery.ToList();
}
}
public Person LoadPerson( int personID )
{
using( var context = this.CreateContext() )
{
var personQuery = from p in context.Persons
where p.id == personID
select new Domain.Person( p );
return personQuery.SingleOrDefault();
}
}
答案 1 :(得分:1)
假设您有以下表格:
tblUsers
UserID LName FName GenderID
1 Joe Chan 1
2 Koh Wang 2
3 John Chen 1
tblGenders
GenderID Gender
1 Male
2 Female
您可以使用此代码获取包含上述两个表中的组合信息的数据集。
SqlConnection con = new SqlConnection(Connection String for you database);
con.Open();
SqlCommand comm = con.CreateCommand();
comm.CommandText = "SELECT u.UserID, u.Fname, u.Lname, u.GenderID, g.Gender
FROM tblUsers u, tblGenders g
WHERE u.GenderID = g.GenderID";
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
您可以使用此代码访问表下的行 da.Table [0] .Rows;
如果您已熟悉存储过程,则还可以使用以下代码:
internal static List<RoomType> FetchRoomTypeList()
{
List<RoomType> roomTypes = new List<RoomType>();
SqlCommand commRoomTypeSelector = ConnectionManager.MainConnection.CreateCommand();
commRoomTypeSelector.CommandType = CommandType.StoredProcedure;
commRoomTypeSelector.CommandText = "Rooms.asp_RMS_RoomTypeList_Select";
SqlDataAdapter da = new SqlDataAdapter(commRoomTypeSelector);
DataSet ds = new DataSet();
da.Fill(ds);
roomTypes = (from rt in ds.Tables[0].AsEnumerable()
select new RoomType
{
RoomTypeID = rt.Field<int>("RoomTypeID"),
RoomTypeName = rt.Field<string>("RoomType"),
LastEditDate = rt.Field<DateTime>("LastEditDate"),
LastEditUser = rt.Field<string>("LastEditUser"),
IsActive = (rt.Field<string>("IsActive") == "Active")
}
).ToList();
return roomTypes;
}
了解更多信息,请访问: