繁体   English   中英

如何使用普及SQL读取插入数据库表中的记录的标识?

[英]How to read the identity of a record inserted into database table with Pervasive SQL?

我正在开发C#Windows Form应用程序,需要将记录插入表中,然后读回记录的标识并将该标识传递给程序的另一部分。 请参见下面的代码。

ID_COLUMN具有PSQL类型标识。 另请注意:

普及型SQL不支持SCOPE_IDENTITY。 在文档中,@@ identity返回最近插入的Identity列的值。 如果表中没有标识列,则@@ identity将返回null。 如果您有两次插入,则@@ identity返回最后插入的值。

其他说明:Pervasive版本是v11,并且Pervasive随附了Using using data provider。

using Pervasive.Data.SqlClient;

int newIdentity = 0;
string connectionString = 
  "Server Name=myServerAddress;Database Name=myDataBase;User ID=myUsername;"
PsqlConnection connection = new PsqlConnection(connectionString); 
string insertStatment= 
  "INSERT INTO TABLE_1 ( ID_COLUMN, DATA_COLUMN_1, DATA_COLUMN_2 ) " + 
  "        VALUES ( 0, 'My First Name', 'My Last Name' )";
try{
   PsqlCommand insertCommand = new PsqlCommand(insertStatment,connection);
   connection.Open();
   int rowCount = insertCommand.ExecuteNonQuery();
   if ( rowCount > 0 )
      {
         string selectIdentityStatment =
            " SELECT @@IDENTITY FROM TABLE_1 ";
         PsqlCommand selectIdentity =
         new PsqlCommand(selectIdentityStatment, connection );
         newIdentity =
           (int)selectIdentity.ExecuteScalar();  // THIS IS RETURNING NULL
       }
       else
        { // nothing was inserted
          // newIdentity = 0
        }
      }
Finally {
         Connection.Close();
        }

任何帮助将不胜感激。

新版本的Actian PSQL Ado.net提供程序随PSQL V12和V13提供。 如果当前版本有任何问题,请告知我们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM