473,378 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Creating a deleting a DSN in code

Help! I've been tasked to create a DSN in code and then delete it on
close. For security we do not want to leave it in place outside of
the application we are implementing.
Nov 13 '05 #1
6 5422
Rather than going to that bother, why not make DSN-less connections?

Check http://members.rogers.com/douglas.j....LessLinks.html for one
approach.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"mathilda" <sm***********@yahoo.com> wrote in message
news:ff********************************@4ax.com...
Help! I've been tasked to create a DSN in code and then delete it on
close. For security we do not want to leave it in place outside of
the application we are implementing.

Nov 13 '05 #2
mathilda wrote:
Help! I've been tasked to create a DSN in code and then delete it on
close. For security we do not want to leave it in place outside of
the application we are implementing.


What is your platform and back end?

Doug's solution is something I am eager to explore now that I am in
A2003, but it would not work for Oracle in A97 and DAO.

Another possiblity is creating a user in the back end database (Oracle,
and I assume the same can be done for MS SQL, but I don't know for sure)
for which only grant select has been given to specific tables. That's
what I do for my oracle database, giving my users read only (no create
or delete) access to the tables they need.

The latter solution is, of course, no good if you want them to be able
to write to your data.

--
Tim - http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Nov 13 '05 #3
"Tim Marshall" <TI****@antarctic.flowerpots> wrote in message
news:cj**********@coranto.ucs.mun.ca...
mathilda wrote:
Help! I've been tasked to create a DSN in code and then delete it on
close. For security we do not want to leave it in place outside of
the application we are implementing.


What is your platform and back end?

Doug's solution is something I am eager to explore now that I am in
A2003, but it would not work for Oracle in A97 and DAO.

Another possiblity is creating a user in the back end database (Oracle,
and I assume the same can be done for MS SQL, but I don't know for sure)
for which only grant select has been given to specific tables. That's
what I do for my oracle database, giving my users read only (no create
or delete) access to the tables they need.

The latter solution is, of course, no good if you want them to be able
to write to your data.


We've used it successfully against Oracle (don't remember which version)
from Access 97.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

Nov 13 '05 #4
On Tue, 05 Oct 2004 13:23:47 -0230, Tim Marshall
<TI****@antarctic.flowerpots> wrote:
mathilda wrote:
Help! I've been tasked to create a DSN in code and then delete it on
close. For security we do not want to leave it in place outside of
the application we are implementing.


What is your platform and back end?

Doug's solution is something I am eager to explore now that I am in
A2003, but it would not work for Oracle in A97 and DAO.

Another possiblity is creating a user in the back end database (Oracle,
and I assume the same can be done for MS SQL, but I don't know for sure)
for which only grant select has been given to specific tables. That's
what I do for my oracle database, giving my users read only (no create
or delete) access to the tables they need.

The latter solution is, of course, no good if you want them to be able
to write to your data.


I'm ussing an Access 2000 front end with a fourth shift (i.e. sql
server 2000) back end. The boss has already set up DSNs on several
peoples machines before we were made aware of the security faux paux.
Nov 13 '05 #5
Try this :
'Start Code
Option Compare Database
Option Explicit

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
Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv&, ByVal
fDirection%, ByVal szDSN$, ByVal cbDSNMax%, pcbDSN%, ByVal szDescription$,
ByVal cbDescriptionMax%, pcbDescription%) As Integer
Private Declare Function SQLAllocEnv% Lib "ODBC32.DLL" (env&)
Const SQL_SUCCESS As Long = 0
Const SQL_FETCH_NEXT As Long = 1

'Constant Declaration
Private Const ODBC_ADD_DSN = 1 ' Add data source
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const vbAPINull As Long = 0& ' NULL Pointer

Sub MakeODBC(ProgPath As String, _
DataPath As String, _
UserName As String, _
PASSWORD As String, _
ServerODBC As String, _
NameDNS As String)

On Error GoTo errDNS
'Usage : MakeODBC "c:\Acomba","c:\f1000.dta\demo","Supervisor","demo "

Dim strConnect As String
Dim strAttributes As String

