Domanda

Ho bisogno di un modo programmatico per creare un'origine dati ODBC di SQL Server. Posso farlo accedendo direttamente al registro. Sarebbe meglio se ciò potesse essere fatto tramite un'API (SQL Server / Windows) disponibile per proteggere dalle modifiche nelle chiavi o nei valori del registro con driver SQL Server aggiornati.

Risposta accettata Nota: l'utilizzo di SQLConfigDataSource consente di estrarre il codice dai dettagli delle chiavi del Registro di sistema, ecc., quindi è più efficace. Speravo, tuttavia, che SQL Server lo avrebbe avvolto con una funzione di livello superiore che assumeva attributi fortemente tipizzati (anziché una stringa delimitata) e lo esponeva attraverso il driver.

È stato utile?

Soluzione

SQLConfigDataSource () fa il lavoro.

Articolo MSDN

Nel caso in cui qui sia un esempio VB6:

Const ODBC_ADD_DSN = 1 'user data source
Const ODBC_ADD_SYS_DSN = 4 'system data source

Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal
hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal
lpszAttributes As String) As Long

strDriver = "SQL Server"
strAttributes = "DSN=Sample" & Chr$(0) _
& "Database=Northwind" & Chr$(0) _
& "Description= Sample Data Source" & Chr$(0) _
& "Server=(local)" & Chr$(0) _
& "Trusted_Connection=No" & Chr$(0)

SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, strDriver, strAttributes)

Altri suggerimenti

Per VB.NET può essere fatto in questo modo:

Importa per 'DllImport':

Imports System.Runtime.InteropServices

Dichiarazione di SQLConfigDataSource:

<DllImport("ODBCCP32.DLL")> Shared Function SQLConfigDataSource _
(ByVal hwndParent As Integer, ByVal fRequest As Integer, _
    ByVal lpszDriver As String, _
    ByVal lpszAttributes As String) As Boolean
End Function

Esempio di utilizzo:

Const ODBC_ADD_DSN = 1 'User data source
Const ODBC_ADD_SYS_DSN = 4 'System data source

Public Function CreateSqlServerDataSource
    Dim strDriver As String : strDriver = "SQL Server"
    Dim strAttributes As String : strAttributes = _
        "DSN=Sample" & Chr(0) & _
        "Database=Northwind" & Chr(0) & _
        "Description= Sample Data Source" & Chr(0) & _
        "Server=(local)" & Chr(0) & _
        "Trusted_Connection=No" & Chr(0)

    SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, strDriver, strAttributes)
End Function

Vorrei usare odbcad32.exe che si trova nella cartella system32.

Questo aggiungerà le tue fonti di dati odbc alla posizione corretta, che non verrà effettuata da nessuna patch.

Per farlo direttamente nel registro è possibile aggiungere un valore stringa a:

HKLM\SOFTWARE\Microsoft\ODBC\ODBC.INI\ODBC Data Sources

per aggiungere un DSN di sistema o:

HKCU\Software\ODBC\ODBC.INI\ODBC Data Sources

per aggiungere un DSN utente.

Il nome del valore è il nome dell'origine dati che si desidera creare e i dati devono essere "SQL Server".

Allo stesso livello di "Origini dati ODBC" nel Registro di sistema creare una chiave con il nome dell'origine dati che si desidera creare.

Questa chiave richiede i seguenti valori stringa:

Database     - Name of default database to which to connect
Description  - A description of the Data Source
Driver       - C:\WINDOWS\system32\SQLSRV32.dll
LastUser     - Name of a database user (e.g. sa)
Server       - Hostname of machine on which database resides

Ad esempio, utilizzando l'applicazione reg.exe dalla riga di comando per aggiungere un'origine dati utente denominata "ExampleDSN":

reg add "HKCU\Software\ODBC\ODBC.INI\ODBC Data Sources" 
    /v ExampleDSN /t REG_SZ /d "SQL Server"
reg add HKCU\Software\ODBC\ExampleDSN 
    /v Database /t REG_SZ /d ExampleDSN
reg add HKCU\Software\ODBC\ExampleDSN 
    /v Description /t REG_SZ /d "An Example Data Source"
reg add HKCU\Software\ODBC\ExampleDSN
    /v Driver /t REG_SZ /d "C:\WINDOWS\system32\SQLSRV32.DLL"
reg add HKCU\Software\ODBC\ExampleDSN
    /v LastUser /t REG_SZ /d sa
reg add HKCU\Software\ODBC\ExampleDSN
    /v Server /t REG_SZ /d localhost

Esempio usando C #:

(riferimento ai parametri dettagliati di SQL Server all'indirizzo http://msdn.microsoft. com / en-us / library / aa177860.aspx )

using System.Runtime.InteropServices; 

        private enum RequestFlags : int
        {

            ODBC_ADD_DSN = 1,
            ODBC_CONFIG_DSN = 2,
            ODBC_REMOVE_DSN = 3,
            ODBC_ADD_SYS_DSN = 4,
            ODBC_CONFIG_SYS_DSN = 5,
            ODBC_REMOVE_SYS_DSN = 6,
            ODBC_REMOVE_DEFAULT_DSN = 7

        }

        [DllImport("ODBCCP32.DLL", CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern bool SQLConfigDataSource(UInt32 hwndParent, RequestFlags  fRequest, 
                                 string lpszDriver, string lpszAttributes);

        public static void CreateDSN()
        {

            string strDrivername = "SQL Server";
            string strConfig =  "DSN=StackOverflow\0" +
                                   "Database=Northwind\0" +
                                   "Description=StackOverflow Sample\0" +
                                   "Server=(local)\0" +
                                   "Trusted_Connection=No\0";

            bool success = SQLConfigDataSource(0, RequestFlags.ODBC_ADD_SYS_DSN, strDrivername, strConfig);

        }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top