Windows Develop Bookmark and Share   
 index > ClickOnce and Setup & Deployment Projects > Adding ODBC connection to a custom action in Windows Installer project
 

Adding ODBC connection to a custom action in Windows Installer project

To all,

I am trying to create an ODBC connection using the following code in VB. NET.

The following code produces the error: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

I have tried creating one string with all of the attributes hardcoded with the same results.

Any suggestions?

Thanks

Const ODBC_ADD_SYS_DSN = 4 'Add 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

Protected Sub AddODBC(ByVal strServerName As String, ByVal strDBName As String)

Try

'Creates the ODBC connection to the SQL Server database

Dim Driver As String

Dim Attributes As String

Driver =

"SQL Server"

Attributes =

"SERVER=" & strServerName & Chr(0)

Attributes = Attributes &

"DSN=SSM_DSN_Test" & Chr(0)

Attributes = Attributes &

"DESCRIPTION=Database Connection" & Chr(0)

Attributes = Attributes &

"DATABASE=" & strDBName & Chr(0)

SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, Driver, Attributes)

Catch ex As Exception

' Reports any errors and abort.

MsgBox(

"In exception handler in AddODBC fn: " & ex.Message)

Throw ex

End Try

End Sub

A123890  Wednesday, August 12, 2009 3:45 PM
Hello,

Instead of using the Declare statement, please also try with the DllImportAttribute to pInvoke that method. I've test the following code, and It works well on my machine.

Public Class Installer1

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add initialization code after the call to InitializeComponent

End Sub

Protected Overrides Sub OnBeforeInstall(ByVal savedState As System.Collections.IDictionary)
CreateSystemDSN()
MyBase.OnBeforeInstall(savedState)
End Sub

<DllImport("ODBCCP32.dll", CallingConvention:=CallingConvention.Winapi, CharSet:=CharSet.Unicode, SetLastError:=True)> _
Private Shared Function SQLConfigDataSource(ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer
End Function

Private Sub CreateSystemDSN()
Dim vAttributes As String = "DSN=DSN Name" & Chr(0)
vAttributes &= "Description=DSN Description" & Chr(0)
vAttributes &= "Trusted_Connection=Yes" & Chr(0)
vAttributes &= "Server=SQLINSTANCE" & Chr(0)
vAttributes &= "Database=databasename" & Chr(0)

If SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, "SQL Server", vAttributes) = 0 Then
MessageBox.Show("Failed to create ODBC data source!!")
End If
End Sub

Const ODBC_ADD_SYS_DSN As Integer = 4

End Class

As this installer actually works on the client machine, we need to make sure that MDAC is installed on the client machine, although it is installed by default after Windows XP.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer byA123890 Thursday, August 13, 2009 1:19 PM
  •  
Rong-Chun Zhang  Thursday, August 13, 2009 10:23 AM
Hello,

Instead of using the Declare statement, please also try with the DllImportAttribute to pInvoke that method. I've test the following code, and It works well on my machine.

Public Class Installer1

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add initialization code after the call to InitializeComponent

End Sub

Protected Overrides Sub OnBeforeInstall(ByVal savedState As System.Collections.IDictionary)
CreateSystemDSN()
MyBase.OnBeforeInstall(savedState)
End Sub

<DllImport("ODBCCP32.dll", CallingConvention:=CallingConvention.Winapi, CharSet:=CharSet.Unicode, SetLastError:=True)> _
Private Shared Function SQLConfigDataSource(ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer
End Function

Private Sub CreateSystemDSN()
Dim vAttributes As String = "DSN=DSN Name" & Chr(0)
vAttributes &= "Description=DSN Description" & Chr(0)
vAttributes &= "Trusted_Connection=Yes" & Chr(0)
vAttributes &= "Server=SQLINSTANCE" & Chr(0)
vAttributes &= "Database=databasename" & Chr(0)

If SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, "SQL Server", vAttributes) = 0 Then
MessageBox.Show("Failed to create ODBC data source!!")
End If
End Sub

Const ODBC_ADD_SYS_DSN As Integer = 4

End Class

As this installer actually works on the client machine, we need to make sure that MDAC is installed on the client machine, although it is installed by default after Windows XP.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer byA123890 Thursday, August 13, 2009 1:19 PM
  •  
Rong-Chun Zhang  Thursday, August 13, 2009 10:23 AM
I get a compiler error that the DllImport is not defined.
Is there an import statement that I have to add to the project so that it will recognize the DllImport statement?

Thanks,
Gloria
A123890  Thursday, August 13, 2009 1:02 PM

I imported

Imports

System.Runtime.InteropServices

and that fixed the problem of recognizingDllImport.

It now creates the DSN without the error

THANKS MR. ZHANG!!!!!

A123890  Thursday, August 13, 2009 1:25 PM

You can use google to search for other answers

Custom Search

More Threads

• Visual Studio 2008 .NET Deployment Project Overwriting User Scoped Settings
• Simple Click Once Deployment generates "An error occurred trying to download" file.
• How to update detected previous version, instead of removing...???
• Microsoft Certificate Services and ClickOnce
• ClickOnce - Find out server from within downloaded application
• How to resize Windows Forms controls on Internet Explorer page?
• Manifest hash problem created when add data files
• How to detect Everyone or Just Me installation setting in custom installer class
• Installer class unable to access network drives?
• How do you get an msi installer to check for a minimum version number of your already installed application