An other approach to this would be to directly edit the registry on the local
machine. Note: This only works for System DSN's.
Here is an example of how to create a SystemDSN for SQL Server:
#using System;
#using Microsoft.Win32
..
..
public class clsODBC
{
private string sDSNName = "";
private string sDB = "";
private string sServer = "";
public clsODBC(string sDSNName, string sDB,string sServer)
{
this.sDSNName = sDSNName;
this.sDB = sDB;
this.sServer = sServer;
}
public bool AddSystemDSN_SQLServer(string sUID, string sPWD)
{
try
{
//create the ODBC Data Source
string sDriver = System.Environment.GetEnvironmentVariable("SystemRoot")
+ @"\System32\SQLSRV32.dll";
RegistryKey oRegKey = Registry.LocalMachine;
oRegKey.OpenSubKey(@"SOFTWARE\ODBC\ODBC.INI",true).CreateSubKey(sDSNName);
oRegKey.OpenSubKey(@"SOFTWARE\ODBC\ODBC.INI\" +
sDSNName,true).SetValue("Database",sDB);
oRegKey.OpenSubKey(@"SOFTWARE\ODBC\ODBC.INI\" +
sDSNName,true).SetValue("Driver",sDriver);
oRegKey.OpenSubKey(@"SOFTWARE\ODBC\ODBC.INI\" +
sDSNName,true).SetValue("Server",sServer);
if(sUID.Trim() == "*") //if Windows Authentication is used
{
string sLastUser = System.Environment.GetEnvironmentVariable("USERNAME");
oRegKey.OpenSubKey(@"SOFTWARE\ODBC\ODBC.INI\" +
sDSNName,true).SetValue("Trusted_Connection","Yes");
oRegKey.OpenSubKey(@"SOFTWARE\ODBC\ODBC.INI\" +
sDSNName,true).SetValue("LastUser",sLastUser);
}
else
{
oRegKey.OpenSubKey(@"SOFTWARE\ODBC\ODBC.INI\" +
sDSNName,true).SetValue("LastUser",sUID);
}
//make the new System DSN visible in the ODBC manager
oRegKey.OpenSubKey(@"SOFTWARE\ODBC\ODBC.INI\ODBC Data
Sources",true).SetValue(sDSNName,"SQL Server");
oRegKey.Close();
return true;
}
catch
{
return false;
}
}
}
Nicholas Paldino said:
Hans,
I think I see part of the problem. Your definition for
SQLConfigDataSource should be:
[DllImport("ODBCCP32.DLL", CharSet=CharSet.Ansi)]
static extern bool SQLConfigDataSource(
IntPtr parent,
[MarshalAs(UnmanagedType.U2)
short request,
string driver,
string attributes);
You had request defined as an int, which was shifting the driver and
attributes parmaeters by two bytes.
I've also updated the definition on pinvoke.net, which was incorrect as
well.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
static extern int This function is used to work with the ODBC Datasources
5/20/2004 2:13:16 PM - (e-mail address removed)-131.107.3.92
SQLConfigDataSource (int hwndParent, int fRequest, string lpszDriver, string
lpszAttributes);
VB Signature:
DerekS said:
I've attached the class.
Here's the basic idea.
I have a simple form that gathers:
Server Name
New DSN Name
I know it SQL Server
I know the server is called 'csgsql2000'
and the user/password is 'ip'/'ip'
I know the remote server the DSN is getting created on is called 'CSAPP13'
See small attached from MSDN
in
message news:
[email protected]...
Hans,
I assume that you are calling the ConfigDSN function in ODBC.dll (I
think that is the dll)? If so, can you show the declarations, as well as
the call for the function?
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Yes.. It must be created.
DerekS wrote:
Hi,
I've been pulling my hair out trying to write a simple method to
programatically create a system DSN with all parameters on a remote
machine. I have ensured I have the correct permissions on that
remote
machine. I have also review many MSDN and codeproject.com articles.
Any help would be great.
Thanks,
Derek
It *is* possible to connect without a DSN, or do you have
specific reasons for using it?
Hans Kesting