• DİKKAT

    DOSYA İndirmek/Yüklemek için ÜCRETLİ ALTIN ÜYELİK Gereklidir!
    Altın Üyelik Hakkında Bilgi

vba ile sql bağlantısı

Katılım
14 Ekim 2017
Mesajlar
26
Excel Vers. ve Dili
excel 2016
merhaba,aşağıdaki kod ile sqle bağlanabiliyorum.tek bir tablodaki verileri çekebiliyorum.yalnız sqlde hazırladığım store procedur ile sorgulattığımda sadece ilk kayıtı getiriyor.hepsi gelmiyor.

neden böyle oluyor,fikti olan varmı.

teşekkür ederim.


Sub ADOExcelSQLServer()
' Carl SQL Server Connection
'
' FOR THIS CODE TO WORK
' In VBE you need to go Tools References and check Microsoft Active X Data Objects 2.x library
'

Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

Server_Name = "EXCEL-PC\EXCELDEVELOPER" ' Enter your server name here
Database_Name = "AdventureWorksLT2012" ' Enter your database name here
User_ID = "" ' enter your user ID here
Password = "" ' Enter your password here
SQLStr = "SELECT * FROM [SalesLT].[Customer]" ' Enter your SQL here

Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"

rs.Open SQLStr, Cn, adOpenStatic
' Dump to spreadsheet
With Worksheets("sheet1").Range("a1:z500") ' Enter your sheet name and range here
.ClearContents
.CopyFromRecordset rs
End With
' Tidy up
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub
 
Yazdığınız SP' yi görebilir miyiz? Konu bunun üzerine dönüyor anlaşılan.

*** Lütfen program kodları için CODE taglarını kullanın, okuması kolay oluyor. ***
 
Geri
Üst