Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列“[Group]”不属于表_C#_Sql Server_Ado.net - Fatal编程技术网

C# 列“[Group]”不属于表

C# 列“[Group]”不属于表,c#,sql-server,ado.net,C#,Sql Server,Ado.net,使用旧的SQL Server表时,它显示了一个名为[Group]的可选列,如果代码中完全省略了该列,insert命令就可以正常工作。这个问题与特定的col有关 我所拥有的: daCust.InsertCommand = new SqlCommand("insert into dbo.TimeRouteTest ([Group], Route, Step_1) values (@Group, @Route, @Step_1)", con); daCust.InsertCommand.Parame

使用旧的SQL Server表时,它显示了一个名为[Group]的可选列,如果代码中完全省略了该列,insert命令就可以正常工作。这个问题与特定的col有关

我所拥有的:

daCust.InsertCommand = new SqlCommand("insert into dbo.TimeRouteTest ([Group], Route, Step_1) values (@Group, @Route, @Step_1)", con);

daCust.InsertCommand.Parameters.Add(new SqlParameter("[Group]", SqlDbType.Int, 4, "[Group]"));
daCust.InsertCommand.Parameters.Add(new SqlParameter("Route", SqlDbType.NVarChar, 100, "Route"));
daCust.InsertCommand.Parameters.Add(new SqlParameter("Step_1", SqlDbType.NVarChar, 50, "Step_1"));
然后

注意:我预处理此数据以确保其干净,我去掉单引号等。mroute和mStep_1只是保存从另一个函数传递的文本数据的局部变量。myGroup也是一个从webservice传递值的本地变量。然后我跟进:

ds.Tables[0].Rows.Add(newRow);
daCust.Update(ds);
如果我注释掉[group]行,该命令工作正常。关于为什么会失败以及我如何修复它,有什么帮助吗?我知道group是一个保留关键字,但添加括号应该可以使它可用,或者我是这么认为的。有趣的是,我可以查询这个表,并从其他行的[group]col中获取值,因此我知道col确实存在。只是不确定为什么我在insert上找不到它

编辑:如果我使用此代码,它可以正常工作:

daCust.InsertCommand = new SqlCommand("insert into dbo.TimeRouteTest (Route, Step_1) values (@Route, @Step_1)", con);

daCust.InsertCommand.Parameters.Add(new SqlParameter("Route", SqlDbType.NVarChar, 100, "Route"));
daCust.InsertCommand.Parameters.Add(new SqlParameter("Step_1", SqlDbType.NVarChar, 50, "Step_1"));

DataRow newRow = ds.Tables[0].NewRow();
newRow["Route"] = mroute;
newRow["Step_1"] = mStep_1;

ds.Tables[0].Rows.Add(newRow);

daCust.Update(ds);
这几乎将问题与名为[Group]的特定列隔离开来,我认为没有括号或其他组合可以使它起作用。执行select*from TIMEOUTETEST,然后从dr[2]中提取值,这是该列的索引位置。除了废弃表并使用更好的列名称外,如何修复?奇怪的是,在VisualStudio中查看表定义时,它将col名称显示为[Group],但若我查看表中的数据,它只将Group显示为结果中的header/label

编辑:

该行:

daCust.InsertCommand.Parameters.Add(new SqlParameter("[Group]", SqlDbType.Int, 4, "[Group]"));

所有操作都会因错误而失败:

System.ArguementException:列“[Group]”不属于表

编辑:发现语法错误。在我的头撞在墙上3个小时后,以下代码实际起作用:

 daCust.InsertCommand = new SqlCommand("insert into dbo.TimeRouteTest ([Group], Route, Step_1) values (@Group, @Route, @Step_1)", con);

 daCust.InsertCommand.Parameters.Add(new SqlParameter("Group", SqlDbType.Int, 4, "Group"));
 daCust.InsertCommand.Parameters.Add(new SqlParameter("Route", SqlDbType.NVarChar, 100, "Route"));
 daCust.InsertCommand.Parameters.Add(new SqlParameter("Step_1", SqlDbType.NVarChar, 50, "Step_1"));

 DataRow newRow = ds.Tables[0].NewRow();
 newRow["Group"] = Convert.ToInt32(myGroup);
 newRow["Route"] = mroute;
 newRow["Step_1"] = mStep_1;

结果表明,在声明col名称时,唯一需要括号的地方是insert语句,将它们添加到其他任何地方都会导致各种错误。我的最佳猜测是,VisualStudio在遇到关键字col name时会导致部分混淆,因为它会自动在col name上显示括号,因此在导出表定义时,括号似乎是名称的一部分,而实际上并非如此


只有在使用某些SQL语句且列名称是保留的SQL Server关键字时,方括号才是必需的。至少可以说是令人恼火的

结果表明,在声明列名称时,唯一需要括号的地方是insert语句,将它们添加到其他任何地方都会导致各种错误。我的最佳猜测是,VisualStudio在遇到关键字col name时会导致部分混淆,因为它会自动在col name上显示括号,因此在导出表定义时,括号似乎是名称的一部分,而实际上并非如此。只有在使用某些sql语句且列名称是保留的sql关键字时,方括号才是必需的。正确的代码显示在问题的底部。感谢GSerg的验证。如果这是一个业余的错误,我很抱歉,这让我困惑了一段时间。

参数名称应该是@Group here:new吗SqlParameter@Group, ...? 还需要为insert命令指定最后一个参数[Group]吗?在SqlParameter行中尝试@Group,不起作用。从技术上讲不是,如果我完全省略[Group]参数,代码就可以工作。它没有被标记为required null,insert允许,但实际上,我确实需要为该列向表中传递一个值。第四个参数,您在其中写入的[Group]不是值。请尝试daCust.InsertCommand.Parameters.AddnewSqlParameter@Group,SqlDbType.Int,4;如果使用该行,则给出相同的错误。该列称为组。只有当它是查询的一部分时,才在它周围加括号,而不是在其他任何地方,因为只有在查询的上下文中,才可能需要将SQL关键字组设置为非关键字。特别是,您可以使用newRow[Group]和@Group。您也不应该去掉单引号等,因为这样做意味着您并不真正了解您正在做什么,以及参数化命令和命令之间的区别。
daCust.InsertCommand.Parameters.Add(new SqlParameter("@Group", SqlDbType.Int, 4, "[Group]"));
daCust.InsertCommand.Parameters.Add(new SqlParameter("@[Group]", SqlDbType.Int, 4, "[Group]"));
 daCust.InsertCommand = new SqlCommand("insert into dbo.TimeRouteTest ([Group], Route, Step_1) values (@Group, @Route, @Step_1)", con);

 daCust.InsertCommand.Parameters.Add(new SqlParameter("Group", SqlDbType.Int, 4, "Group"));
 daCust.InsertCommand.Parameters.Add(new SqlParameter("Route", SqlDbType.NVarChar, 100, "Route"));
 daCust.InsertCommand.Parameters.Add(new SqlParameter("Step_1", SqlDbType.NVarChar, 50, "Step_1"));

 DataRow newRow = ds.Tables[0].NewRow();
 newRow["Group"] = Convert.ToInt32(myGroup);
 newRow["Route"] = mroute;
 newRow["Step_1"] = mStep_1;