ASP SQL Server Connection - sql

<%
DIM objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "Data Source=123.123.12.123,1234;Database=DatabaseName;User Id=Usernm;Password=abcd1234;"
objConn.Open
DIM mySQL
mySQL = "SELECT * FROM [Users] WHERE [User ID]='1'"
DIM objRS
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.open(mySQL, objConn)
Response.Write objRS("FullName")
objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
%>
I want to connect to a SQL Server Database, read the data and close the connection. I have studied the examples and came up with this. But its not working. Please guide me. Where am I going wrong?

Some answers have suggested wrapping logic into functions there is no need for this.
It's just a lot of fluff that isn't needed, just use the ADODB.Command object. There are hundreds of ways to approach this but a method I have found to work time and again is let the ADODB.Command object do the work then return your results into an Array using .GetRows() method of the ADODB.Recordset object.
That way you can close off both the ADODB.Recordset and ADODB.Command objects quickly and work just with the Array.
Dim conn, cmd, rs, sql, data, search
'Assume value to query comes from a Request Collection.
search = Request("myvalue") & ""
conn = "Data Source=123.123.12.123,1234;Database=DatabaseName;User Id=Usernm;Password=abcd1234;"
sql = "select from mytable where this = ?"
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
'No need to handle connection let ADODB.Command create and destory it.
.ActiveConnection = conn
.CommandType = adCmdText
.CommandText = sql
.Parameters.Append(.CreateParameter("#myparam", adVarWChar, adParamInput, 50))
.Parameters("#myparam").Value = search
Set rs = .Execute()
If Not rs.EOF Then data = rs.GetRows()
Call rs.Close()
Set rs = Nothing
End with
Set cmd = Nothing
'ADODB.Connection is closed when ADODB.Command is destroyed.
If IsArray(data) Then
rows = UBound(data, 2)
For row = 0 To rows
'Return first column of the current row
Call Response.Write("First Column of Row " & row & " is '" & data(0, row) & "'<br />"
Next
Else
Call Response.Write("No records")
End If

Dim rs, dbConn
Function OpenDB()
Set dbConn = Server.CreateObject("ADODB.Connection")
dbConn.ConnectionTimeout = 300
dbConn.CommandTimeout = 300
dbConn.Open "Data Source=123.123.12.123,1234;Database=DatabaseName;User Id=Usernm;Password=abcd1234;"
End Function
Function CloseDB()
Set rs = Nothing
if ucase(TypeName(dbConn)) = "CONNECTION" then
dbConn.Close
Set dbConn = Nothing
end if
End Function
Function OpenRecordSet(recset, tablename)
Call OpenDB()
Set recset = Server.CreateObject("ADODB.Recordset")
recset.Open tablename, dbConn, 0, 1
End Function
Function CloseRecordSet(recset)
Set recset = Nothing
Call CloseDB()
End Function
Then use
<%
Call OpenDB()
sql = "select from mytable where this = 'that'"
Set rs = dbConn.Execute(sql)
if not rs.EOF then
' do your stuff!
end if
Call CloseDB()
%>
http://www.shiningstar.net/articles/articles/database/datafunctions.asp?ID=AW

Related

Is it possible to pass a parameter from MS Access to a SQL Server stored procedure and display the output in a datasheet form?

Users in MS Access enter values in a field that is passed as a parameter to a stored procedure in a SQL Server database. The stored procedure will return a result set with two columns. I would like to display that result set in a datasheet form. The code below almost does that, but will only display the final row of the result set. I confirmed that the stored procedure is being passed to the db correctly and that the test values I'm using should return 4 rows. The code in comments are a few of the many things that I have tried that didn't work.
Private Sub btnBulkLink_Click()
Dim cmd As New ADODB.Command
Dim rst As New ADODB.Recordset
With cmd
.CommandType = adCmdStoredProc
.ActiveConnection = "Driver=SQL Server Native Client 11.0;Server=xxxxxxxxxxxxxxxxx;Database=xxxxx;Trusted_Connection=Yes"
.CommandText = "dbo.usp_FindResolutionsLinkedToServices"
.CommandTimeout = 180
.NamedParameters = True
End With
cmd.Parameters.Append cmd.CreateParameter("#PprsRefINP", adLongVarChar, adParamInput, Len(txtBulkLink.Value), txtBulkLink.Value)
Set rst = cmd.Execute
DoCmd.OpenForm FormName:="ServiceResolutionLink", View:=acFormDS, DataMode:=acFormReadOnly, WindowMode:=acHidden
' Set Forms!ServiceResolutionLink.Recordset = rst
With rst
Do While Not .EOF
Forms("ServiceResolutionLink").txtPprsRef = rst!PprsRef
Forms("ServiceResolutionLink").txtSubmissionId = rst!SubmissionId
.MoveNext
Loop
End With
' Forms("ServiceResolutionLink").txtPprsRef = rst!PprsRef
' Forms("ServiceResolutionLink").txtSubmissionId = rst!SubmissionId
' Forms("ServiceResolutionLink").Visible = True
DoCmd.OpenForm FormName:="ServiceResolutionLink", View:=acFormDS
rst.Close
End Sub
Ok, we assume you setup a pass-though query. (and it set with returns records = true).
And if your datasheet is a REAL sub form (setup as datasheet - not a table).
Then this code will work:
Private Sub Command2_Click()
With CurrentDb.QueryDefs("qryPassR")
.SQL = "exec dbo.GetHotels2 #City = '" & "Banff" & "'"
End With
Me.MyDataSheet.Form.RecordSource = "qryPassR"
End Sub
And if you are using a real datasheet (form that does not exist), then you can have the form set to
Query.PassR
The trick (or challenge) then becomes to have the form NOT load until such time you setup the query with the correct stored procedure call.
I was in the midst of testing suggestions from others when my boss figured this out. Here is our final code:
Private Sub Form_Load()
Dim rst As New ADODB.Recordset
Dim cmd As New ADODB.Command
Dim strPPRSRef As String
Dim cn As New ADODB.Connection
With cn
.Provider = "sqloledb"
cn.Properties("Data Source").Value = "[server]"
cn.Properties("Initial Catalog").Value = "[db]"
cn.CursorLocation = adUseClient
' Windows authentication.
cn.Properties("Integrated Security").Value = "SSPI"
' .CursorLocation = adUseClient
End With
cn.Open
With cmd
.CommandType = adCmdStoredProc
.ActiveConnection = cn
.CommandText = "dbo.usp_FindResolutionsLinkedToServices"
.CommandTimeout = 180
.NamedParameters = True
End With
strPPRSRef = Forms("Resolution_Tracker").txtBulkLink.Value
cmd.Parameters.Append cmd.CreateParameter("#PprsRefINP", adLongVarChar, adParamInput, Len(strPPRSRef), strPPRSRef)
With rst
.CursorType = adOpenStatic
.CursorLocation = adUseClient
End With
Set rst = cmd.Execute
Set Me.Recordset = rst
rst.Close
Set rst = Nothing
cn.Close
Set cn = Nothing
End Sub

If i create a disconnected ADO recordset from scratch in VBA how do i set the base table information for UpdateBatch?

I have been using disconnected recordsets for a few weeks now, typically retrieving data from SQL Server, disconnecting the rs and filtering/formatting in VBA. Now i'm trying to do the reverse and create a new ADO recordset from scratch, and then connect it to my database and use UpdateBatch to insert the recordset into the database without using a loop. I have a fully populated recordset at this point, hooked it back up to my connection string, and try UpdateBatch. Understandably, it has no information at this point about what table I'm trying to update (only Data Source and Initial Catalog via the connection string). Is there a recordset property that I use to provide the table in question? Additionally, the table I'm trying to import into has a GUID field (first field) that I have left blank on purpose in my disconnected recordset assuming that upon import, SQL Server would assign this GUID/primary key automatically.
The specific error I'm getting after "rs.UpdateBatch" is
Run-time error '-2147467259 (80004005)'"
Insufficient base table information for updating or refreshing.
I know I could use a loop and a SQL command "INSERT INTO ...". I'd like to use a recordset object though since those provide much more functionality as a container for data. One thing I haven't tried is to first retrieve a recordset from the table in question, then clear it and re-populate it with the new data so that the recordset itself retains all of the original database and table properties. If that's the only/best approach I can try that route too. I just wanted to see if it was possible to create an ADO recordset, populate it, and then insert it into a matching table of my choice.
dim rs as ADODB.Recordset
set rs = New ADODB.Recordset
With rs.Fields
.append "alias", adVarChar, 255
.append "textA", adVarChar, 255
.append ......
End With
rs.Open
rs.AddNew Array(0, 1, 2, ..., n), Array(val0, val1, val2, ..., valn)
rs.Update
call importRS(rs)
rs.close
set rs = nothing
After rs.update above some recordsets may need to go to a database, other recordset objects are just used to expedite filtering and sorting so I just use them as a convenient container and they'd never go to importRS()
However, IF I need to send the disconnected recordset to a database, i'd like to just pass the recordset object to another function that serves the purpose of opening the connection, sending the update, and closing the connection. The code below would serve that purpose which is why i'd like to wait to establish a connection until this point, right at the end after my rs is populated.
sub importRS(byref rs as ADODB.Recordset)
dim cn as ADODB.Connection
set cn = New ADODB.Connection
cn.ConnectionString = strConnection 'my connection string variable'
cn.Open
rs.ActiveConnection = cn
rs.UpdateBatch '-------error message appears on this line
cn.close
set cn = nothing
You can get the data, (wherever it may be) into an array and add to the recordset using a loop. Then then when the loop is finished, you do rs.updatebatch as follows:
Private Sub SaveToSQLSever()
Dim lngLastRow As Long
Dim arrySheet As Variant
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
Dim strCn As String
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
strCn= "Provider=VersionOfSQL;User ID=*********;Password=*********;"
& _ "Data Source=ServerName;Initial Catalog=DataBaseName"
cn.Open strCn
On Error Goto exiting
'*********************************************************
'If the data is coming from a sheet
'Set to your Range
With Sheets("SheetName")
lngLastRow = .Range("A2").CurrentRegion.Rows _
(.Range("A2").CurrentRegion.Rows.Count).Row
arrySheet = .Range("A1:G" & lngLastRow).Value2
End With
'Else populate the array and pass it to this Sub
'*************************************************************
'Note the property parameters
'.Source = Table That you want to populate
With rs
.ActiveConnection = cn
.Source = "Select * from TableName"
.CursorType = adOpenDynamic
.CursorLocation = adUseClient
.LockType = adLockBatchOptimistic
.Open
End With
For i = LBound(arrySheet,1) To UBound(arrySheet,1)
rs.AddNew
For j = LBound(arrySheet,2) To UBound(arrySheet,2)
rs.Fields(j).Value = arrySheet(i,j)
Next j
rs.MoveNext
Next i
rs.UpdateBatch 'Updates the table with additions from the array
i = 0
'******************************************************************
'Note that you can also refer to the Field Names Explicitly Like So:
For i = LBound(arryData,1) To UBound(arryData,1)
With rs
.AddNew
.Fields("FieldName1").Value = arryData(i,1)
.Fields("FieldName2").Value = arryData(i,2)
.Fields("FieldName3").Value = arryData(i,3)
.Fields("FieldName4").Value = arryData(i,4)
.Fields("FieldName5").Value = arryData(i,5)
.Fields("FieldName6").Value = arryData(i,6)
.Fields("FieldName7").Value = arryData(i,7)
End With
Next i
rs.UpdateBatch
'******************************************************************
MsgBox "The data has successfully been saved to the SQL Server", _
vbInformation + vbOKOnly,"Alert: Upload Successful"
exiting:
If cn.State > 0 Then cn.Close
If rs.State > 0 Then rs.Close
Set cn = Nothing
Set rs = Nothing
End Sub
Edit: As per OP's request to pass an existing recordset to a SQL table, below should do so:
Private Sub SendRcrdsetToSQL(ByRef rsIn As ADODB.Recordset)
Dim arrySheet As Variant
Dim rsSQL As ADODB.Recordset
Dim cn As ADODB.Connection
Dim strCn As String
Set cn = New ADODB.Connection
strCn= "Provider=VersionOfSQL;User ID=*********;Password=*********;"
& _ "Data Source=ServerName;Initial Catalog=DataBaseName"
cn.Open strCn
On Error Goto exiting
Set rsSQL = New ADODB.Recordset
With rsSQL
.ActiveConnection = cn
.Source = "Select * from TableName"
.CursorType = adOpenDynamic
.CursorLocation = adUseClient
.LockType = adLockBatchOptimistic
.Open
End With
'disconnect the recordset and close the connection
Set rsSQL.ActiveConnection = Nothing
cn.Close
Set cn = Nothing
rsIn.MoveFirst
rsSQL.MoveLast
'Add the records from the passed recordset to the SQL recordset
Do While Not rsIn.EOF
With rsSQL
.AddNew
.Fields("FieldName1").Value = rsIn.Fields("FieldName1").Value
.Fields("FieldName2").Value = rsIn.Fields("FieldName2").Value
.Fields("FieldName3").Value = rsIn.Fields("FieldName3").Value
.Fields("FieldName4").Value = rsIn.Fields("FieldName4").Value
.Fields("FieldName5").Value = rsIn.Fields("FieldName5").Value
.Fields("FieldName6").Value = rsIn.Fields("FieldName6").Value
.Fields("FieldName7").Value = rsIn.Fields("FieldName7").Value
End With
rsIn.MoveNext
Loop
rsSQL.UpdateBatch
MsgBox "The data has successfully been saved to the SQL Server", _
vbInformation + vbOKOnly,"Alert: Upload Successful"
exiting:
If cn.State > 0 Then cn.Close
If rsIn.State > 0 Then rsIn.Close
If rsSQL.State > 0 Then rsSQL.Close
Set cn = Nothing
Set rsIn = Nothing
Set rsSQL = Nothing
End Sub
The only way I was able to get this to work was by running a query to build the structure of my Recordset. So your code becomes something like this:
Private Sub Command1_Click()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.ConnectionString = "<your connection string>"
cn.CursorLocation = adUseClient
cn.Open
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set rs.ActiveConnection = cn
rs.Open "select * from states where 1<>1", , adOpenStatic, adLockBatchOptimistic
rs.AddNew Array("Abbrev", "Name", "Region", "SchoolDataDirect"), Array("TN", "TestName", "MyRegion", 1)
Set rs.ActiveConnection = Nothing
cn.Close
ImportRS rs
End Sub
Private Sub ImportRS(ByRef rs As ADODB.Recordset)
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.ConnectionString = "<your connection string>"
cn.CursorLocation = adUseClient
cn.Open
Set rs.ActiveConnection = cn
rs.UpdateBatch
Set rs.ActiveConnection = Nothing
cn.Close
End Sub

Excel VBA stored procedure with parameters not working?

I am struggling bit here with a stored procedure with parameters in VBA. The code below without parameters working fine but with parameters not working.
My code:
Sub CopyDataFromDatabase()
Dim Conn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim Fields As ADODB.Field
Dim Cmd As ADODB.Command
Set Conn = New ADODB.Connection
Set Cmd = New ADODB.Command
Set Rs = New ADODB.Recordset
Conn.Open "My connection string here--------"
Cmd.CommandType = adCmdStoredProc
Cmd.Parameters.Append Cmd.CreateParameter("#Division", adVarChar, adParamInput, 40)
Cmd.Parameters("#Division").Value = "South"
Cmd.Parameters.Append Cmd.CreateParameter("#Area", adVarChar, adParamInput, 40)
Cmd.Parameters("#Area").Value = "IT"
Cmd.CommandText = "My SP here------"
Set Rs = Cmd.Execute
On Error GoTo CloseRecordset
Worksheets.Add
For Each Fields In Rs.Fields
ActiveCell.Value = Fields.Name
ActiveCell.Font.Bold = True
ActiveCell.Font.Underline = True
ActiveCell.HorizontalAlignment = xlCenter
ActiveCell.Interior.Color = RGB(0, 128, 255)
ActiveCell.Font.Color = RGB(255, 255, 255)
ActiveCell.Offset(0, 1).Select
Next Fields
Range("A1").Select
Range("A2").CopyFromRecordset Rs
CloseRecordset:
Rs.Close
Set Rs = Nothing
Set Cmd = Nothing
CloseConnection:
Conn.Close
Set Conn = Nothing
End Sub
When I run, its not giving any error, just showing like executing but no result
Can anybody suggest where I am doing wrong? Thanks
I have successfully declared a variant array and populated the parameters (in order!) into that array, then passed to the array into the execute method to execute a stored procedure.
Assuming your stored proc expects 'Division' then 'Area', something like this may do the trick:
Sub CopyDataFromDatabase()
Dim Conn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim Fields As ADODB.Field
Dim Cmd As ADODB.Command
'New variable
Dim v_Params(1 To 2) As Variant 'assuming you have 2 parameters
Set Conn = New ADODB.Connection
Set Cmd = New ADODB.Command
Set Rs = New ADODB.Recordset
Conn.Open "My connection string here--------"
v_Params(1) = "South"
v_Params(2) = "IT"
With Cmd
.ActiveConnection = Conn
.CommandType = adCmdStoredProc
.CommandText = "My SP here------"
.CommandTimeout = 0
Set rs = .Execute(, v_Params)
End With
See if that works, as I am currently using this method successfully. I didn't see the need to modify the rest of your subroutine.
I used the With Cmd and End With to avoid fully qualifying the reference each time.
Updated
The issue, found by the author of the question, was that the SP was timing out when parameters were passed into it. The resolution was to set the CommandTimeout property to 0.

Connecting to sql server 2008 using vb

Function DBConnect()
Dim vConnString, wfConnection, objConn
set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open Application("DB_CONNECT")
set DBConnect = objConn
exit function
Response.Write("connected to Server 2008")
End Function
Function GetValue()
Dim objCmd, objRS
Set objCMD = Server.CreateObject("ADODB.Command")
Set objRs = Server.CreateObject("ADODB.Recordset")
With (objCMD)
.ActiveConnection = DBConnect()
.CommandType = adCmdStoredProc
.CommandText = "select * from Acc.dbo.table"
set ObjRS = .Execute()
End With
if err.number = 0 then
if not objRs.EOF then
arrData = objRs.GetRows
vDesc = arrData(5,0)
else
vDesc = vValue
end if
GetDescFromCode = True
end if
Response is Coming as
connected to Server 2008
While debuging, i got that its not going inside objRs.EOF if loop...any idea whats wrong
In DBConnect, check the status of objConn to make sure it's really being opened.
In DBConnect, you have exit function before response.write.
In GetValue, check value of err.number.

How to call a stored procedure using asp classic?

I am using sql server and asp classic, and am currently calling queries like this:
newHireSQL = "select * from NewHire where Archived = 0 order by HireID desc"
Set rsGetHireID = Server.CreateObject("ADODB.Recordset")
rsGetHireID.Open newHireSQL,ConnectionString,adOpenStatic
NumOfHireID = rsGetHireID.RecordCount
But instead of having the query statement here, I want to call a stored procedure called dbo.sp_selectNewHireSQL. How can I do that?
Thanks
EDIT:
I tried this
Dim Conn
SET Conn = Server.CreateObject("ADODB.Connection")
SET rsGetHireID = Server.CreateObject("ADODB.RecordSet")
Conn.Open ConnectionString
set rsGetHireID=Conn.Execute("Exec sp_selectNewHireSQL")
NumOfHireID = rsGetHireID.RecordCount
Response.Write (NumOfHireID)
But I am getting a -1 value for the record count.
It's just use exec or execute statement:
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open ConnectionString
Conn.Execute "Exec sp_selectNewHireSQL"
Reference: http://msdn.microsoft.com/en-us/library/ms188332(v=sql.90).aspx
To have the RecordCount working you need to use proper cursor:
Set rsGetHireID = Server.CreateObject("ADODB.RecordSet")
Conn.Open ConnectionString
rsGetHireID.CursorLocation = 3 'adUseClient
rsGetHireID.Open "Exec sp_selectNewHireSQL", Conn
NumOfHireID = rsGetHireID.RecordCount
Instead of using ADODB.Connection, try using ADODB.Command instead like this:
Set objCommand = Server.CreateObject("ADODB.Command")
objCommand.ActiveConnection = ConnectionString
objCommand.CommandText = "dbo.sp_selectNewHireSQL"
objCommand.CommandType = adCmdStoredProc ' you have to include adovbs.inc file or you can use 4
Set rsGetHireID = objCommand.Execute()
NumOfHireID = rsGetHireID.RecordCount
Response.Write (NumOfHireID)
<%
dim db_conn
db_conn = "Provider=SQLOLEDB.1;Server=server name;Database=dbname;Uid=sa; Pwd=123;"
set conn = server.createobject("adodb.connection")
set Cmd = Server.CreateObject("ADODB.Command")
'-------------------------------------------------------
conn.open (db_conn)
'-------------------------------------------------------
set rs = Server.CreateObject("ADODB.RecordSet")
sSQL = "EXEC sp_countrylist #countryname ='" & countryname & "'"
set rs = conn.execute(sSQL)
if (rs.bof and rs.eof) then
response.Write "<span class=""error"">No Record Found</span>"
response.End
end if %>