gpt4 book ai didi

c# - 我应该将 SQL Server UNIQUEIDENTIFIER 检索到哪种 C# 数据类型?

转载 作者:行者123 更新时间:2023-11-30 21:51:50 25 4
gpt4 key购买 nike

<分区>

我有一张 table

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)
);

和一个简单的存储过程:

CREATE PROCEDURE GetPartnersWithIds
AS
SELECT id, name
FROM Partners;

我试图读入 List<PartnerWithId> 的结果其中 PartnerWithId

定义
public class PartnerWithId
{
public Guid Id { get; set; }
public string Name { get; set; }
}

我的方法是

    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;
}

但是我遇到了一个错误

Cannot convert type string to System.Guid

在 block 上 (Guid)dataReader.GetString(0) (这真的只是一个猜测)。获取此值的正确方法是什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com