【问题标题】:Primary Key in bound datagridview displayes negative numbers绑定datagridview中的主键显示负数
【发布时间】:2014-01-26 05:10:10
【问题描述】:

我有一个绑定到 sqlserver 2012 数据库中的表的 datagridview。 所有字段都正确显示,除了显示为负数的主键字段。

这是什么原因造成的?

<Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
     Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")>  _
    Private Sub InitCommandCollection()
        Me._commandCollection = New Global.System.Data.SqlClient.SqlCommand(1) {}
        Me._commandCollection(0) = New Global.System.Data.SqlClient.SqlCommand()
        Me._commandCollection(0).Connection = Me.Connection
        Me._commandCollection(0).CommandText = "SELECT TSId, TCId, ElementNo, MSECId, UseInd, ElementDepth, ElementName, ChildInd"& _ 
            ", ElementValueDef, ElementValueNew, ElementPath, ElementPathFull, ElementValuePr"& _ 
            "efix, ElementValueLength, ElementValueIncrement, UpperControlElement1, UpperCont"& _ 
            "rolElementValue1, UpperControlElement2, UpperControlElementValue2, LowerControlE"& _ 
            "lement1, LowerControlElementValue1, LowerControlElement2, LowerControlElementVal"& _ 
            "ue2, AttributeName1, AttributeValue1, AttributeName2, AttributeValue2, Attribute"& _ 
            "Name3, AttributeValue3, AttributeName4, AttributeValue4, AttributeName5, Attribu"& _ 
            "teValue5 FROM dbo.XML_TEST_SET"
        Me._commandCollection(0).CommandType = Global.System.Data.CommandType.Text
        Me._commandCollection(1) = New Global.System.Data.SqlClient.SqlCommand()
        Me._commandCollection(1).Connection = Me.Connection
        Me._commandCollection(1).CommandText = "dbo.SP_XML_TEST_SET_FillByComboBoxes"
        Me._commandCollection(1).CommandType = Global.System.Data.CommandType.StoredProcedure
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@RETURN_VALUE", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.ReturnValue, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyENVId", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyUId", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyMTId", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyTestSetName", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyTestCaseName", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyInterchangeSeqNo", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyInstructionSeqNo", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyTransactionSeqNo", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyElementValueDef", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyElementPathFull", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyFetchRows", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
    End Sub

存储过程:

    USE [UToolDb]

去 设置 ANSI_NULLS ON 走 设置 QUOTED_IDENTIFIER ON 走 更改程序 [dbo].[SP_XML_TEST_SET_FillByComboBoxes] -- 这里添加存储过程的参数 @MyENVId 作为 INT, @MyUId 作为 INT, @MyMTId 作为 INT, @MyTestSetName AS NVARCHAR(MAX), @MyTestCaseName AS NVARCHAR(MAX), @MyInterchangeSeqNo AS INT, @MyInstructionSeqNo 作为 INT, @MyTransactionSeqNo 作为 INT, @MyElementValueDef AS NVARCHAR(MAX), @MyElementPathFull AS NVARCHAR(MAX), @MyFetchRows AS INT

作为 开始 -- 添加了 SET NOCOUNT ON 以防止额外的结果集 -- 干扰 SELECT 语句。 设置无计数;

SET ROWCOUNT @MyFetchRows

