3

Excel の VBA モジュール。ADODB.Connection とこの接続文字列を使用します。

"Provider=SQLOLEDB;Data Source=MARS;Initial Catalog=automation;Trusted_connection=yes;"

したい:

  1. INSERT INTO テスト (データ) VALUES ('何か')
  2. 新しく挿入された行の自動増分 ID (test.data_id) を取得します。
4

1 に答える 1

5
Dim identity as integer
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

cn.ConnectionString = "whatever..."
cn.Open
cn.Execute ("INSERT INTO test(data) VALUES ('hello world')")
rs.Open "SELECT @@identity AS NewID", cn
identity = rs.Fields("NewID") 
MsgBox (identity)
于 2013-02-10T23:39:08.467 に答える