C# 如何在c中解决此错误#

C# 如何在c中解决此错误#,c#,sql,sql-server,sql-server-2008,C#,Sql,Sql Server,Sql Server 2008,我想将数据插入数据库表: myCommand.CommandText = "INSERT INTO Selectionner (IdPrestation, IdPhrase, DegreUrgence,RisqueConcerne,rowguid,Cotation) " + "VALUES ('" +new Guid(emp.IdPrestation) +

我想将数据插入数据库表:

myCommand.CommandText = "INSERT INTO Selectionner (IdPrestation,
   IdPhrase, DegreUrgence,RisqueConcerne,rowguid,Cotation) " +                                                               
   "VALUES   ('" +new Guid(emp.IdPrestation) + 
   "', '" +new Guid(emp.IdPhrase)+ "', '" +
   emp.DegreUrgence + "','" + emp.RisqueConcerne + "','" + 
   new Guid(emp.rowguid) + "','" + emp.Cotation + "')";
但这会返回一个错误:

Guid应包含带4个破折号的32位数字
(xxxxxxxx-xxxx-xxxx-xxxx-XXXXXXXXXX)

如何解决此错误?

您的一个或多个

emp.IdPrestation //Or 
emp.IdPhrase //Or 
emp.rowguid //Check them before creating 
是/不是
GUID
。这就是它抛出错误的原因

编辑:开始

如何使用
Guid.TryParse()
,如果解析操作成功,它将返回true;否则,错误

//How to parse safely
Guid IdPrestation;
Guid IdPhrase;
Guid rowguid;

if(Guid.TryParse(emp.IdPrestation, out IdPrestation) &&
   Guid.TryParse(emp.IdPhrase, out IdPhrase) &&
   Guid.TryParse(emp.rowguid, out rowguid) )
{
   //all variables have been parse successfully
   //Execute the sql query as follows using parameters
}
编辑:结束

此外,使用内联sql将参数作为直接字符串传递是一种不安全的错误做法。而是使用参数化查询

myCommand.CommandText = "INSERT INTO yourTableName (c1, c2, ...)
VALUES (@p1, @p2,...)";
myCommand.Parameters.Add(new SqlParameter("p1", valueforCol1));
myCommand.Parameters.Add(new SqlParameter("p2", valueforCol2));
...
你的一个或多个

emp.IdPrestation //Or 
emp.IdPhrase //Or 
emp.rowguid //Check them before creating 
是/不是
GUID
。这就是它抛出错误的原因

编辑:开始

如何使用
Guid.TryParse()
,如果解析操作成功,它将返回true;否则,错误

//How to parse safely
Guid IdPrestation;
Guid IdPhrase;
Guid rowguid;

if(Guid.TryParse(emp.IdPrestation, out IdPrestation) &&
   Guid.TryParse(emp.IdPhrase, out IdPhrase) &&
   Guid.TryParse(emp.rowguid, out rowguid) )
{
   //all variables have been parse successfully
   //Execute the sql query as follows using parameters
}
编辑:结束

此外,使用内联sql将参数作为直接字符串传递是一种不安全的错误做法。而是使用参数化查询

myCommand.CommandText = "INSERT INTO yourTableName (c1, c2, ...)
VALUES (@p1, @p2,...)";
myCommand.Parameters.Add(new SqlParameter("p1", valueforCol1));
myCommand.Parameters.Add(new SqlParameter("p2", valueforCol2));
...

尝试使用参数化查询作为第一个改进

然后,尝试使用而不是。这样,我希望对不兼容的字符串引发异常


构造函数可能有点过于宽容,在这种情况下,您可能希望快速失败,以便知道哪个字段给您带来了麻烦。

尝试使用参数化查询作为第一个改进

然后,尝试使用而不是。这样,我希望对不兼容的字符串引发异常


构造函数可能有点不允许,在这种情况下,您可能希望快速失败,以便知道哪个字段给您带来了麻烦。

您不能仅从字符串创建GUID,字符串需要符合GUID

Guid originalGuid = Guid.NewGuid();
originalGuid.ToString("B")  gets converted to {81a130d2-502f-4cf1-a376-63edeb000e9f}
同样地

