Shurikn Posted June 30, 2005 Posted June 30, 2005 I dont know how to do this. I want my program to copy an acess database to a folder, then create a DNS so that lets say... php can have acess to it. Is there a way to do this directly from C#? Quote
Shurikn Posted July 6, 2005 Author Posted July 6, 2005 ((Sorry if I double post in that topic... but The Edit button is just not here)) It seems like nobody can help me, but just to make sure everyone understand what I mean I'll explain more clearly.... I want to register an ODBC connection to an Acess database so that I can use odbc_connect('name','',''); So my installation program should do this... now i'll ask to question, 1: do you understand what i'm talking about? 2: is it even possible? 3: any place I could look? Thank you Quote
Shurikn Posted July 8, 2005 Author Posted July 8, 2005 (edited) Once again, I dont see the Edit button (does it diseapear after a while?) Anyway I found a way to do it! I fond some vb6 code on the web, wich I changed in vb.net, and it worked, but my program uses c# so I translated it to C#, but there is 1 problem (trhe last I hope) that make so it does not work... here's the solution in VB.NET Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer Private Const ODBC_ADD_SYS_DSN As Integer = 4 Public Function CreateSQLServerDSN(ByVal DSNName As String, ByVal ServerName As String, ByVal Database As String) As Boolean Dim sAttributes As String sAttributes = "DSN=" & DSNName & Chr(0) sAttributes = sAttributes & "Server=" & ServerName & Chr(0) sAttributes = sAttributes & "Database=" & Database & Chr(0) CreateSQLServerDSN = CreateDSN("SQL Server", sAttributes) End Function Public Function CreateAccessDSN(ByVal DSNName As String, ByVal DatabaseFullPath As String) As Boolean Dim sAttributes As String 'TEST TO SEE IF FILE EXISTS: YOU CAN 'REMOVE IF YOU DON'T WANT IT If Dir(DatabaseFullPath) = "" Then Exit Function sAttributes = "DSN=" & DSNName & Chr(0) sAttributes = sAttributes & "DBQ=" & DatabaseFullPath & Chr(0) CreateAccessDSN = CreateDSN("Microsoft Access Driver (*.mdb)", sAttributes) End Function Public Function CreateDSN(ByVal Driver As String, ByVal Attributes As String) As Boolean CreateDSN = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, Driver, Attributes) End Function and the C# Version I came up with: [DllImport("ODBCCP32.Dll", SetLastError=true,CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] private static extern int SQLConfigDataSource(int hwndParent, int fRequest,string lpszDriver,string lpszAttributes); private const int ODBC_ADD_SYS_DSN=4; public static int CreateSQLServerDSN(string DSNName,string ServerName,string Database) { string sAttributes="DSN=" + DSNName + Convert.ToChar(0); sAttributes+="Server=" + ServerName + Convert.ToChar(0); sAttributes+="Database=" + Database + Convert.ToChar(0); return CreateDSN("SQL Server",sAttributes); } public static int CreateAccessDSN(string DSNName,string DatabaseFullPath) { int returnValue=0; string sAttributes; if(System.IO.File.Exists(DatabaseFullPath)) { sAttributes="DSN=" + DSNName + Convert.ToChar(0); sAttributes+="DBQ=" + DatabaseFullPath + Convert.ToChar(0); returnValue=CreateDSN("Microsoft Access Driver (*.mdb)", sAttributes); } return returnValue; } public static int CreateDSN(string Driver,string Attributes) { return SQLConfigDataSource(0,ODBC_ADD_SYS_DSN,Driver,Attributes); } the problem comes at the last line, and I suspect it actually comes from the Chr(0) to Convert.ToChar(0) translation, because in C# Attribues="DSN=100232\0DBQ=C:\\bd\\1002.mdb\0" and in vb.net is it: "DSN=100232 the fact that the string is unfinished tells me that i'ts probably multiline or something, and that the watcher just does not show the DBQ=C\bd\1002.mdb" that's what I guess anyway... so does anyone know what is the REAL remplacement for Chr(0) in C#, or does anyone see something else? PS:((I am really sorry for triple posting, but I assure you the edit button is not here :( )) ============================EDIT=========================== I fond Another way of doing it directly in C#, wich include playing directly with the registry, this: MSDN article helped me find a way to do it: private void CreerODBCAcessDSN(string dsnName,string dbq, string driver, string uID) { RegistryKey software = Registry.LocalMachine.OpenSubKey("Software",true); RegistryKey odbc=software.OpenSubKey("ODBC",true); RegistryKey odbcini=odbc.OpenSubKey("ODBC.INI",true); RegistryKey nwKey=odbcini.CreateSubKey(dsnName); nwKey.SetValue("DBQ",dbq); nwKey.SetValue("Driver",driver); //nwKey.SetValue("DriverId",driverId); nwKey.SetValue("FIL","MS Access;"); nwKey.SetValue("DriverId",0x00000019); nwKey.SetValue("SafeTransactions",0x00000000); nwKey.SetValue("UID",uID); RegistryKey engine=nwKey.CreateSubKey("Engines"); RegistryKey jet=engine.CreateSubKey("Jet"); jet.SetValue("ImplicitCommitSync",""); jet.SetValue("MaxBufferSize",0x00000800); jet.SetValue("PageTimeout",0x00000005); jet.SetValue("Threads",0x00000003); jet.SetValue("UserCommitSync","Yes"); jet.Close(); engine.Close(); nwKey.Close(); RegistryKey odbcdata = odbcini.OpenSubKey("ODBC Data Sources",true); odbcdata.SetValue(dsnName,"Microsoft Access Driver (*.mdb)"); odbcdata.Close(); odbcini.Close(); odbc.Close(); software.Close(); } WARNING:: I've only tested in on my computer, and some of the value I entered may or may not be system dependant... I sugest being carefull as it could hurt your registry. PS: before using this code, check an exemple of what is actually in the registry by making a false entry by the ODBC creating tool, and then check the resul with regedit Edited July 8, 2005 by Shurikn Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.