2016-04-07 11 views
1

içinde SELECT MAX (id) yüklemek için Tablodan son id değerini almakta sorun yaşıyorum.nerededir

Tüm sütunu yüklediğimden bu yana bana bir sorun verecektir (DataTable sınırına sahiptir) bu yüzden başka bir çözüm aramaya karar verdim, ancak çalışmam için şansım yok.

Dize komutunu sahibim ancak döndürülen değeri nerede saklayacağımı bilmiyorum.

 Dim query As String = "SELECT MAX(id) AS LastId FROM Table1" 
    Dim dtmain As New DataTable 
    Try 
     With sqlcmd 
      .CommandText = query 
      .Connection = sqlcon 
     End With 

     With sqladp 
      .SelectCommand = sqlcmd 
      .Fill(dtMain) 
     End With 
     MsgBox("last id = " & dtmain.Rows(0)("LastId")) 
    Catch ex As Exception 

    End Try 

ben bir şey almak ve msgbox bile göstermez: İşte benim kodudur.

not: Geçen tablosundan id ve NOT sokulan idistiyoruz.

Bu benim referanslar ve bildirimleri şunlardır:

Imports System.Data.OleDb 
    Private conn As OleDbConnection 
    Private adapter As OleDbDataAdapter 
    Private cmdd As OleDbCommand 

de benim proje referansta Microsoft ADO Ext 2.8 for DDL and Security eklendi

cevap

1

verilerinizin kaynağı mevcut veritabanında ise:

Dim dbConnect As Database 
Dim rstRecordset As DAO.Recordset 

Set dbConnect = Access.CurrentDb 

Dim query As String = "SELECT MAX(id) AS LastId FROM Table1" 

Set rstRecordset = dbConnect .OpenRecordset(query) 
MsgBox("last id = " & rstRecordset.Fields(0).value & "")) 

dbConnect.Close 
Set dbConnect = Nothing 
Set rstRecordset = Nothing 

ise Verilerinizin kaynağı başka bir Access veritabanındadır:

Dim cnnConn As ADODB.connection 
Dim rstRecordset As ADODB.Recordset 
Dim cmdCommand As ADODB.Command 

' Open the connection. 
Set cnnConn = New ADODB.connection 
With cnnConn 
    .ConnectionString = _ 
     "Provider=Microsoft.Jet.OLEDB.4.0" 
    .Properties("Jet OLEDB:Database Password") = "pass" 
    .Open "D:\Databases\Database.mdb" 
End With 

' Set the command text. 
Set cmdCommand = New ADODB.Command 
Set cmdCommand.ActiveConnection = cnnConn 
With cmdCommand 
    .CommandText = "SELECT MAX(id) AS LastId FROM Table1" 
    .CommandType = adCmdText 
    .Execute 
End With 

' Open the recordset. 
Set rstRecordset = New ADODB.Recordset 
Set rstRecordset.ActiveConnection = cnnConn 
rstRecordset.Open cmdCommand 
MsgBox("last id = " & rstRecordset.Fields(0).value & "")) 

' Close the connections and clean up. 
cnnConn.Close 
Set cmdCommand = Nothing 
Set rstRecordset = Nothing 
Set cnnConn = Nothing 
+0

Hata "DAO.Recordset" yazın tanımlı değil. \t Hata 'CurrentDB' bildirilmedi. Koruma seviyesi nedeniyle erişilemez. \t "Microsoft ActiveX Veri Nesneleri 2.x Kitaplığı'nı ekleyerek hataları gidermeyi denedim, ancak ikinci hatayı düzeltmedim." İtiraflarımı ekleyeceğim belki de – pwalaako2

+0

yardım etmeden önce adodb'i öğrenmem gerekiyor gibi görünüyor işlevi tamamen kullanabilir ... şimdilik oy kullanacağım :) – pwalaako2