strAttributes = "SERVER=" & ServerODBC & Chr$(0)
strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
strAttributes = strAttributes & "DSN=" & NameDNS & Chr$(0)
strAttributes = strAttributes & "AcombaExe=" & ProgPath & Chr$(0)
strAttributes = strAttributes & "DBQ=" & DataPath & Chr$(0)
strAttributes = strAttributes & "UID=" & UserName & Chr$(0)
strAttributes = strAttributes & "PWD=" & PASSWORD & Chr$(0)

DBEngine.RegisterDatabase "Acomba", "Acomba ODBC Driver", True,
strAttributes

MsgBox "Création du DNS avec succès !", vbInformation, "Création de DNS"
Exit Sub
errDNS:
MsgBox "Erreur dans la création du DNS !", vbExclamation, "Création de
DNS"

End Sub

Public Function GetDSNsAndDrivers(ModeRetour As Integer) As String
Dim i As Integer
Dim sDSNItem As String * 1024
Dim sDRVItem As String * 1024
Dim sDSN As String
Dim sDRV As String
Dim iDSNLen As Integer
Dim iDRVLen As Integer
Dim lHenv As Long 'handle to the environment
Dim retvalue As String

On Error Resume Next

If ModeRetour = 1 Then
CurrentDb.Execute "DELETE TMP_ODBC.* FROM TMP_ODBC"
Else
CurrentDb.Execute "DELETE TMP_ODBCServeur.* FROM TMP_ODBCServeur"
End If
'get the DSNs
If SQLAllocEnv(lHenv) <> -1 Then
Do Until i <> SQL_SUCCESS
sDSNItem = Space$(1024)
sDRVItem = Space$(1024)
i = SQLDataSources(lHenv, SQL_FETCH_NEXT, sDSNItem, 1024,
iDSNLen, sDRVItem, 1024, iDRVLen)
sDSN = Left$(sDSNItem, iDSNLen)
sDRV = Left$(sDRVItem, iDRVLen)

If sDSN <> Space(iDSNLen) Then
If ModeRetour = 1 Then
CurrentDb.Execute "INSERT INTO TMP_ODBC ( Description )
SELECT '" & sDRV & "' AS ODBC"
Else
CurrentDb.Execute "INSERT INTO TMP_ODBCServeur (
Description ) SELECT '" & sDSN & "' AS ODBC"
End If
End If
Loop
End If
End Function

Public Sub DeleteDNS(ServerODBC As String, NameDNS As String)
'Supprimer un DNS
'Usage : DeleteDNS("Acomba ODBC Driver", "Acomba")
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String

strDriver = ServerODBC
strAttributes = "DSN=" & NameDNS & Chr$(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, strDriver,
strAttributes)
If intRet Then
MsgBox "Suppression du DNS réussi avec succès !", vbInformation,
"Suppression DNS"
Else
MsgBox "Impossible de supprimer le DNS", vbInformation,
"Suppression DNS"
End If

End Sub

'Ré-attache tout les tables d'une base de donnée ODBC
Public Sub MakeLinkODBC(ServerName As String, _
DNSName As String, _
DatabaseName As String, _
UserName As String, _
UserPass As String)

On Error GoTo errODBC
Dim SQLDb As DAO.Database
Dim dbLocal As DAO.Workspace
Dim i As Long
Dim TblName As String
Dim tdfCurrent As DAO.TableDef

Set dbLocal = DBEngine.Workspaces(0)
Set SQLDb = dbLocal.OpenDatabase("", False, False, "ODBC;DATABASE=" &
DatabaseName & _
";UID=" & UserName & _
";PWD=" & UserPass & _
";DSN=" & DNSName & _
";SERVER=" & ServerName &
"")

For i = 0 To SQLDb.TableDefs.Count - 1
TblName = SQLDb.TableDefs(i).Name
If Left(TblName, 4) = "dbo." And Left(TblName, 1) <> "~" And Left(TblName,
3) <> "sys" Then
TblName = Right(TblName, Len(TblName) - 4)
If TblName <> CurrentDb.TableDefs(i).Name Then
CurrentDb.TableDefs.Delete TblName
End If

