Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Foxpro, ADO and updating records.... so confusing?!?
Message
 
 
À
18/07/2001 08:10:23
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00531917
Message ID:
00532292
Vues:
19
>Can somebody provide me with a connection string and recordset definition that let me select a record in the recordset, write a value to a record column and then update the recordset to the table?
*-- Declare the objects we'll use later, so that
*-- IntelliSense can be used

LOCAL oCat As adox.catalog
LOCAL oTable As Adox.table
LOCAL oConn As Adodb.connection
LOCAL oRs As Adodb.recordset
LOCAL oCmd As Adodb.command

*-- Setup code
SET EXCLUSIVE Off
Clear
RELEASE ALL
CLOSE ALL
CLEAR ALL

IF File("TestDBC.DBC")
	Erase TestDBC.DBC
Endif

IF File("TestDBC.Dct")
	Erase TestDBC.Dct
Endif

IF File("TestDBC.Dcx")
	Erase TestDBC.Dcx
Endif

IF File("TestTable.dbf")
	Erase TestTable.dbf
Endif

IF File("TestTable2.dbf")
	Erase TestTable2.dbf
Endif

*-- Three ADOX objects are needed:
*-- Catalog - equivalent to a DBC
*--	Connection - Similar to an ODBC connection
*--	Table - equivalent to a DBF
oCat = CREATEOBJECT("adox.catalog")
oConn = CREATEOBJECT("adodb.connection")
oTable = CREATEOBJECT("adox.table")

*-- Create a catalog using a connection string
*-- Values for Provider could also be:
*-- Vfpoledb.1
*-- Vfpoledb
oCat.Create("Provider=Vfpoledb.vfpoledb;Data Source=.\TestDBC.dbc")

*-- Code below is redundant, since creating the catalog
*-- creates a connection as well

*--	oConn.ConnectionString = "Provider=Vfpoledb.vfpoledb;Data Source=.\TestDBC.dbc"
*--	oConn.Open 
*--	oCat.ActiveConnection = oConn

*-- Create columns for our table
oTable.Columns.Append("Cust_ID", 3, 5) && adInteger, 5
oTable.Columns.Append("FName", 129, 20) && adChar
oTable.Columns.Append("LName", 129, 20)
oTable.Columns.Append("Addr", 129, 20)
oTable.Columns.Append("City", 129, 20)
oTable.Columns.Append("PostCode", 129, 10)

*-- Set all columns to be nullable, since they're non-nullable by default
FOR i = 0 TO oTable.Columns.Count - 1
	oTable.Columns(i).Attributes = 2  && adColNullable 
ENDFOR

oTable.Name = "TestTable"

*-- Once the table is created, it needs to be added to the catalog
oCat.Tables.Append(oTable)

*-- We need to change the connection mode, but since we can't do it 
*-- after the connection is opened, as with the Catalog.Create method,
*-- we'll create a new connection
oConn.ConnectionString = "Provider=Vfpoledb.vfpoledb;Data Source=.\TestDBC.dbc"
oConn.Mode = 16  && adModeShareDenyNone 
oConn.Open 

*-- Create our recordset, which is what ADO uses to collect data
*-- using a SELECT statement or other means
oRs = NEWOBJECT("adodb.recordset")
oRs.CursorLocation = 3  && adUseClient 
oRs.ActiveConnection = oConn
oRs.Open("select * from testtable", oConn, 3, 3)

*-- AddNew method using a technique similar to INSERT
oRs.AddNew("Cust_ID", 1234)

*-- Or do something like an APPEND BLANK, then fill in the fields
oRs.AddNew()
oRs.Fields("Cust_ID").Value = "12345"
oRs.Fields(1).Value = "Mike"
oRs.Fields("Lname").Value = "Stewart"
oRs.Fields("Addr").Value = "1234 Main St."
oRs.Fields("City").Value = "Redmond"
oRs.Fields("PostCode").Value = "98052"

*-- Since we did two AddNews, we'll do a batch update to gather both records
oRs.UpdateBatch 
oRs.Close()

*-- Verify that the data are there
USE testtable
BROWSE 

oRs.Open("select * from testtable where FName = 'Mike'", oConn, 3, 3)

*-- Now make a change to one of the fields
oRs.Fields("Fname").Value = "Michael"

*-- Only one row changed, so we'll just use Update
oRs.Update()
oRs.Close()

*-- Verify the change
USE testtable
BROWSE
Mike Stewart
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform