12.07.2015 Views

Solution Guide for Migrating Oracle on UNIX to SQL Server - Willy .Net

Solution Guide for Migrating Oracle on UNIX to SQL Server - Willy .Net

Solution Guide for Migrating Oracle on UNIX to SQL Server - Willy .Net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

274 Developing: Applicati<strong>on</strong>s — <str<strong>on</strong>g>Migrating</str<strong>on</strong>g> <str<strong>on</strong>g>Oracle</str<strong>on</strong>g> Pro*Ccn = SqlC<strong>on</strong>necti<strong>on</strong>(strC<strong>on</strong>n)' open c<strong>on</strong>necti<strong>on</strong>scn.Open()' close c<strong>on</strong>necti<strong>on</strong>scn.Close()For more details <strong>on</strong> SqlC<strong>on</strong>necti<strong>on</strong>, refer <strong>to</strong>http://msdn.microsoft.com/library/default.asp?url=/library/enus/cpref/html/frlrfsystemdatasqlclientsqlc<strong>on</strong>necti<strong>on</strong>class<strong>to</strong>pic.asp3. Rewrite the queries and cursor functi<strong>on</strong>s.Pro*C uses embedded <strong>SQL</strong> <strong>to</strong> s<strong>to</strong>re the output of a query statement in a cursorfrom which rows can be fetched. Visual Basic.NET does not support cursors;instead, it uses several other methods <str<strong>on</strong>g>for</str<strong>on</strong>g> retrieving data sets from <strong>SQL</strong> queries.One of these data retrieval methods is the DataReader object, which is aligh tweight object that is read-<strong>on</strong>ly and, hence, ideal <str<strong>on</strong>g>for</str<strong>on</strong>g> queries. The following linkprovides access <strong>to</strong> more in<str<strong>on</strong>g>for</str<strong>on</strong>g>mati<strong>on</strong> <strong>on</strong> retrieving data using DataReader object:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpc<strong>on</strong>thead<strong>on</strong>etdatareader.asp.● Here are some comm<strong>on</strong> cursor operati<strong>on</strong>s encountered in Pro*C applicati<strong>on</strong>s:Defining a cursor:●EXEC <strong>SQL</strong> DECLARE cust_cursor FORSELECT Cus<strong>to</strong>merID, CompanyName FROM Cus<strong>to</strong>mers;Opening the cursor:EXEC <strong>SQL</strong> OPEN cust_cursor;Fetching rows using the cursor:<str<strong>on</strong>g>for</str<strong>on</strong>g> (;;){EXEC <strong>SQL</strong> WHENEVER NOT FOUND DO break;EXEC <strong>SQL</strong> FETCH cust_cursor INTO :cus<strong>to</strong>merid, :companyname;printf("Cus<strong>to</strong>merID: %s – CompanyName: %s\n", cus<strong>to</strong>merid,companyname);}Closing the cursor:EXEC <strong>SQL</strong> CLOSE cust_cursor;Although <strong>SQL</strong> <strong>Server</strong> does not support cursors, equivalent functi<strong>on</strong>s can becreated using ADO.NET:Defining the query:str<strong>SQL</strong> = "SELECT Cus<strong>to</strong>merID, CompanyName FROM Cus<strong>to</strong>mers"Creating a command object:Dim cmd As New SqlCommand(str<strong>SQL</strong>, cn)Creating a data reader object:Dim rdr As SqlDataReader = cmd.ExecuteReader()Displaying the data reader result set:While rdr.Read()C<strong>on</strong>sole.WriteLine(rdr("Cus<strong>to</strong>merID") & " – " & rdr("CompanyName"))End WhileDo While rdr.Read()C<strong>on</strong>sole.WriteLine(rdr.GetInt32(0) & " " & _“ rdr.GetString(1) & " " & rdr.GetFloat(2))

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!