将查询结果保存/添加到集合中 [英] Save/ADD Query result to a Collection

查看:133
本文介绍了将查询结果保存/添加到集合中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





是否可以将查询结果添加到集合中



喜欢



Hi,

Is it possible to add the result of a Query to a collection

Like

SELECT LastName,FirstName,MiddleName,Country FROM Contact ORDER BY LastName







Dim Contact As New List(Of String)





我的目的是搜索我的收藏,这样我就不需要再次查询



希望你可以帮我这个:)





好​​吧我不能做得对,



我创建了一个类CollectContact,它具有LastName,FirstName和FamilyName的公共属性。还有一个类ListContact,其中包含



My purpose it to Search my Collection So that I Don't Need to Query Again and Again

Hope You can help me with This :)


OK I can't do it right,

I Created a Class "CollectContact" which Have Public Property for LastName, FirstName, and FamilyName. And a Class "ListContact" which contains

Public Class CollectContact
Private _FirstName As String = String.Empty
Friend Property FirstName() As String
       Get
           Return _FirstName
       End Get
       Set(ByVal value As String)
           _FirstName= value
       End Set
   End Property
Private _LastName As String = String.Empty
Friend Property LastName () As String
       Get
           Return _LastName 
       End Get
       Set(ByVal value As String)
           _LastName = value
       End Set
   End Property
End Class







Public Class ListContact
Public Sub New()
        _List = New List(Of CollectContact)
End Sub
Private _List As List(Of CollectContact)
Public Property ListOfFriend() As List(Of CollectContact)
        Get
            Return _Profile
        End Get
        Set(ByVal value As List(Of CollectContact))
            _Profile = value
        End Set
    End Property
End Class

< br $> b $ b



Dim Contact As New CollectContact
Dim ContactList As New ListContact



现在在我的DataReader.Read上


Now on my DataReader.Read

Contact.FirstName = DataReader("First Name").ToString
Contact.LastName = DataReader("Last Name").ToString
ContactList.ListOfFriend.Add(Contact)



假设我有一个数据:

FirstName LastName

Ron McMiller

James Baker

Sarah Jasmine



现在当我调试ListOfFriend的返回值



我得到3行具有相同的FirstName& LastName

行数是正确的但是FirstName& LastName都是一样的


Lets say I have a Data:
FirstName LastName
Ron McMiller
James Baker
Sarah Jasmine

Now When I Debug the Return Value of ListOfFriend

I get 3 Rows that have same FirstName & LastName
Number of Rows are correct but the FirstName & LastName are all the same

推荐答案

是的可能。

你必须遵循的步骤是

1创建一个Object类,其中应该包含从sql中获取数据的字段,因此sql表和object类中两个字段的类型应该相同。

ex:

public class BOEmployee

{

public string LastName {get; set;}

public string FirstName {get; set;}

.........

........

}

2.创建您查询的类的对​​象/实例正在传递给数据库

并声明您的类型集合,这里我使用的是对象类类型的ObservableColletion

ex :

BOEmployee boemployee;

ObservableCollection CLemployee == new ObservableCollection< boemployee>();

3.用你的连接超越查询

ex:

SqlConnection Conn = AppConfiguration .Connection;

SqlDataReader rdr = SqlHelper.ExecuteReader(Conn,CommandType.Text,

SELECT LastName,FirstName,MiddleName,Country FROM Contact ORDER BY LastName);

4.从DataReader读取结果并将结果添加到集合中

While(rdr.Read())

{

boemployee = new BOEmployee();

boemployee.FirstName = rdr [FirstName]。ToString();

........ ..........

...................

CLemployee.Add(boemployee );

}

现在您拥有集合中的所有数据,您可以根据您的使用情况对其进行过滤。无需始终与数据库连接....
Yes Its Possible.
The Steps you have to follow is
1. Create an Object class which should contains the fields to fetch your data from sql, so the type of both fields in sql table and object class should be same.
ex:
public Class BOEmployee
{
public string LastName{get;set;}
public string FirstName{get;set;}
.........
........
}
2. Create an object/instance of the class where you are query is passing to database
And Declare your collection of your type, here i am using ObservableColletion of your object class type
ex:
BOEmployee boemployee;
ObservableCollection CLemployee== new ObservableCollection<boemployee>();
3. Excecute the query with your connection
ex:
SqlConnection Conn = AppConfiguration.Connection;
SqlDataReader rdr = SqlHelper.ExecuteReader(Conn, CommandType.Text,
"SELECT LastName,FirstName,MiddleName,Country FROM Contact ORDER BY LastName");
4. Read the result from DataReader and add the results to the collection
While (rdr.Read())
{
boemployee=new BOEmployee();
boemployee.FirstName=rdr["FirstName"].ToString();
..................
...................
CLemployee.Add(boemployee);
}
Now you have all the data in the collection , you can filter it according to your usage No need to connect with database always....


我做错了是声明
Dim Contact As New CollectContact

让我这么傻X(



另一个问题我如何搜索ListContact上的名字?



我试过

outside my Reader so stupid of me X(

Another Question how Can I search a Name on my ListContact ?

I tried

Public Sub SearchFirstName(ByVal Name As String)
       Dim _ListContact As New ListContact
       Dim cAccount As CollectContact = _ListContact.ListOfFriend.Find(AddressOf sName)
       If Not cAccount Is Nothing Then
           'How to getting the FirstName, LastName, And MiddleName
       End If
   End Sub

   Private Function sName(ByVal cName As AllUsers) As Boolean
       If cName.UserName = Name Then
           Return True
       Else
           Return Nothing
       End If
   End Function

这篇关于将查询结果保存/添加到集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