错误 "Insufficient result space to convert uniqueidentifier value to char."
Error "Insufficient result space to convert uniqueidentifier value to char."
Table 用于插入:
CREATE TABLE [dbo].[CcureMessage]
(
[CcureMessageId] [UNIQUEIDENTIFIER] NOT NULL,
[Event] [VARCHAR](20) NULL,
[Type] [VARCHAR](20) NULL,
[Message] [VARCHAR](MAX) NOT NULL,
[Xml] [VARCHAR](4000) NOT NULL,
CONSTRAINT [PK_CcureMessage]
PRIMARY KEY CLUSTERED ([CcureMessageId] ASC)
WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[CcureMessage]
ADD CONSTRAINT [DF_CcureMessage_CcureMessageId]
DEFAULT (NEWID()) FOR [CcureMessageId]
GO
我让 PK table 有一个默认值,这样我就根本不会传递 GUID,但似乎我仍然收到与 GUID 相关的错误。
插入通过 SSMS 正常工作的命令:
INSERT INTO CcureMessage (Event, Type, Message, Xml)
VALUES ('event 3', 'type 3', 'big json 3', 'xml-ish');
C#代码:
public void DoInsert(Message msg)
{
// hard-coding this to set test values
TopicMessage tm = new TopicMessage();
tm.Event = "event 1";
tm.Type = "Type 1";
tm.Message = "json data message";
tm.Xml = "xml data goes here";
string connString = set to correct value;
string sql = "INSERT INTO CcureMessage (Event, Type, Message, Xml) VALUES (@Event, @Type, @Message, @Xml)";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = System.Data.CommandType.Text;
SqlParameter eventParm = new SqlParameter("@Event", tm.CcureMessageId);
SqlParameter typeParm = new SqlParameter("@Type", tm.Type);
SqlParameter msgParm = new SqlParameter("@Message", tm.Message);
SqlParameter xmlParm = new SqlParameter("@Xml", tm.Xml);
cmd.Parameters.Add(eventParm);
cmd.Parameters.Add(typeParm);
cmd.Parameters.Add(msgParm);
cmd.Parameters.Add(xmlParm);
cmd.ExecuteNonQuery();
}
}
运行 这会导致错误
Insufficient result space to convert uniqueidentifier value to char
问题似乎是您将 guid 传递给代码中的 varchar 列
SqlParameter eventParm = new SqlParameter("@Event", tm.CcureMessageId);
应该是:
SqlParameter eventParm = new SqlParameter("@Event", tm.Event);
就我而言,我没有在 C# 代码中使用 guid 并收到此错误。所以追溯到存储过程,我发现错误直接来自那里。
我将其追溯到我在 GUID 字段上 运行 的转换,如下所示:
CONVERT(varchar(13), guid)
我以为它只会截断字段值。所以我必须执行以下操作:
CONVERT(varchar(13), CONVERT(varchar(36), guid))
Table 用于插入:
CREATE TABLE [dbo].[CcureMessage]
(
[CcureMessageId] [UNIQUEIDENTIFIER] NOT NULL,
[Event] [VARCHAR](20) NULL,
[Type] [VARCHAR](20) NULL,
[Message] [VARCHAR](MAX) NOT NULL,
[Xml] [VARCHAR](4000) NOT NULL,
CONSTRAINT [PK_CcureMessage]
PRIMARY KEY CLUSTERED ([CcureMessageId] ASC)
WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[CcureMessage]
ADD CONSTRAINT [DF_CcureMessage_CcureMessageId]
DEFAULT (NEWID()) FOR [CcureMessageId]
GO
我让 PK table 有一个默认值,这样我就根本不会传递 GUID,但似乎我仍然收到与 GUID 相关的错误。
插入通过 SSMS 正常工作的命令:
INSERT INTO CcureMessage (Event, Type, Message, Xml)
VALUES ('event 3', 'type 3', 'big json 3', 'xml-ish');
C#代码:
public void DoInsert(Message msg)
{
// hard-coding this to set test values
TopicMessage tm = new TopicMessage();
tm.Event = "event 1";
tm.Type = "Type 1";
tm.Message = "json data message";
tm.Xml = "xml data goes here";
string connString = set to correct value;
string sql = "INSERT INTO CcureMessage (Event, Type, Message, Xml) VALUES (@Event, @Type, @Message, @Xml)";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = System.Data.CommandType.Text;
SqlParameter eventParm = new SqlParameter("@Event", tm.CcureMessageId);
SqlParameter typeParm = new SqlParameter("@Type", tm.Type);
SqlParameter msgParm = new SqlParameter("@Message", tm.Message);
SqlParameter xmlParm = new SqlParameter("@Xml", tm.Xml);
cmd.Parameters.Add(eventParm);
cmd.Parameters.Add(typeParm);
cmd.Parameters.Add(msgParm);
cmd.Parameters.Add(xmlParm);
cmd.ExecuteNonQuery();
}
}
运行 这会导致错误
Insufficient result space to convert uniqueidentifier value to char
问题似乎是您将 guid 传递给代码中的 varchar 列
SqlParameter eventParm = new SqlParameter("@Event", tm.CcureMessageId);
应该是:
SqlParameter eventParm = new SqlParameter("@Event", tm.Event);
就我而言,我没有在 C# 代码中使用 guid 并收到此错误。所以追溯到存储过程,我发现错误直接来自那里。
我将其追溯到我在 GUID 字段上 运行 的转换,如下所示:
CONVERT(varchar(13), guid)
我以为它只会截断字段值。所以我必须执行以下操作:
CONVERT(varchar(13), CONVERT(varchar(36), guid))