"N" - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (32 digits)
"D" - xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (32 digits separated by hyphens)
"B" - {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} (same as "D" with addition of braces)
"P" - (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) (same as "D" with addition of parentheses)
"X" - {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00.0x00}}
guid本身没有格式。它只是一种价值。注意,您可以使用NewGuid或guid的构造函数创建guid。使用NewGuid,您无法控制guid的值。使用guid的构造函数,可以控制该值。如果已经有guid的字符串表示形式(可能从数据库中读取),或者希望在开发过程中更容易解释guid,则使用构造函数非常有用。也可以使用、和方法

因此,您可以创建如下GUI:

Guid g1 = Guid.NewGuid(); //Get a Guid without any control over the contents
Guid g2 = new Guid(new string('A',32)); //Get a Guid where all digits == 'A'
Guid g3 = Guid.Parse(g1.ToString());
Guid g4 = Guid.ParseExact(g1.ToString("D"),"D");
Guid g5;
bool b1 = Guid.TryParse(g1.ToString(), out g5);
Guid g6;
bool b2 = Guid.TryParseExact(g1.ToString("D"),"D", out g6);

不能仅从字符串创建GUID,该字符串需要符合GUID

Guid originalGuid = Guid.NewGuid();
originalGuid.ToString("B")  gets converted to {81a130d2-502f-4cf1-a376-63edeb000e9f}
同样地

"N" - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (32 digits)
"D" - xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (32 digits separated by hyphens)
"B" - {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} (same as "D" with addition of braces)
"P" - (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) (same as "D" with addition of parentheses)
"X" - {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00.0x00}}
guid本身没有格式。它只是一种价值。注意,您可以使用NewGuid或guid的构造函数创建guid。使用NewGuid,您无法控制guid的值。使用guid的构造函数,可以控制该值。如果已经有guid的字符串表示形式(可能从数据库中读取),或者希望在开发过程中更容易解释guid,则使用构造函数非常有用。也可以使用、和方法

因此,您可以创建如下GUI:

Guid g1 = Guid.NewGuid(); //Get a Guid without any control over the contents
Guid g2 = new Guid(new string('A',32)); //Get a Guid where all digits == 'A'
Guid g3 = Guid.Parse(g1.ToString());
Guid g4 = Guid.ParseExact(g1.ToString("D"),"D");
Guid g5;
bool b1 = Guid.TryParse(g1.ToString(), out g5);
Guid g6;
bool b2 = Guid.TryParseExact(g1.ToString("D"),"D", out g6);

不要连接sql字符串,而是使用参数来避免sql注入和解析错误。我们需要以下变量的值来回答您的问题:
emp.IdPrestation
emp.IdPhrase
emp.rowguid
。您正在使用它们初始化
guid
,guid必须包含32位数字和4个破折号(错误是不言自明的,不是吗?)。能否给出emp.IdPrestation、emp.IdPhrase和emp.rowguid的一些示例值?不要连接sql字符串,而是使用参数来避免sql注入和解析错误。我们需要以下变量的值来回答您的问题:
emp.IdPrestation
emp.IdPhrase
emp.rowguid
。您正在使用它们初始化
guid
,guid必须包含32位数字和4个破折号(错误是不言自明的,不是吗?)。您可以给出emp.IdPrestation、emp.IdPhrase和emp.rowguid的一些示例值吗?但是,该错误是由于无法完成解析而导致的吗?或者解析成功,但实际插入正在生成异常?是。我从其他数据库sqlite恢复数据,其中类型为
IdPrestation
不是Guid而是int。现在我正在研究如何在sqlite数据库中插入Guid。Guidsqlite不支持Guid,您可以做的是作为字符串插入,并在数据层中公开一个属性Guid,该属性Guid自动处理get和set的转换。但是,错误是由于无法完成解析而导致的吗?或者解析成功,但实际插入正在生成异常?是。我从其他数据库sqlite恢复数据,其中
IdPrestation
的类型不是Guid而是int。现在我正在研究如何在sqlite数据库中插入Guid。Guidsqlite不支持Guid,您可以做的是作为字符串插入,并在数据层中公开一个属性Guid,该属性Guid自动处理get和set的转换。