Sunday 15 September 2013

sql server - How to Use SQLConfigDataSource in a Visual Studio 2012 C# Project -



sql server - How to Use SQLConfigDataSource in a Visual Studio 2012 C# Project -

i want create user dsn programmatically. have read this , similar questions; trolled msdn forums; , looked in several other places, find out how create sqlconfigdatasource work. in post i've linked above, see sqlconfigdatasourcew referenced, cannot find reference that.

i want utilize sqlconfigdatasource or similar function in visual studio 2012 c# project, , not know should included in .cs file.

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;

i've looked @ system.data.odbc , sqlconfigdatasource appears not part of that. believe odbc drivers installed on workstation. other using .reg file or manually creating dsn, other steps can try?

other using .reg file or manually creating dsn, other steps can seek create user dsn programmatically?

you utilize odbcconf.exe, command-line tool allows configure odbc drivers , info source names. uses sqlconfigdatasource function. however, such old technology , microsoft warns:

odbcconf.exe removed in future version of windows info access components. avoid using feature, , plan modify applications utilize feature.

i not sure impact decommissioning odbcconf.exe have on sqlconfigdatasource doesn't good. hence 1 of best way forwards utilize recommended powershell commands manage drivers , info sources, here list of windows info access components powershell commands.

you run cmdlets add-odbcdsn:

add-odbcdsn [-name] <string> -drivername <string> -dsntype <string> [-asjob] [-cimsession <cimsession> ] [-passthru] [-platform <string> ] [-setpropertyvalue <string> ] [-throttlelimit <int32> ] [ <commonparameters>]

eg:

ps c:> add-odbcdsn -name "mypayroll" -drivername "sql server native client 10.0" -dsntype "system" -setpropertyvalue @("server=myserver", "trusted_connection=yes", "database=payroll")

i'm guessing know or can find out how execute these powershell commands in c#.

unfortunately above powershell commands windows 8 , above.

for windows 7 , below either: a) utilize odbcconf.exe b) write registry straight in c# c) utilize the sqlconfigdatasource function, or d) utilize powershell script works windows 7:

ps c:\> add-odbcconnection2hkcu.ps1

here add-odbcconnection2hkcu.ps1:

## add together "hkey_current_user" - registry odbc update add together odbc connection user dsn tab $hkcuaddpath = "hkcu:\software\odbc\odbc.ini\localdeveloper" cd hkcu:\software\odbc\odbc.ini\localdeveloper md localdeveloper set-itemproperty -path $hkcuaddpath -name database -value "developer" set-itemproperty -path $hkcuaddpath -name description -value "local developer database" set-itemproperty -path $hkcuaddpath -name driver -value "c:\windows\system32\sqlsrv32.dll" set-itemproperty -path $hkcuaddpath -name lastuser -value "" set-itemproperty -path $hkcuaddpath -name server -value "max-pcwin1" set-itemproperty -path $hkcuaddpath -name trusted_connection -value "yes" ## needed odbc connection show in "odbc administator" application cd hkcu:\software\odbc\odbc.ini\'odbc info sources' set-itemproperty -path hkcu:\software\odbc\odbc.ini\'odbc info sources' -name localdeveloper -value "sql server"

ref: http://maxt2posh.wordpress.com/2009/06/07/powershell-working-with-odbc-dsn-connection/

c# sql-server visual-studio-2012 odbc

No comments:

Post a Comment