{"id":79,"date":"2009-10-25T17:10:02","date_gmt":"2009-10-25T17:10:02","guid":{"rendered":"http:\/\/blog.rabihtawil.com\/?p=67"},"modified":"2010-07-22T13:46:51","modified_gmt":"2010-07-22T13:46:51","slug":"how-to-connect-to-sql-server-database-from-visual-foxpro-9","status":"publish","type":"post","link":"http:\/\/www.raytawil.com\/?p=79","title":{"rendered":"How to Connect to SQL Server Database from Visual FoxPro 9"},"content":{"rendered":"<p>In Microsoft public newsgroups, I&#8217;ve noticed a recent increase in the number of questions that deal with how to connect from Visual Foxpro to SQL Server, and the problems related to making this connection. So I&#8217;ve decided to write this article\u00a0 to cover such an important topic.<\/p>\n<p>There are two functions that can be used to establish a connection with the a remote SQL Server from Visual FoxPro:<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\">SQLConnect()<\/span><\/li>\n<li><span style=\"color: #0000ff;\">SQLStringConnect()<\/span><\/li>\n<\/ul>\n<p><span class=\"textBoldBlue\">The <span style=\"color: #0000ff;\">SQLConnect()<\/span> Function<\/span><\/p>\n<p>There are two ways to use the <span style=\"color: #0000ff;\">SQLConnect()<\/span> function to connect to a remote data source, such as SQL Server. The first requires that you supply the name of a data source as defined in the ODBC Data Source Administrator applet of the Control Panel.<\/p>\n<p>The following example creates a connection to a remote server using the\u00a0ODBCNorthwind DSN:<\/p>\n<p><span style=\"color: #0000ff;\"><span class=\"textCode\">LOCAL hConn<\/p>\n<p>hConn = SQLConnect(&#8220;ODBCNorthwind&#8221;, &#8220;sa&#8221;, &#8220;&#8221;)<\/span><\/span><\/p>\n<p>The second way to use<span style=\"color: #0000ff;\"> SQLConnect()<\/span> is to supply the name of a Visual FoxPro\u00a0 connection that was created using the create connection command. The CREATE\u00a0CONNECTION command stores the metadata that Visual FoxPro needs to connect to a remote data source.<\/p>\n<p>The following example creates a Visual FoxPro connection named Northwind and then connects to the database described by the connection:<\/p>\n<p><span class=\"textCode\"><span style=\"color: #0000ff;\">LOCAL hConn<\/p>\n<p>CREATE DATABASE cstemp<\/p>\n<p>CREATE CONNECTION Northwind ;<\/p>\n<p>DATASOURCE &#8220;ODBCNorthwind&#8221; ;<\/span><\/p>\n<p><span style=\"color: #0000ff;\">USERID &#8220;sa&#8221; ;<\/p>\n<p>PASSWORD &#8220;&#8221;<\/p>\n<p>hConn = SQLConnect(&#8220;Northwind&#8221;)<\/span><\/span><\/p>\n<p><span class=\"textBoldBlue\"><span style=\"color: #0000ff;\">SQLStringConnect()<\/span> Function<\/span><\/p>\n<p>The other function that can be used to establish a connection to a remote data source, such as SQL Server, is <span style=\"color: #0000ff;\">SQLStringConnect()<\/span>. Unlike <span style=\"color: #0000ff;\">SQLConnect()<\/span>, <span style=\"color: #0000ff;\">SQLStringConnect()<\/span> requires a single parameter, a string of semicolon-delimited options that describes the remote data source and optional connections settings.<\/p>\n<p>The valid options are determined by the requirements of the ODBC driver. Specific requirements for each ODBC driver can be found in that ODBC driver&#8217;s documentation.<\/p>\n<p>The following table lists some commonly used connection string options for SQL Server:<\/p>\n<div>\n<table id=\"AutoNumber1\" style=\"border-collapse: collapse;\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" width=\"90%\">\n<tbody>\n<tr>\n<td width=\"149\"><span class=\"textBold\">Option<\/span><\/td>\n<td width=\"438\"><span class=\"textBold\">Description<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"149\">DSN<\/td>\n<td width=\"438\">References an ODBC DSN.<\/td>\n<\/tr>\n<tr>\n<td width=\"149\">Driver<\/td>\n<td width=\"438\">Specifies the name of the ODBC driver to use.<\/td>\n<\/tr>\n<tr>\n<td width=\"149\">Server<\/td>\n<td width=\"438\">Specifies the name of the SQL Server to connect to.<\/td>\n<\/tr>\n<tr>\n<td width=\"149\">UID<\/td>\n<td width=\"438\">Specifies the login ID or username.<\/td>\n<\/tr>\n<tr>\n<td width=\"149\">PWD<\/td>\n<td width=\"438\">Specifies the password for the given login ID or username.<\/td>\n<\/tr>\n<tr>\n<td width=\"149\">Database<\/td>\n<td width=\"438\">Specifies the initial database to connect to.<\/td>\n<\/tr>\n<tr>\n<td width=\"149\">APP<\/td>\n<td width=\"438\">Specifies the name of the application making the connection.<\/td>\n<\/tr>\n<tr>\n<td width=\"149\">WSID<\/td>\n<td width=\"438\">The name of the workstation making the connection.<\/td>\n<\/tr>\n<tr>\n<td width=\"149\">Trusted_Connection<\/td>\n<td width=\"438\">Specifies whether the login is being validated by the Windows NT Domain.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Not all of the options listed in the above table have to be used for each connection.<\/p>\n<p>For instance, if you specify the Trusted_Connection option and connect to SQL Server using NT Authentication, there is no reason to use the UID and PWD options since SQL Server would invariably ignore them. The following code demonstrates some examples of using <span style=\"color: #0000ff;\">SQLStringConnect()<\/span>.<\/p>\n<p>Note: You can use the name of your server instead of the string.<\/p>\n<p>SQL Server 2000 code example:<span class=\"textCode\"><\/p>\n<p><span style=\"color: #0000ff;\">LOCAL hConn<\/p>\n<p>hConn = SQLStringConnect(&#8220;Driver=SQL Server;Server=&lt;SQL2000&gt;;&#8221;+ ;<\/p>\n<p>UID=sa;PWD=;Database=Northwind&#8221;)<\/p>\n<p>hConn = SQLStringConnect(&#8220;DSN=ODBCNorthwind;UID=sa;PWD=;Database=Northwind&#8221;)<br \/>\n<\/span><br \/>\n<span style=\"color: #0000ff;\">hConn =<br \/>\nSQLStringConnect(&#8220;DSN=ODBCNorthwind;Database=Northwind;Trusted_Connection=Yes&#8221;)<\/span><\/span><\/p>\n<p><span class=\"textBoldBlue\">Handling Connection Errors<\/span><\/p>\n<p>Both the <span style=\"color: #0000ff;\">SQLConnect()<\/span> and <span style=\"color: #0000ff;\">SQLStringConnect()<\/span> functions return a connection\u00a0handle. If<br \/>\nthe connection is established successfully, the handle will be a positive\u00a0integer. If Visual FoxPro failed to make the connection, the handle will\u00a0contain a negative integer. A simple<br \/>\ncall to the<span style=\"color: #0000ff;\"> AERROR()<\/span> function can be used to retrieve the error number and\u00a0 message. The following example traps for a failed connection and displays the error number and message using the Visual FoxPro <span style=\"color: #0000ff;\">MESSAGEBOX()<\/span> function.<\/p>\n<p>Visual FoxPro returns error 1526 for all errors against a remote data source. The fifth element of the array returned by <span style=\"color: #0000ff;\">AERROR()<\/span> contains the remote data source-specific error.<\/p>\n<p><span style=\"color: #0000ff;\"><span class=\"textCode\">#define MB_OKBUTTON 0<\/p>\n<p>#define MB_STOPSIGNICON 16<\/p>\n<p>LOCAL hConn<\/p>\n<p>hConn = SQLConnect(&#8220;ODBCNorthwind&#8221;, &#8220;falseuser&#8221;, &#8220;&#8221;)<\/p>\n<p>IF (hConn &lt; 0)<\/p>\n<p>LOCAL ARRAY laError[1]<\/p>\n<p>AERROR(laError)<\/p>\n<p>MESSAGEBOX( ;<\/p>\n<p>laError[2], ;<\/p>\n<p>MB_OKBUTTON + MB_STOPSIGNICON, ;<\/p>\n<p>&#8220;Error &#8221; + TRANSFORM(laError[5]))<\/p>\n<p>ENDIF<\/span><\/span><\/p>\n<p><span class=\"textBoldBlue\">Disconnecting From SQL Server<\/span><\/p>\n<p>It is very important that a connection be released when it is no longer needed by the application because connections consume valuable resources on the server, and the number of connections may be limited by licensing constraints.<\/p>\n<p>You break the connection to the remote data source using the <span style=\"color: #0000ff;\">SQLDisconnect()<\/span> function. <span style=\"color: #0000ff;\">SQLDisconnect()<\/span> takes one parameter, the connection handle created by a call to either <span style=\"color: #0000ff;\">SQLConnect() <\/span>or <span style=\"color: #0000ff;\">SQLStringConnect()<\/span>. <span style=\"color: #0000ff;\">SQLDisconnect()<\/span> returns a 1 if the connection was correctly terminated and a negative value if an error occurred.<\/p>\n<p>The following example establishes a connection to SQL Server, and then drops the connection:<\/p>\n<p><span class=\"textCode\"><span style=\"color: #0000ff;\">LOCAL hConn,lnResult<\/span><\/p>\n<p><span style=\"color: #0000ff;\">*hConn = SQLStringConnect(&#8220;Driver=SQL Server;Server=&lt;SQL2000&gt;;&#8221;+ ;<\/p>\n<p>UID=sa;PWD=;Database=Northwind&#8221;)<\/p>\n<p>hConn = SQLConnect(&#8220;ODBCNorthwind&#8221;, &#8220;sa&#8221;, &#8220;&#8221;)<\/p>\n<p>IF (hConn &gt; 0)<\/span><\/p>\n<p><span style=\"color: #0000ff;\">MESSAGEBOX(&#8220;Connection has done&#8221;)<\/span><\/p>\n<p><span style=\"color: #0000ff;\">lnResult = SQLDisconnect(hConn)<\/p>\n<p>IF lnResult &lt; 0<\/p>\n<p>MESSAGEBOX(&#8220;Disconnect failed&#8221;)<\/span><\/p>\n<p><span style=\"color: #0000ff;\">ENDIF &amp;&amp; lnResult &lt; 0<\/span><\/p>\n<p><span style=\"color: #0000ff;\">ENDIF &amp;&amp; hConn &gt; 0<\/span><\/span><\/p>\n<p>If the parameter supplied to <span style=\"color: #0000ff;\">SQLDisconnect()<\/span> is not a valid connection\u00a0handle, Visual FoxPro will return a run-time error (#1466). Currently there is no way to determine whether a connection handle is valid without\u00a0attempting to use it.<\/p>\n<p>To disconnect all SQL pass through connections, you can pass a value of zero\u00a0to <span style=\"color: #0000ff;\">SQLDisconnect()<\/span>.<\/p>\n<address>source:  <a id=\"ctl00_ctl00_ContentPlaceHolder1_articleContent_authDateCntrl_authLbl\">Sayed Geneidy<\/a><\/address>\n","protected":false},"excerpt":{"rendered":"<p>In Microsoft public newsgroups, I&#8217;ve noticed a recent increase in the number of questions that deal with how to connect from Visual Foxpro to SQL Server, and the problems related to making this connection. So I&#8217;ve decided to write this article\u00a0 to cover such an important topic. There are two functions that can be used [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[112],"tags":[30,78,90],"_links":{"self":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/posts\/79"}],"collection":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=79"}],"version-history":[{"count":1,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":176,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/posts\/79\/revisions\/176"}],"wp:attachment":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}