Set tdfCurrent = CurrentDb.CreateTableDef(TblName)
tdfCurrent.Connect = "ODBC;DATABASE=" & DatabaseName & ";UID=" &
UserName & ";PWD=" & UserPass & ";DSN=" & DNSName

tdfCurrent.SourceTableName = TblName
CurrentDb.TableDefs.Append tdfCurrent
RefreshDatabaseWindow
End If
Next i

SQLDb.Close
dbLocal.Close

Exit Sub

errODBC:
If Err.Number = 3010 Then 'Objet existe déjà
Resume Next
ElseIf Err.Number = 3265 Then
Resume Next
Else
MsgBox Err.Description, vbExclamation, "SQL Serveur"
Resume Next
End If

End Sub
'// End code
Robert Simard
Logipro

Nov 13 '05 #6
On Wed, 6 Oct 2004 09:16:12 -0400, "JoeBlo"
<lo******************@hotmail.com> wrote:


'Start Code
Option Compare Database
Option Explicit

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
Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv&, ByVal
fDirection%, ByVal szDSN$, ByVal cbDSNMax%, pcbDSN%, ByVal szDescription$,
ByVal cbDescriptionMax%, pcbDescription%) As Integer
Private Declare Function SQLAllocEnv% Lib "ODBC32.DLL" (env&)
Const SQL_SUCCESS As Long = 0
Const SQL_FETCH_NEXT As Long = 1

'Constant Declaration
Private Const ODBC_ADD_DSN = 1 ' Add data source
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const vbAPINull As Long = 0& ' NULL Pointer

Sub MakeODBC(ProgPath As String, _
DataPath As String, _
UserName As String, _
PASSWORD As String, _
ServerODBC As String, _
NameDNS As String)

On Error GoTo errDNS
'Usage : MakeODBC "c:\Acomba","c:\f1000.dta\demo","Supervisor","demo "

Dim strConnect As String
Dim strAttributes As String

strAttributes = "SERVER=" & ServerODBC & Chr$(0)
strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
strAttributes = strAttributes & "DSN=" & NameDNS & Chr$(0)
strAttributes = strAttributes & "AcombaExe=" & ProgPath & Chr$(0)
strAttributes = strAttributes & "DBQ=" & DataPath & Chr$(0)
strAttributes = strAttributes & "UID=" & UserName & Chr$(0)
strAttributes = strAttributes & "PWD=" & PASSWORD & Chr$(0)

DBEngine.RegisterDatabase "Acomba", "Acomba ODBC Driver", True,
strAttributes

MsgBox "Création du DNS avec succès !", vbInformation, "Création de DNS"
Exit Sub
errDNS:
MsgBox "Erreur dans la création du DNS !", vbExclamation, "Création de
DNS"

End Sub

Public Function GetDSNsAndDrivers(ModeRetour As Integer) As String
Dim i As Integer
Dim sDSNItem As String * 1024
Dim sDRVItem As String * 1024
Dim sDSN As String
Dim sDRV As String
Dim iDSNLen As Integer
Dim iDRVLen As Integer
Dim lHenv As Long 'handle to the environment
Dim retvalue As String

On Error Resume Next

If ModeRetour = 1 Then
CurrentDb.Execute "DELETE TMP_ODBC.* FROM TMP_ODBC"
Else
CurrentDb.Execute "DELETE TMP_ODBCServeur.* FROM TMP_ODBCServeur"
End If
'get the DSNs
If SQLAllocEnv(lHenv) <> -1 Then
Do Until i <> SQL_SUCCESS
sDSNItem = Space$(1024)
sDRVItem = Space$(1024)
i = SQLDataSources(lHenv, SQL_FETCH_NEXT, sDSNItem, 1024,
iDSNLen, sDRVItem, 1024, iDRVLen)
sDSN = Left$(sDSNItem, iDSNLen)
sDRV = Left$(sDRVItem, iDRVLen)

If sDSN <> Space(iDSNLen) Then
If ModeRetour = 1 Then
CurrentDb.Execute "INSERT INTO TMP_ODBC ( Description )
SELECT '" & sDRV & "' AS ODBC"
Else
CurrentDb.Execute "INSERT INTO TMP_ODBCServeur (
Description ) SELECT '" & sDSN & "' AS ODBC"
End If
End If
Loop
End If
End Function

