Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
809 views
in Technique[技术] by (71.8m points)

c# - What can I do to set the value of a column in a new row in such a way that it have the same value of the auto increment primary key of the table?

I am absolutly new in C# (I came from Java) and I have the following problem

I have a method that execute an INSERT query to insert a new record into the table that is univocallu identify by an Id (the primary key)

So my table have some fields including: Id and VulnerabilityAllertId.

These two fields must have the same value.

At this time my method that perform the INSERT query work in this way:

I pass a VulnsSmall model object to the method and it create a new record in the table with each fields having the value of the related field in the model object

    public void insert(DataModel.Vulnerability.VulnSmall v) {

        _strSQL = "";
        string strSQLParametri = "";
        string query = "";

        System.Data.Common.DbCommand command;
        command = _connection.CreateCommand();

        // [VulnerabilityAlertId] insertion on the DB:
        if (v.VulnerabilityAlertId != null) {
            _strSQL = "INSERT INTO VulnerabilityAlertDocument ( [VulnerabilityAlertId] ";
            strSQLParametri = " VALUES (@VULNERABILITYALERTID ";
            addParameter(command, "@VULNERABILITYALERTID ", v.Id);
        }

        // [SourceId] insertion on the DB:
        if (!String.IsNullOrEmpty(v.SourceId))
        {
            _strSQL += ",[SourceId] ";
            strSQLParametri += ", @SOURCEID ";
            addParameter(command, "@SOURCEID", v.SourceId);
        }

        .................................
        .................................
        .................................   

        // [Language] insertion on the DB:
        if (v.Language != null)
        {
            _strSQL += ",[Language] ";
            strSQLParametri += ", @LANGUAGE ";
            addParameter(command, "@LANGUAGE", v.Language);
        }


        query = _strSQL + " ) " + strSQLParametri + " );";
        command.CommandText = query;
        _executeNoQuery(command);

    } 

My problem is that this code section is wrong:

        // [VulnerabilityAlertId] insertion on the DB:
        if (v.VulnerabilityAlertId != null) {
            _strSQL = "INSERT INTO VulnerabilityAlertDocument ( [VulnerabilityAlertId] ";
            strSQLParametri = " VALUES (@VULNERABILITYALERTID ";
            addParameter(command, "@VULNERABILITYALERTID ", v.Id);
        }

It is wrong because, in the database VulnerabilityAlertDocument ** table the **[VulnerabilityAlertId] column must have the same value of the Id primary key that is auto increment

In my previous code it use the value into v.VulnerabilityAlertId model field to set the [VulnerabilityAlertId] column value for my new row, so it have not the same value of the Id primary key column

What can I do to set the same value of the [Id] autoincrement column for the [VulnerabilityAlertId] column in my new row?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If i get your query correctly, you can make changes at database level by adding a mirror column to the primary key.

ALTER TABLE VulnerabilityAlertDocument
ADD mirror AS [Id] UNIQUE

and then you have a column which always replicate the value of primary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...