DECLARE @sql NVARCHAR(MAX) = N'
SELECT     
TS.TCId,
TS.ElementNo,
TS.MSECId,
TS.UseInd,
TS.ElementDepth,
TS.ElementName,
TS.ChildInd,
TS.ElementValueDef,
TS.ElementValueNew,
TS.ElementPath,
TS.ElementPathFull,
TS.ElementValuePrefix,
TS.ElementValueLength,
TS.ElementValueIncrement,
TS.UpperControlElement1,
TS.UpperControlElementValue1,
TS.UpperControlElement2,
TS.UpperControlElementValue2,
TS.LowerControlElement1,
TS.LowerControlElementValue1,
TS.LowerControlElement2,
TS.LowerControlElementValue2,
TS.AttributeName1,
TS.AttributeValue1,
TS.AttributeName2,
TS.AttributeValue2,
TS.AttributeName3,
TS.AttributeValue3,
TS.AttributeName4,
TS.AttributeValue4,
TS.AttributeName5,
TS.AttributeValue5
FROM dbo.XML_TEST_SET TS 
LEFT JOIN dbo.XML_TEST_SET_OVERVIEW TSO 
ON TSO.TCId = TS.TCId '     
+ CASE WHEN (@MyENVId IS NOT NULL) THEN N'WHERE TSO.ENVId = @MyENVId ' ELSE 'WHERE TSO.ENVId IS NOT NULL '  END
+ CASE WHEN (@MyUId IS NOT NULL) THEN N'AND TSO.UId = @MyUId ' ELSE '' END
+ CASE WHEN (@MyMTId IS NOT NULL) THEN N'AND TSO.MTId = @MyMTId ' ELSE '' END
+ CASE WHEN (@MyTestSetName IS NOT NULL) THEN N'AND TSO.TestSetName = @MyTestSetName ' ELSE '' END 
+ CASE WHEN (@MyTestCaseName IS NOT NULL) THEN N'AND TSO.TestCaseName = @MyTestCaseName ' ELSE '' END 
+ CASE WHEN (@MyInterchangeSeqNo IS NOT NULL) THEN N'AND TSO.TestCaseName = @MyTestCaseName ' ELSE '' END 
+ CASE WHEN (@MyInstructionSeqNo IS NOT NULL) THEN N'AND TSO.TestCaseName = @MyTestCaseName ' ELSE '' END 
+ CASE WHEN (@MyTransactionSeqNo IS NOT NULL) THEN N'AND TSO.TransactionSeqNo = @MyTransactionSeqNo ' ELSE '' END
+ CASE WHEN (@MyElementValueDef IS NOT NULL) THEN N'AND TS.ElementValueDef = @MyElementValueDef ' ELSE '' END 
+ CASE WHEN (@MyElementPathFull IS NOT NULL) THEN N'AND TS.ElementPathFull = @MyElementPathFull ' ELSE '' END
+ N'ORDER BY TSO.TestSetName, TSO.TestCaseName, TS.ElementNo, TSO.InterchangeSeqNo, TSO.InstructionSeqNo, TSO.TransactionSeqNo;';

执行 sp_executesql @sql, N' @MyENVId INT, @MyUId INT, @MyMTId INT, @MyTestSetName NVARCHAR(MAX), @MyTestCaseName NVARCHAR(MAX), @MyInterchangeSeqNo SMALLINT, @MyInstructionSeqNo SMALLINT, @MyTransactionSeqNo SMALLINT, @MyElementValueDef NVARCHAR(MAX), @MyElementPathFull NVARCHAR(MAX)', @MyENVId、@MyUId、@MyMTId、@MyTestSetName、@MyTestCaseName、@MyInterchangeSeqNo、@MyInstructionSeqNo、@MyTransactionNSeqNo、@MyElementValueDef、@MyElementPathFull; 结束

【问题讨论】:

    标签: vb.net datagridview sql-server-2012


    【解决方案1】:

    如果您使用向导生成数据集,它会自动将自动增量设置为 -1,因为它无法知道数据库中的数据是什么,它不知道使用什么数字作为自动增量,在这种情况下,-1 将是最好的解决方案。

    在这里查看social.msdn.microsoft.com

    【讨论】:

    • “它无法知道数据库中的数据”是什么意思?如果所有其他列都正确显示,那么它肯定知道数据库中的数据...
    • 我很抱歉。我误解了这个问题。您能否发布一些代码,以便我们更好地了解这个问题?
    • 我不确定您要查找的代码,但这里有一些我认为可以提供信息的代码。添加到原始帖子中。
    • 所以,我的 GDV 填充了一个存储过程 (SP_XML_TEST_SET_FillByComboBoxes),它从组合框获取其值。我向我的 TabelAdapter 添加了一个新查询,它引用了存储过程。 (SP_XML_TEST_SET_FillByComboBoxes)。
    猜你喜欢
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    相关资源
    最近更新 更多