I have a table like
CREATE TABLE Partners
(
id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(),
name NVARCHAR(50) NOT NULL,
email NVARCHAR(254) NOT NULL, -- 254 is optimal length according to http://stackoverflow.com/questions/1199190/what-is-the-optimal-length-for-an-email-address-in-a-database
PRIMARY KEY (id)
);
and a simple stored procedure:
CREATE PROCEDURE GetPartnersWithIds
AS
SELECT id, name
FROM Partners;
The result of which I'm trying to reading into a List<PartnerWithId>
where PartnerWithId
is defined by
public class PartnerWithId
{
public Guid Id { get; set; }
public string Name { get; set; }
}
My method for doing so is
public List<PartnerWithId> GetPartners ( )
{
List<PartnerWithId> Partners = new List<PartnerWithId>();
using (SqlCommand cmd = new SqlCommand("GetPartnersWithIds", this._Conn))
{
cmd.CommandType = CommandType.StoredProcedure;
this._Conn.Open();
using (SqlDataReader dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
Partners.Add(new PartnerWithId {
Id = (Guid)dataReader.GetString(0),
Name = dataReader.GetString(1)
});
}
}
this._Conn.Close();
}
return Partners;
}
but I'm getting an error
Cannot convert type
string
toSystem.Guid
on chunk (Guid)dataReader.GetString(0)
(which was really just a guess). What is the correct way to get this value?