Acceder a MSSQL

Post Reply
User avatar
Willi Quintana
Posts: 859
Joined: Sun Oct 09, 2005 10:41 pm
Location: Cusco - Perú
Contact:

Acceder a MSSQL

Post by Willi Quintana »

Amigos, algún código lib o dll para acceder a MSSQL y poder leer datos y actualizaciones??, probe los demos pero no van,,,,
Salu2
Willi
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Acceder a MSSQL

Post by Rick Lipkin »

Willi

Connecting to Ms Sql server is no different than any other RDMS using ADO. One of the nice things about connecting to MS Sql is that SqlOleDb is native to all Windows OS thru Win8 32 bit and there is no need for any third party OleDb provider. Nothing is needed on the client workstation and nothing to configure.

Here is a sample connection using ADO and the link to download the Free Ms Sql Server 2012 Express.

Rick Lipkin

Code: Select all

// Sql Server test program

#Include "FiveWin.ch"


//----------------------------
Func Main()

Local xProvider,xSource,xCatalog,xUserId,xPassword,xConnect
Local oRs,cSql,oErr,Saying

Public oCn


xPROVIDER := "SQLOLEDB"                  // oledb provider
xSOURCE   := "RICKLIPKIN-PC\SQLEXPRESS"  // sql server name
xCATALOG  := "TRAVEL"                    // sql server database 
xUSERID   := "xxxxxxx"
xPASSWORD := "xxxxxxx"
xConnect  := 'Provider='+xPROVIDER+';Data Source='+xSOURCE+';Initial Catalog='+xCATALOG+';User Id='+xUSERID+';Password='+xPASSWORD // connection string

Try
  oCn  := CREATEOBJECT( "ADODB.Connection" )
Catch
  MsgInfo( "Could not create the ADO object for connection")
  Return(.f.)
End Try

TRY
  oCn:Open( xConnect )  // connection object 
CATCH oErr
  MsgInfo( "Could not open a Connection to Database "+xSource )
  RETURN(.F.)
END TRY

oRs := TOleAuto():New( "ADODB.Recordset" )
oRs:CursorType     := 1        // opendkeyset
oRs:CursorLocation := 3        // local cache
oRs:LockType       := 3        // lockoportunistic

cSQL := "SELECT * From [Staff]"
TRY
   oRs:Open( cSQL, oCn )
CATCH oErr
   Saying := "Error in Opening STAFF table to"+chr(10)
   Saying += "Import Legacy Data"+chr(10)
   MsgInfo( Saying )
   oCn:Close()
   RETURN(.F.)
END TRY

xBrowse( oRs )

oRs:Close()
oCn:Close()

Return(.t.)
 
http://www.microsoft.com/en-us/download ... x?id=29062
User avatar
Willi Quintana
Posts: 859
Joined: Sun Oct 09, 2005 10:41 pm
Location: Cusco - Perú
Contact:

Re: Acceder a MSSQL

Post by Willi Quintana »

Error in "oCn: Open (xConnect)" something must be wrong, as would do to connect to the IP, my data server has the IP 192.168.1.99 or http:serverdat.net?
regards
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Acceder a MSSQL

Post by Rick Lipkin »

Willi

You can use the IP address of your server or the Server name. Make sure your userid, password and database name are correct.

You can use the Microsoft SQL Management Studio that comes with Sql Server to login to your database to test your connection credentials.

Rick Lipkin
juan carlos bellucci
Posts: 115
Joined: Sat Mar 07, 2009 9:36 pm
Location: Argentina
Contact:

Re: Acceder a MSSQL

Post by juan carlos bellucci »

Hola Rick , le hago una pregunta. usted seria tan amable si posee una conexión de esta forma para MySql. ya que veo esta mas avanzado que yo.
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Acceder a MSSQL

Post by Rick Lipkin »

Juan

Take another look at my sample .. Ms Sql Server uses the built in Ole Provider called SqlOleDb which is on every Windows machine from Xp thru Windows 8.

There is nothing to configure or any setup needed on the workstation. That is what makes connecting to Ms Sql Server so nice. You do not need to distribute any Ole Client, run-time or Odbc to the desktop..

All you need to do is make sure your connection credentials are setup properly on your Sql Server and use ADO to manage and manipulate the data.

Rick Lipkin
Post Reply