Home > Software engineering >  How will the value of the field in the SQL database, assigned to an array, urgent, please help! .
How will the value of the field in the SQL database, assigned to an array, urgent, please help! .

Time:11-01

Hope will be able to make the instance warrior

CodePudding user response:

You itself is a very good collection, transfer array?

CodePudding user response:

Instance, no...

CodePudding user response:

Don't use, if I want to take the first value of a field how to take, I really have not used,

CodePudding user response:

Suggest you learn how to read the first SQL database, how to pass a pointer to obtain want data

MoveFirst is a pointer to the most fields (" column ") available on the current cursor position column data
MoveNext is moved to the next line

In the last eof on behalf of the pointer

CodePudding user response:

The GetRows method using the record set

GetRows method (ADO)


Will you back to more than one record in the array object,

Syntax

Array=you. GetRows (Rows, Start, Fields)

The return value

Return to a two-dimensional array,

Parameter

Rows optional, long integer expression, specified number to retrieve records, the default value is adGetRowsRest (1),

Start optional, string or a long integer, will Start the GetRows operation records assigned to bookmarks, also can use the following BookmarkEnum value,

Constants that
AdBookmarkCurrent starting from the current record,
AdBookmarkFirst from the first record,
AdBookmarkLast since the last record,


Fields optional, on behalf of a single field name, the ordinal position, the number of array field name or serial number location change size, ADO just return data in these Fields,

Description

Use GetRows method can record from you copy into two-dimensional array, the first subscript id field, the second record number, when GetRows method returns data array variable is automatically adjusted to the correct size,

If you do not specify Rows, the value of the parameter will automatically retrieve GetRows method you all records in the object, if the request record is more than the available records, the GetRows return only available record number,

If you object to support bookmarks, will be sent to the record of a Bookmark attribute value to specify the location of the GetRows method should begin to retrieve data,

Fields such as to limit GetRows call returns, then you can send a single field in the Fields parameter name/number or field name/number arrays,

After calling GetRows next did not read the record has been becoming the current; If there are no more records, the EOF attribute set to True,

CodePudding user response:

This example USES GetRows method from you to retrieve the specified number of rows, and to fill the data into the array, in both cases GetRows method returns is less than the required number: a situation in which because the EOF, another case because GetRows attempting to retrieve has been other users delete data, only when the second case occurred when the function returns False, run the process need to use GetRowsOK function,

Public Sub GetRowsX ()

Dim rstEmployees As ADODB. You
Dim strCnn As String
Dim strMessage As String
Dim intRows As Integer
Dim avarRecords As the Variant
Dim intRecord As Integer

Employees' use of the name and date of employment in the table to open the recordset,
StrCnn="Provider=sqloledb;" & _
"Data Source=the SRV; Initial Catalog=pubs; User Id=sa; Password=; "
The Set rstEmployees=New ADODB. You
RstEmployees. Open the "SELECT fName, lName, hire_date" & amp; _
"FROM the Employee ORDER BY lName," strCnn,,, adCmdText

The Do While True
'get the user input the number of rows,
StrMessage="Enter the number of rows to retrieve."
IntRows=Val (InputBox (strMessage))

If intRows & lt;=0 Then Exit the Do

'if GetRowsOK successful printing results, please pay attention to whether reach the end of the file,
If GetRowsOK (rstEmployees, intRows, _
Then avarRecords)
If intRows & gt; UBound (avarRecords, 2) + 1 Then
The Debug. Print "(Not the enough records in" & amp; _
"You to retrieve" & amp; IntRows & amp; _
"Rows.)"
End the If
The Debug. Print UBound (avarRecords, 2) + 1 & amp; _
"No records found."

'print has to retrieve data,
For intRecord=0 To UBound (avarRecords, 2)
The Debug. Print "" & amp; _
AvarRecords (0, intRecord) & amp; "" & amp; _
AvarRecords (1, intRecord) & amp; ", "& amp; _
AvarRecords (2, intRecord)
Next intRecord
The Else
'assume GetRows error from other users of the data changes,
'use Requery refresh you and start again,
If MsgBox (" GetRows failed - retry?" , _
VbYesNo)=vbYes Then
RstEmployees. Requery
The Else
The Debug. Print "GetRows failed!"
The Exit Do
End the If
End the If

'due to the use of GetRows makes the current record pointer to visit the last record,
'so, before the loop back to another search will start record pointer back to you,
RstEmployees. MoveFirst
Loop

RstEmployees. Close

End Sub

The Public Function GetRowsOK (rstTemp As ADODB library. You, _
IntNumber As an Integer, avarData As the Variant) As Boolean

'to save GetRows method results in an array,
AvarData=https://bbs.csdn.net/topics/rstTemp.GetRows (intNumber)
'only when the number of rows returned is less than the required number of rows, rather than returns False, only because they arrived at the end you

If intNumber & gt; UBound (avarData, 2) + 1 And _
EOF Then Not rstTemp.
GetRowsOK=False
The Else
GetRowsOK=True
End the If

End the Function

CodePudding user response:

Estimate the original poster is not use database operation data, and don't want to spend a few minutes to understand, and even don't want to know, so I want to let others help to write code to database data into the array, so don't have to understand the database can directly use someone else's code to get your understanding an array of data, and then own to deal with these data, if so, have "wonderful work",

CodePudding user response:

GetRows method will save record set to array, the array type must be the Variant type, actually this understandable, because field type and length are uncertain,