IntelliSense Code Snippets provide a way for you to insert ready-made snippets of code into your projects. Microsoft is positioning snippets as a way to speed development by enabling repetitive code to be consolidated into a minimal number of keystrokes or mouse clicks. Its also a way of distributing code that demonstrates best practices, design patterns, and solutions to common tasks that can be easily added to Microsoft Visual Studio 2005.
SAS is providing these code snippets for the SAS® Integration Technologies Windows Client to help developers get started using the client side API's included with the IT Client.
This sample will be periodically updated with new snippets. If you have ideas for snippets, feel free to leave feedback and we'll look into adding them.
Title: | Get Data Using ADO.NET |
Author: | SAS Institute Inc. |
Description: | Uses an OleDbDataAdapter to retrieve data from SAS using the IOM Data Provider. |
Code Snippet: | |
//Create a SAS workspace and initialize it. //Insert a snippet to create a workspace. // Add the SAS object to the ObjectKeeper. If the object is not // in the ObjectKeeper, it cannot be used by the IOM Data // Provider. SASObjectManager.ObjectKeeperClass objKeeper = new SASObjectManager.ObjectKeeperClass(); objKeeper.AddObject(1, "BridgeConnection", sasWorkspace); string selectCommandText = "$QueryString$"; //Get the requested data using ADO.NET. System.Data.OleDb.OleDbDataAdapter sasDataAdapter = new System.Data.OleDb.OleDbDataAdapter( selectCommandText, "provider=sas.iomprovider.1; SAS Workspace ID=" + sasWorkspace.UniqueIdentifier); System.Data.DataSet dataSetResult = new System.Data.DataSet(); sasDataAdapter.Fill(dataSetResult, "sasdata"); objKeeper.RemoveObject(sasWorkspace); |
Title: | Get Data Using ADODB |
Author: | SAS Institute Inc. |
Description: | Gets data using the IOM Data Provider and ADODB through the COM Interop. |
Code Snippet: | |
//Create a SAS workspace and initialize it. //Insert a snippet to create a workspace. // Add the SAS object to the ObjectKeeper. If the object is not // in the ObjectKeeper, it cannot be used by the IOM Data // Provider. SASObjectManager.ObjectKeeperClass objKeeper = new SASObjectManager.ObjectKeeperClass(); objKeeper.AddObject(1, "BridgeConnection", sasWorkspace); //Get the formatted data using ADODB. ADODB.Connection adoConnection = new ADODB.ConnectionClass(); ADODB.Recordset adoRecordset = new ADODB.Recordset(); ADODB.Command adoCommand = new ADODB.CommandClass(); adoConnection.Open("provider=SAS.IOMProvider.1; SAS Workspace ID=" + sasWorkspace.UniqueIdentifier, "", "", 0); adoCommand.ActiveConnection = adoConnection; adoCommand.CommandText = "$QueryString$"; adoCommand.Properties["SAS Formats"].Value = "_ALL_"; adoCommand.CommandType = ADODB.CommandTypeEnum.adCmdText; adoRecordset.Open( adoCommand, System.Reflection.Missing.Value, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockReadOnly, 1); //Fill an ADO.NET DataSet using the ADODB Recordset. System.Data.OleDb.OleDbDataAdapter sasDataAdapter = new System.Data.OleDb.OleDbDataAdapter(); System.Data.DataSet sasData = new System.Data.DataSet(); sasDataAdapter.Fill(sasData, adoRecordset, "sasdata"); objKeeper.RemoveObject(sasWorkspace); |
Title: | Create a SAS Metadata Configuration File |
Author: | SAS Institute Inc. |
Description: | Inserts code to call WriteConfigFile, which creates a metadata configuration file. |
Code Snippet: | |
SASObjectManager.ObjectFactoryClass objFactory = new SASObjectManager.ObjectFactoryClass(); SASObjectManager.ServerDefClass serverDef = new SASObjectManager.ServerDefClass(); SASObjectManager.LoginDefClass loginDef = new SASObjectManager.LoginDefClass(); // ************************************************ // TODO: Configure where files will be saved by setting the // following boolean statements. // ************************************************ bool bSaveLoginForAllUsers = true; bool bSaveServerForAllUsers = true; String strAllUsersFilePath = Environment.GetFolderPath( Environment.SpecialFolder.CommonApplicationData) + @"\SAS\MetadataServer\oms_serverinfo.xml"; String strCurrentUserFilePath = Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData) + @"\SAS\MetadataServer\oms_serverinfo.xml"; String strCurrentUserLoginFilePath = Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData) + @"\SAS\MetadataServer\oms_userinfo.xml"; // ************************************************ // Configure the SAS® Metadata Server information. // TODO: Modify these values for your SAS Metadata Server. // ************************************************ serverDef.BridgeEncryptionAlgorithm = "SASProprietary"; serverDef.BridgeEncryptionLevel = SASObjectManager.EncryptionLevels.EncryptUserAndPassword; serverDef.ClassIdentifier = "2887E7D7-4780-11D4-879F-00C04F38F0DB"; serverDef.Name = "SAS Metadata Server"; serverDef.Port = $ServerDef.Port$; serverDef.MachineDNSName = $ServerDef.MachineDNSName$; serverDef.Protocol = SASObjectManager.Protocols.ProtocolBridge; serverDef.DomainName = $ServerDef.Domain$; String strRepository = $Repository$; // ************************************************ // Configure the SAS Metadata Server login information. // TODO: Modify these values for your SAS Metadata Server. // ************************************************ loginDef.DomainName = serverDef.DomainName; loginDef.LoginName = $LoginDef.LoginName$; loginDef.Name = loginDef.LoginName; loginDef.Password = $LoginDef.Password$; if (bSaveServerForAllUsers && bSaveLoginForAllUsers) { // ************************************************ // Save server and login information for all users. // ************************************************ objFactory.WriteConfigFile( strAllUsersFilePath, serverDef, loginDef, SASObjectManager.RepositoryTypes.RepositoryOMR, strRepository); } else if (bSaveServerForAllUsers) { // ************************************************ // Save server information for all users and login // information for the current user. // ************************************************ objFactory.WriteConfigFile( strAllUsersFilePath, serverDef, null, SASObjectManager.RepositoryTypes.RepositoryOMR, strRepository); objFactory.WriteConfigFile( strCurrentUserLoginFilePath, null, loginDef, SASObjectManager.RepositoryTypes.RepositoryNone, null); } else { // ************************************************ // Save server and login information for current users. // ************************************************ objFactory.WriteConfigFile( strCurrentUserFilePath, serverDef, loginDef, SASObjectManager.RepositoryTypes.RepositoryOMR, strRepository); // It is a good idea to clean up conflicting login // information to ensure that your configuration is using // the correct user name and password. System.IO.FileInfo fiCurrentUserLoginFile = new System.IO.FileInfo(strCurrentUserLoginFilePath); if (fiCurrentUserLoginFile.Exists) fiCurrentUserLoginFile.Delete(); } |
Title: | Create a COM or DCOM Connection |
Author: | SAS Institute Inc. |
Description: | Creates a COM or DCOM connection to a SAS Workspace Server. |
Code Snippet: | |
SASObjectManager.ObjectFactoryClass objFactory = new SASObjectManager.ObjectFactoryClass(); SASObjectManager.ServerDefClass serverDef = new SASObjectManager.ServerDefClass(); // ************************************************ // Configure the SAS server information. // TODO: Modify these options as appropriate. // ************************************************ serverDef.Protocol = SASObjectManager.Protocols.ProtocolCom; serverDef.MachineDNSName = $MachineDNSName$; // ************************************************ // Create the connection. // ************************************************ SAS.Workspace sasWorkspace = null; sasWorkspace = (SAS.Workspace) objFactory.CreateObjectByServer( "COMConnection", true, serverDef, null, null); |
Title: | Create an IOM Bridge Connection |
Author: | SAS Institute Inc. |
Description: | Creates an IOM Bridge connection to a SAS Workspace Server. |
Code Snippet: | |
SASObjectManager.ObjectFactoryClass objFactory = new SASObjectManager.ObjectFactoryClass(); SASObjectManager.ServerDefClass serverDef = new SASObjectManager.ServerDefClass(); // ************************************************ // Configure the SAS server information. // TODO: Modify these options as appropriate. // ************************************************ serverDef.BridgeEncryptionAlgorithm = "SASProprietary"; serverDef.BridgeEncryptionLevel = SASObjectManager.EncryptionLevels.EncryptUserAndPassword; serverDef.MachineDNSName = $MachineDNSName$; serverDef.Port = $Port$; serverDef.Protocol = SASObjectManager.Protocols.ProtocolBridge; // ************************************************ // Create the connection. // ************************************************ SAS.Workspace sasWorkspace = null; sasWorkspace = (SAS.Workspace)objFactory.CreateObjectByServer( "BridgeConnection", true, serverDef, $UserName$, $Password$); |
Title: | Create a Connection by Logical Name |
Author: | SAS Institute Inc. |
Description: | Creates a connection to a SAS IOM Server that is defined on a metadata server. |
Code Snippet: | |
SASObjectManager.ObjectFactoryClass objFactory = new SASObjectManager.ObjectFactoryClass(); // ************************************************ // Create the connection. // ************************************************ SAS.Workspace sasWorkspace = null; sasWorkspace = (SAS.Workspace)objFactory.CreateObjectByLogicalName( "SASMain", true, $LogicalName$, null); |
Title: | Create a Connection Pool |
Author: | SAS Institute Inc. |
Description: | Creates a connection pool of workspaces for use in an application that needs to make frequent use of workspaces with minimal wait time. |
Code Snippet: | |
SASObjectManager.ObjectFactoryClass objFactory = new SASObjectManager.ObjectFactoryClass(); SASObjectManager.ServerDefClass serverDef = new SASObjectManager.ServerDefClass(); SASObjectManager.LoginDefClass loginDef = new SASObjectManager.LoginDefClass(); SASObjectManager.ObjectPool objectPool = null; string strPoolName = $PoolName$; try { //Check to be sure that the pool is not already running. objectPool = objFactory.ObjectPools.Item(strPoolName); if (objectPool != null) return; } catch (Exception ex) { Trace.Write( "Pool not created yet, creating pool." + "Exception message: " + ex.Message); } try { // ************************************************ // Configure the SAS server information. // TODO: Modify these options as appropriate. // ************************************************ serverDef.MachineDNSName = $MachineDNSName$; serverDef.Protocol = SASObjectManager.Protocols.ProtocolBridge; serverDef.Port = $Port$; serverDef.BridgeEncryptionAlgorithm = "SASProprietary"; serverDef.BridgeEncryptionLevel = SASObjectManager.EncryptionLevels.EncryptUserAndPassword; serverDef.MaxPerObjectPool = 5; serverDef.RunForever = false; serverDef.ShutdownAfter = 30; serverDef.RecycleActivationLimit = 30; // ************************************************ // Configure the login information and pooling parameters. // TODO: Modify these options as appropriate. // ************************************************ loginDef.LoginName = $UserName$; loginDef.Password = $Password$; loginDef.MinSize = 2; loginDef.MinAvail = 0; // ************************************************ // Create the ObjectPool. // ************************************************ objectPool = objFactory.ObjectPools.CreatePoolByServer( strPoolName, serverDef, loginDef); Trace.Write("ObjectPool created."); } catch (Exception ex) { Trace.Write("Exception while creating object pool: " + ex.Message); throw; } |
Title: | Create a DataSet from an XmlElement |
Author: | SAS Institute Inc. |
Description: | Create an ADO.NET DataSet from an XmlElement. This XmlElement is typically be retrieved from SAS BI Web Services (an interface in SAS® Integration Technologies) using code that created by the SAS BI Web Services Wizard. |
Code Snippet: | |
System.Data.DataSet ds; System.Xml.XmlElement result; // ************************************************ // Add code to call SAS BI Web Services Wizard code. // // System.Data.DataSet dsIn = new System.Data.Dataset(); // ds.ReadXml("c:\\mydoc.xml"); // System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); // doc.LoadXml(ds.GetXml()); // // string tablename = "mydata"; // result = // XMLA.SamplesStoredProcessesSampleMEANSProcedureWebService( // tablename, doc.DocumentElement); // ************************************************ try { System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(ms, System.Text.Encoding.UTF8); //Write the results to the xmlWriter. result.WriteTo(xmlWriter); xmlWriter.Flush(); //Seek back to the beginning of MemoryStream. ms.Seek(0, System.IO.SeekOrigin.Begin); //Let ADO.NET read in the data from the stream, automatically //reading the in-line schema. ds = new System.Data.DataSet(); ds.ReadXml(ms); } catch(Exception ex) { throw ex; } |
Title: | Add CILanguageEvents Listener Methods |
Author: | SAS Institute Inc. |
Description: | Adds a method to initialize CILanguageEvents and all of the required method signatures for the event delegates. |
Code Snippet: | |
#region ConfigureSASLanguageEvents public void ConfigureSASLanguageEvents(SAS.Workspace sasWorkspace) { SAS.LanguageService sasLS = sasWorkspace.LanguageService; sasLS.DatastepStart += new SAS.CILanguageEvents_DatastepStartEventHandler( sasLS_DatastepStart); sasLS.DatastepComplete += new SAS.CILanguageEvents_DatastepCompleteEventHandler( sasLS_DatastepComplete); sasLS.ProcStart += new SAS.CILanguageEvents_ProcStartEventHandler( sasLS_ProcStart); sasLS.ProcComplete += new SAS.CILanguageEvents_ProcCompleteEventHandler( sasLS_ProcComplete); sasLS.StepError += new SAS.CILanguageEvents_StepErrorEventHandler( sasLS_StepError); sasLS.SubmitComplete += new SAS.CILanguageEvents_SubmitCompleteEventHandler( sasLS_SubmitComplete); } void sasLS_DatastepStart() { throw new Exception("The method or operation is not implemented."); } void sasLS_DatastepComplete() { throw new Exception("The method or operation is not implemented."); } void sasLS_ProcStart(string Procname) { throw new Exception("The method or operation is not implemented."); } void sasLS_ProcComplete(string Procname) { throw new Exception("The method or operation is not implemented."); } void sasLS_StepError() { throw new Exception("The method or operation is not implemented."); } void sasLS_SubmitComplete(int Sasrc) { throw new Exception("The method or operation is not implemented."); } #endregion |
Title: | Download a Temporary File Using FileService |
Author: | SAS Institute Inc. |
Description: | Creates a temporary fileref and uses it to download output that is generated by SAS back to the client as a byte array. This action requires a connection to be established already. |
Code Snippet: | |
// ************************************************ // Define instances of the interfaces that are used. // ************************************************ SAS.LanguageService sasLS = $sasWorkspace$.LanguageService; SAS.FileService sasFileService = $sasWorkspace$.FileService; SAS.Fileref sasFileref; string outstring; // ************************************************ // Create a temporary fileref to store dynamically // generated output. Pass this fileref into your // SAS code so that the temporary fileref is used // to write your output. // ************************************************ sasFileref = sasFileService.AssignFileref( "", "TEMP", "", "", out outstring); // ************************************************************ // TODO: Insert your own logic in the following REGION section // to generate output in SAS that needs to be downloaded. // ************************************************************ string sasCode; #region SAS Code to Create HTML File // Create a HTML Chart // Add code here to create some ODS output. #endregion // ************************************************ // Submit the sample code to generate output. // ************************************************ sasLS.Submit(sasCode); // ************************************************ // Stream back the output file from SAS. // ************************************************ SCRIPTOLib.StreamHelper sasStreamHelper = new SCRIPTOLib.StreamHelper(); SAS.BinaryStream sasBinaryStream; sasBinaryStream = sasFileref.OpenBinaryStream( SAS.StreamOpenMode.StreamOpenModeForReading); byte[] pieChart = (byte[])sasStreamHelper.ReadBinaryArray( sasBinaryStream, 0); sasBinaryStream.Close(); // ************************************************ // The encoding of the HTML is specified as UTF-8 in // the ODS code. Here are some example uses of the // the byte array that you can add to your code. // ************************************************ //string strPieChart = System.Text.Encoding.UTF8.GetString(pieChart); //System.IO.FileStream fs = new FileStream( // "c:\\pieChart.html", FileMode.Create, FileAccess.Write); //fs.Write(pieChart, 0, pieChart.Length); //fs.Close(); |
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
These code snippets are being distributed as a compressed ZIP file using the Microsoft Visual Studio Content Installer format. or this installer to work, you must have Microsoft Visual Studio 2005 or one of the Express editions installed on your machine.
If you don not have Microsoft Visual Studio 2005, you should be able to open the ZIP file with any tool that can open zipped files and then extract the snippet files.
For more information on using the Microsoft Visual Studio Component Installer, see How To: Install Community Components on MSDN.
For more information on IntelliSense Code Snippets , see Creating and Using IntelliSense Code Snippets on MSDN.
Type: | Sample |
Topic: | Third Party ==> Programming ==> .NET |
Date Modified: | 2008-02-10 12:12:25 |
Date Created: | 2006-05-31 10:59:01 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | SAS Integration Technologies | Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M0 | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.1 TS1M0 | |||
Microsoft Windows 2000 Advanced Server | 9.1 TS1M0 | |||
Microsoft Windows 2000 Datacenter Server | 9.1 TS1M0 | |||
Microsoft Windows 2000 Server | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.1 TS1M0 | |||
Microsoft Windows NT Workstation | 9.1 TS1M0 | |||
Microsoft Windows 2000 Professional | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9.1 TS1M0 | |||
Microsoft Windows XP Professional | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.1 TS1M0 |