Public Sub DeleteDNS(ServerODBC As String, NameDNS As String)
'Supprimer un DNS
'Usage : DeleteDNS("Acomba ODBC Driver", "Acomba")
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String

strDriver = ServerODBC
strAttributes = "DSN=" & NameDNS & Chr$(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, strDriver,
strAttributes)
If intRet Then
MsgBox "Suppression du DNS réussi avec succès !", vbInformation,
"Suppression DNS"
Else
MsgBox "Impossible de supprimer le DNS", vbInformation,
"Suppression DNS"
End If

End Sub

'Ré-attache tout les tables d'une base de donnée ODBC
Public Sub MakeLinkODBC(ServerName As String, _
DNSName As String, _
DatabaseName As String, _
UserName As String, _
UserPass As String)

On Error GoTo errODBC
Dim SQLDb As DAO.Database
Dim dbLocal As DAO.Workspace
Dim i As Long
Dim TblName As String
Dim tdfCurrent As DAO.TableDef

Set dbLocal = DBEngine.Workspaces(0)
Set SQLDb = dbLocal.OpenDatabase("", False, False, "ODBC;DATABASE=" &
DatabaseName & _
";UID=" & UserName & _
";PWD=" & UserPass & _
";DSN=" & DNSName & _
";SERVER=" & ServerName &
"")

For i = 0 To SQLDb.TableDefs.Count - 1
TblName = SQLDb.TableDefs(i).Name
If Left(TblName, 4) = "dbo." And Left(TblName, 1) <> "~" And Left(TblName,
3) <> "sys" Then
TblName = Right(TblName, Len(TblName) - 4)
If TblName <> CurrentDb.TableDefs(i).Name Then
CurrentDb.TableDefs.Delete TblName
End If

Set tdfCurrent = CurrentDb.CreateTableDef(TblName)
tdfCurrent.Connect = "ODBC;DATABASE=" & DatabaseName & ";UID=" &
UserName & ";PWD=" & UserPass & ";DSN=" & DNSName

tdfCurrent.SourceTableName = TblName
CurrentDb.TableDefs.Append tdfCurrent
RefreshDatabaseWindow
End If
Next i

SQLDb.Close
dbLocal.Close

Exit Sub

errODBC:
If Err.Number = 3010 Then 'Objet existe déjà
Resume Next
ElseIf Err.Number = 3265 Then
Resume Next
Else
MsgBox Err.Description, vbExclamation, "SQL Serveur"
Resume Next
End If

End Sub
'// End code

Sweet!

Thanks, that did the trick.
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John Baker | last post by:
Hi: I would like to know how to create a temp DB to store the data in a table while I do something else with the table. Specifically, how do I create the temp remove the temp I want to be...
3
by: Keith Smith | last post by:
Are there any alternatives to sharing a database without creating a shared folder? I guess I could create a client/server app, but I'm trying to avoid that. On the other hand, I am also trying to...
3
by: kris.dorey | last post by:
Hi, Ive got the following code which seems ok but when the user runs the function for a second time I get an error message stating that the mdb is in use by another process. There is still an...
9
by: Maziar Aflatoun | last post by:
Hi everyone, I like to export some data from the database and allow my users to store this file to their harddrive as a .txt file. Does anyone know how I would go about doing that? (without...
5
by: Sam777 | last post by:
I was under the impression that creating the app_offline.htm file at the root of the webapp would cause all handles to be closed so that the app could be removed. Unfortunately, this isn't the...
5
by: Brian McClellan | last post by:
Just wondering if anyone has a simple example of creating a gridview completely programmatically, i'm not doing anything terribly sophisticated. When creating the gridview declaratively evertying...
3
by: sklett | last post by:
I have several objects that I'm saving to disk via the XmlSerializer. 80% of the time this works fine, but occasionaly it will create a file like this: <?xml version="1.0"?> <SystemOptions...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
8
by: Andrus | last post by:
Code below causes error in class definition line .....Isolator<T>' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'Myapp.Isolator<T>.GetEnumerator()'...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.