{"id":71033,"date":"2004-11-10T17:08:00","date_gmt":"2004-11-10T17:08:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/10\/can-i-create-and-delete-a-dsn-using-a-script\/"},"modified":"2004-11-10T17:08:00","modified_gmt":"2004-11-10T17:08:00","slug":"can-i-create-and-delete-a-dsn-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/can-i-create-and-delete-a-dsn-using-a-script\/","title":{"rendered":"Can I Create and Delete a DSN Using a Script?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! Is it possible to create an ODBC Data Sources Name (DSN) on the fly and then remove it when done?<BR><BR>&#8212; KC<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, KC. Is it possible to create and then delete an ODBC DSN? To tell you the truth, we weren\u2019t entirely sure. As it turns out, however the answer is this: as long as you\u2019re willing to write a script that modifies the registry, then yes, you can programmatically create and delete DSNs.<\/P>\n<P><B>Note<\/B>. If you aren\u2019t sure what a DSN is, it\u2019s a way for you to connect to a database merely by referencing the DSN name, and without having to specify the entire path to the database. That\u2019s particularly handy if you have a bunch of scripts that access this database. If you hardcode the path into the scripts and then move the database from one server to another, you\u2019ll have to edit the path on each and every script. If you use a DSN, however, you won\u2019t have to edit the scripts at all; instead, you just need to modify the DSN. That\u2019s one change versus hundreds of changes (depending on how many scripts you have).<\/P>\n<P>What we\u2019re about to show you is a script that creates a DSN that uses SQL Server. Is that important? Yes. You can create DSNs that use other ODBC databases (Access, Oracle, FoxPro, whatever). <I>However<\/I>, the registry keys and values used when creating a DSN to access an Oracle database aren\u2019t necessarily the same as those used to access a SQL Server database. If you\u2019re using SQL Server, you can just copy our script and change the names of the database, the server, etc. If you\u2019re using a different database, we\u2019d recommend that you manually create a DSN. After you do that, fire up your favorite registry editor and look for the DSN here:<\/P><PRE class=\"codeSample\">HKLM\\SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources\n<\/PRE>\n<P>And here:<\/P><PRE class=\"codeSample\">HKLM\\SOFTWARE\\ODBC\\ODBC.INI\\Name You Gave the DSN\n<\/PRE>\n<P>Take a look at the registry keys and values required for that type of database, and then plug that information into our script. If you aren\u2019t sure how to do that, give us a week or two, and we\u2019ll see what we can do about getting some sample scripts for different ODBC databases posted in the Script Center.<\/P>\n<P>At any rate, here\u2019s a script that creates a DSN that uses SQL Server:<\/P><PRE class=\"codeSample\">Const HKEY_LOCAL_MACHINE = &amp;H80000002<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objReg=GetObject(&#8220;winmgmts:{impersonationLevel=impersonate}!\\\\&#8221; &amp; _ \n    strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources&#8221;\nstrValueName = &#8220;Script Repository&#8221;\nstrValue = &#8220;SQL Server&#8221;\nobjReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<\/p>\n<p>strKeyPath = &#8220;SOFTWARE\\ODBC\\ODBC.INI\\Script Repository&#8221;<\/p>\n<p>objReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath<\/p>\n<p>strKeyPath = &#8220;SOFTWARE\\ODBC\\ODBC.INI\\Script Repository&#8221;<\/p>\n<p>strValueName = &#8220;Database&#8221;\nstrValue = &#8220;Script Center&#8221;\nobjReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<\/p>\n<p>strValueName = &#8220;Driver&#8221;\nstrValue = &#8220;C:\\WINDOWS\\System32\\SQLSRV32.dll&#8221;\nobjReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<\/p>\n<p>strValueName = &#8220;Server&#8221;\nstrValue = &#8220;atl-sql-01&#8221;\nobjReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<\/p>\n<p>strValueName = &#8220;Trusted_Connection&#8221;\nstrValue = &#8220;Yes&#8221;\nobjReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue\n<\/PRE>\n<P>Yes, we know, it\u2019s a bit long, but that\u2019s because there are a number of registry keys and values that have to be created. For the most part, though, the script is pretty straightforward. We start by setting the constant HKEY_LOCAL_MACHINE to &amp;H80000002, the value required to connect to the HKLM portion of the registry. We then bind to the WMI service, and to the StdRegProv class (which &#8211; as we always remind you &#8211; happens to be in the root\\default namespace).<\/P>\n<P>From there we create a new registry value (HKLM\\SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources\\Script Repository), with <B>Script Repository<\/B> the name we\u2019re giving to our new DSN.We set the value of this, well, value to <B>SQL Server<\/B>, because that\u2019s the type of database we\u2019re connecting to.<\/P>\n<P>Got that? After that we create a new registry <I>key<\/I>: HKLM\\SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources\\Script Repository (note that the key name is the same as our DSN Name). We then need to configure the following registry values:<\/P>\n<P><B>Database &#8212; <\/B>Name of the actual database we want to connect to. In this example, the database is named <B>ScriptCenter<\/B>.<\/P>\n<P><B>Driver &#8212; <\/B>Path to SQL Server ODBC drive. In this example, that path is C:\\WINDOWS\\System32\\SQLSRV32.dll.<\/P>\n<P><B>Server &#8212; <\/B>Name of the server where the database is found. In this case, the server name is <B>atl-sql-01<\/B>.<\/P>\n<P><B>Trusted_Connection &#8212; <\/B>Tells SQL Server to use our logon credentials when we access the database. This enables us to connect to the database without having to supply a user name and password.<\/P>\n<P>That\u2019s it; run the script, and you\u2019ll have yourself a fully-functioning DSN.<\/P>\n<P>And what about getting rid of that DSN once you\u2019re finished with it? Well, that\u2019s equally easy: after all, we just need to delete the registry keys and values our first script created. Here\u2019s a script that will delete the DSN <B>Script Repository<\/B> : <\/P><PRE class=\"codeSample\">Const HKEY_LOCAL_MACHINE = &amp;H80000002<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objReg=GetObject(&#8220;winmgmts:{impersonationLevel=impersonate}!\\\\&#8221; &amp; _ \n    strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;SOFTWARE\\ODBC\\ODBC.INI\\Script Repository&#8221;\nobjReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath<\/p>\n<p>strKeyPath = &#8220;SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources&#8221;\nstrValueName = &#8220;Script Repository&#8221;\nobjReg.DeleteValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName\n<\/PRE>\n<P>Note that what this script does is delete the first registry value we created (Script Repository), and then deletes the registry key we created. We don\u2019t have to delete the individual registry values within that key, because deleting HKLM\\SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources\\Script Repository will delete all the values as well.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is it possible to create an ODBC Data Sources Name (DSN) on the fly and then remove it when done?&#8212; KC Hey, KC. Is it possible to create and then delete an ODBC DSN? To tell you the truth, we weren\u2019t entirely sure. As it turns out, however the answer is this: [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,26,3,5],"class_list":["post-71033","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-registry","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is it possible to create an ODBC Data Sources Name (DSN) on the fly and then remove it when done?&#8212; KC Hey, KC. Is it possible to create and then delete an ODBC DSN? To tell you the truth, we weren\u2019t entirely sure. As it turns out, however the answer is this: [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=71033"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71033\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=71033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}