Change ODBC-Connection in Access 2007 - odbc

I hava a Pass-Through sql-query in access which queries my mysql-db.
My current ODBC connection for the query is defined as follows:
ODBC;UID=access_frontend; PWD=hello#world; DSN=my_db_test;
If I change my ODBC connection from my test to my normal DB
ODBC;UID=access_frontend; PWD=hello#world; DSN=my_db;
If I save my changes and restart again, Access has changed it back to my_db_test.
Is there any place where I can globally change my ODBC connection?`

I do not get this problem changing in code or manually. You can change the connection through VBA:
Dim qdf As QueryDef
''dbQSQLPassThrough = 112
For Each qdf In CurrentDb.QueryDefs
If qdf.Type = dbQSQLPassThrough Then
Debug.Print qdf.connect
qdf.connect = "ODBC;filedsn=z:\docs\test.dsn;"
Debug.Print qdf.connect
End If
Next
You will notice that the passthrough query illustrated refers to:
filedsn=z:\docs\test.dsn;
This is another easy way of changing the connection, just change the DSN, in the above case, you could just edit the file test.dsn.

Related

How to use the functionality of the new Data.DBXOdbc unit included in Delphi 10.2 Tokyo

I used to work with Delphi 2007 and I was able to connect to many different database mgmt. systems using the Open source dbExpress drivers of Vadim V. Lopushanskiy. Now I work with 10.2 Tokyo. I'm happy that I can connect to Access again after reading this thread. And to SQL Server, but I don't use it ;-)
I'm experiencing problems though esp. when trying to connect to Oracle and Sqlite3 in the kind of flexible way I'm used to. Let me focus on the problems I have with connecting to Sqlite. I use the SQLite ODBC Driver developed by Christian Werner. No success, i.e. I get this as error: 'unsupported info option 131'. It's unfortunate that there's so little documentation available how to use the functionality of the new Data.DBXOdbc unit. Hopefully Embarcadero will do something about this.
Below my code, from a DataModule unit with a TSQLConnection on it which is assigned to TCustomConnection called FDatabase. This is the code for connecting to Sqlite:
with (FDatabase As TSQLConnection) do
begin
VendorLib := 'sqlite3odbc.dll';
Params.Values['TRIMCHAR'] := 'True'; // 'Trim Char'
end;
if aUseDsn then
ConnStr := 'DSN=' + aDbxProviderStr + ';'
else
begin
// Work the database qualifier into a connection string.
// Assume that aDbxProviderStr contains that database qualifier.
Connstr := 'DRIVER=SQLite3 ODBC Driver;Database=';
Connstr := Connstr + aDbxProviderStr + ';';
end;
if (aUsername <> '') and (aPassword <> '') then
begin
ConnStr := ConnStr + 'UID=' + aUsername + ';PWD=' + aPassword + ';';
end;
with (FDatabase As TSQLConnection) do
begin
Params.Values[DATABASENAME_KEY] := '?';
Params.Values[CONNECTION_STRING] := ConnStr + 'coEnableUnicode=0;';
end;
(FDatabase As TSQLConnection).Open;
I hope somebody out there can show us how to overcome this problem.
Ok, it's apparently possible to use a TSQLConnection object for connecting to different databases, e.g. Sqlite, MS-Access or Oracle. Of course one at the time. You need to assign all the properties mentioned in the dbxconnections.ini file to the TSQLConnection object, starting with the DriverName property. This ini file can be found in folder C:\Users\Public\Documents\Embarcadero\Studio\dbExpress\19.0
I was hoping that I could connect to the 3 mentioned database management systems (dbms) via Odbc, but I gave up. After all, the error 'unsupported info option 131' is from the ODBC driver and this error is difficult to solve for me.
Instead of adding 1 TSQLConnection object to my data module, I added 3 of them. One for each dbms. When I want to connect, I assign it to the SQLConnection property of my TSQLDataset object - also found on the data module. Before I open the connection, I change only a few properties of the relevant TSQLConnection object, i.e. only the Params.Values. This way, you end up with a more stable application. The memory used by the TSQLConnection and TSQLDataset objects is freed when the data module is freed.
It means that I only use the Data.DBXOdbc unit when I want to connect to MS Access. For Oracle, I use the Oracle driver included in Delphi. And for Sqlite, I use the Sqlite driver. The main thing is that I can easily switch to another database ...

(asp.net) my sql-code (update statement) works in database but not in program

I wrote a program to easily store/edit my comic book collection. I can add, delete and request anything (comic/character/team) by my update statement doesn't work. I made a copy in a query in my access database and that worked perfectly. The strangest thing is that my program doesn't give any errors, it completes the transaction but doesn't change the data in my database. (this is a problem on both comic, character and team updates).
Here is my code to update a comic.
Public Sub UpdateComic(comic As Comic)
Dim cn As New OleDbConnection(persistenceController.connectiestring)
Dim cmd As New OleDbCommand("UPDATE tblComics SET serie = #serie, volume = #volume, issue = #issue, release = #release, inpossession = #inpossession, timesread = #read, story = #story, Languages = #languages WHERE ID = #ID", cn)
cmd.Parameters.AddWithValue("serie", comic.Serie)
cmd.Parameters.AddWithValue("volume", comic.Volume)
cmd.Parameters.AddWithValue("issue", comic.Issue)
cmd.Parameters.AddWithValue("release", comic.Release)
cmd.Parameters.AddWithValue("inpossession", comic.InPossession)
cmd.Parameters.AddWithValue("read", comic.Read)
cmd.Parameters.AddWithValue("story", comic.story)
cmd.Parameters.AddWithValue("ID", comic.ID)
cmd.Parameters.AddWithValue("Languages", comic.Language)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
End Sub
Can anyone help me with this.
Thanks in advance.
Switch the position of the last two parameters added to the OleDbParameterCollection
cmd.Parameters.AddWithValue("Languages", comic.Language)
cmd.Parameters.AddWithValue("ID", comic.ID)
OleDb doesn't recognize the parameters by their name, but by their position. You should add the parameters in the exact order defined by the parameter placeholders in the command text
By the way. OleDb wants its placeholders parameter defined with the single question mark character (?), but MS-Access accepts also the syntax #name probably to have a better upgrade compatibility with its big cousin Sql Server.

Link DB in Oracle parameter problem

I've got this Oracle query that links to another db but it doesn't seem to accept my parameters that i pass to the query
Here's my code
cmd.CommandText = "SELECT * FROM table1#dev tb1 join table2 tb2 on tb1.id = tb2.id WHERE tb1.id = :id"
cmd.CommandType = CommandType.Text
cmd.BindByName = True
cmd.Parameters.Add(New OracleParameter("id", id))
I get this error when do this
ORA-03113: end-of-file on communication channel
But if i simply change the query to not be using parameters then it works
Any ideas on how I could get this working with parameters?
EDIT
On further investigation if I try the following
cmd.CommandText = "SELECT * FROM table1#dev tb1 WHERE tb1.id = :id"
cmd.CommandType = CommandType.Text
cmd.BindByName = True
cmd.Parameters.Add(New OracleParameter("id", id))
I get a different error
ORA-28511: lost RPC connection to heterogeneous remote agent
I would really like a solution which allows parameters to be passed to the linked db but I can't seem to find anything about using parameters with linked db's
Thanks
ORA-03113 is a generic exception, hurled when something catastrophic causes the connection to drop. There may be some additional information in the alert log or a trace file. Check the background dump or diagnostics directory.
ORA-28511 is rather more specific. The error message points to a particular area for investigation; the advice is "Check for network problems and remote host crashes. The problem is probably in the agent software. "
So, it's not really a syntax problem. It's an environment (configuration/hardware) issue. I'm afraid you're going to have to do a some more digging.
Good luck.
have you tried specifying the parameter type ?:
OracleParameter p1 = new OracleParameter("id",OracleDbType.Decimal);
p1.Value=id;
cmd.Parameters.Add(p1);
Personally, this whole Oracle driver/query stuff really made me go nuts; e.g. I experienced that certain SELECT * generated a DivideByZeroException while replacing the * by the really required column names worked like a charm.
It could be that you have a similar issue here.
What helped me resolving the issues was to choose the ODP.NET drivers really wisely; we are running a production system here with beta drivers since they were the only ones working in all circumstances in our project.

Biztalk - can I change the receive port of an existing receive location?

I have two different receive ports and two receive locations - one location assigned to each port. The ports are set to receive the exact same type of file - I ended up with both because I consolidated two different applications that did the same thing.
I want to combine both locations into a single receive port, but I don't seem to be able to change the location that either belongs to - there's no option to do this that I can find. Essentially, I just want to take one location (either - I don't care), and assign it to the other port, so that one port has two locations and the other has none.
Does somebody know of a way to change the receive port of an existing location?
I resorted to the dark side, and updated the SQL table manually. I'd still welcome anybody who has a legitimate, supported way to do this, but to any others who need an answer, here's the script I wrote to fix this problem (no side-effects so far, though it's only been a day):
DECLARE #AppName VARCHAR(255),
#ReceiveLocationName VARCHAR(255),
#NewReceivePortName VARCHAR(255)
SET #AppName = 'Your application name'
SET #ReceiveLocationName = 'Name of your existing receive location'
SET #NewReceivePortName = 'Name of receive port to move location to'
DECLARE #NewPortID INT
DECLARE #ReceiveLocationID INT
SELECT #NewPortID = rp.[nID]
FROM [BizTalkMgmtDb].[dbo].[bts_application] a
JOIN [BizTalkMgmtDb].[dbo].[bts_receiveport] rp
ON a.nID = rp.nApplicationID
WHERE a.nvcName = #AppName
AND rp.nvcName = #NewReceivePortName
SELECT #ReceiveLocationID = Id
FROM [BizTalkMgmtDb].[dbo].[adm_receivelocation]
WHERE Name = #ReceiveLocationName
UPDATE [BizTalkMgmtDb].[dbo].[adm_receivelocation]
SET ReceivePortId = #NewPortID,
IsPrimary = 0
WHERE Id = #ReceiveLocationID
Please do not attempt such direct SQL changes in BizTalk system databases. You always use the API's provided by Microsoft.
Try either the ExplorerOM or WMI to do any such configuration changes.
http://msdn.microsoft.com/en-us/library/microsoft.biztalk.explorerom.receiveport_members(v=bts.10)
http://msdn.microsoft.com/en-us/library/ee277482(v=bts.10).aspx
If in case you make direct DB changes and raise Microsoft support, they won't support it.

What's wrong with this ASP recursive function?

When I call this function, everything works, as long as I don't try to recursively call the function again. In other words if I uncomment the line:
GetChilds rsData("AcctID"), intLevel + 1
Then the function breaks.
<%
Function GetChilds(ParentID, intLevel)
Set rsData= Server.CreateObject("ADODB.Recordset")
sSQL = "SELECT AcctID, ParentID FROM Accounts WHERE ParentID='" & ParentID &"'"
rsData.Open sSQL, conDB, adOpenKeyset, adLockOptimistic
If IsRSEmpty(rsData) Then
Response.Write("Empty")
Else
Do Until rsData.EOF
Response.Write rsData("AcctID") & "<br />"
'GetChilds rsData("AcctID"), intLevel + 1
rsData.MoveNext
Loop
End If
rsData.close: set rsData = nothing
End Function
Call GetChilds(1,0)
%>
*Edited after feedback
Thanks everyone,
Other than the usual error:
Error Type: (0x80020009) Exception occurred.
I wasn't sure what was causing the problems. I understand that is probably due to a couple of factors.
Not closing the connection and attempting to re-open the same connection.
To many concurrent connections to the database.
The database content is as follows:
AcctID | ParentID
1 Null
2 1
3 1
4 2
5 2
6 3
7 4
The idea is so that I can have a Master Account with Child Accounts, and those Child Accounts can have Child Accounts of their Own. Eventually there will be Another Master Account with a ParentID of Null that will have childs of its own. With that in mind, am I going about this the correct way?
Thanks for the quick responses.
Thanks everyone,
Other than the usual error:
Error Type: (0x80020009) Exception
occurred.
I wasn't sure what was causing the problems. I understand that is probably due to a couple of factors.
Not closing the connection and attempting to re-open the same connection.
To many concurrent connections to the database.
The database content is as follows:
AcctID | ParentID
1 Null
2 1
3 1
4 2
5 2
6 3
7 4
The idea is so that I can have a Master Account with Child Accounts, and those Child Accounts can have Child Accounts of their Own. Eventually there will be Another Master Account with a ParentID of Null that will have childs of its own. With that in mind, am I going about this the correct way?
Thanks for the quick responses.
Look like it fails because your connection is still busy serving the RecordSet from the previous call.
One option is to use a fresh connection for each call. The danger there is that you'll quickly run out of connections if you recurse too many times.
Another option is to read the contents of each RecordSet into a disconnected collection: (Dictionary, Array, etc) so you can close the connection right away. Then iterate over the disconnected collection.
If you're using SQL Server 2005 or later there's an even better option. You can use a CTE (common table expression) to write a recursive sql query. Then you can move everything to the database and you only need to execute one query.
Some other notes:
ID fields are normally ints, so you shouldn't encase them in ' characters in the sql string.
Finally, this code is probably okay because I doubt the user is allowed to input an id number directly. However, the dynamic sql technique used is very dangerous and should generally be avoided. Use query parameters instead to prevent sql injection.
I'm not too worried about not using intLevel for anything. Looking at the code this is obviously an early version, and intLevel can be used later to determine something like indentation or the class name used when styling an element.
Running out of SQL Connections?
You are dealing with so many layers there (Response.Write for the client, the ASP for the server, and the database) that its not surprising that there are problems.
Perhaps you can post some details about the error?
hard to tell without more description of how it breaks, but you are not using intLevel for anything.
How does it break?
My guess is that after a certain number of recursions you're probably getting a Stack Overflow (ironic) because you're not allocating too many RecordSets.
In each call you open a new connection to the database and you don't close it before opening a new one.
Not that this is actually a solution to the recursion issue, but it might be better for you to work out an SQL statement that returns all the information in a hierarchical format, rather than making recursive calls to your database.
Come to think of it though, it may be because you have too many concurrent db connections. You continually open, but aren't going to start closing until your pulling out of your recursive loop.
try declaring the variables as local using a DIM statement within the function definition:
Function GetChilds(ParentID, intLevel)
Dim rsData, sSQL
Set ...
Edit: Ok, I try to be more explicit.
My understanding is that since rsData is not declared by DIM, it is not a local variable, but a global var. Therefore, if you loop through the WHILE statement, you reach the .Eof of the inner-most rsData recordset. You return from the recursive function call, and the next step is again a rsData.MoveNext, which fails.
Please correct me if rsData is indeed local.
If you need recursion such as this I would personally put the recursion into a stored procedure and handle that processing on the database side in order to avoid opening multiple connections. If you are using mssql2005 look into something called Common Table Expressions (CTE), they make recursion easy. There are other ways to implement recursion with other RDBMS's.
Based on the sugestions I will atempt to move the query into a CTE (common table expression) when I find a good tutorial on how to do that. For now and as a quick and dirty fix, I have changed the code as follows:
Function GetChilds(ParentID, intLevel)
'Open my Database Connection and Query the current Parent ID
Set rsData= Server.CreateObject("ADODB.Recordset")
sSQL = "SELECT AcctID, ParentID FROM Accounts WHERE ParentID='" & ParentID &"'"
rsData.Open sSQL, conDB, adOpenKeyset, adLockOptimistic
'If the Record Set is not empty continue
If Not IsRSEmpty(rsData) Then
Dim myAccts()
ReDim myAccts(rsData.RecordCount)
Dim i
i = 0
Do Until rsData.EOF
Response.Write "Account ID: " & rsData("AcctID") & " ParentID: " & rsData("ParentID") & "<br />"
'Add the Childs of the current Parent ID to an array.
myAccts(i) = rsData("AcctID")
i = i + 1
rsData.MoveNext
Loop
'Close the SQL connection and get it ready for reopen. (I know not the best way but hey I am just learning this stuff)
rsData.close: set rsData = nothing
'For each Child found in the previous query, now lets get their childs.
For i = 0 To UBound(myAccts)
Call GetChilds(myAccts(i), intLevel + 1)
Next
End If
End Function
Call GetChilds(1,0)
I have working code with the same scenario.
I use a clientside cursor
...
rsData.CursorLocation = adUseClient
rsData.Open sSQL, conDB, adOpenKeyset, adLockOptimistic
rsData.ActiveConnectcion = Nothing
...
as pointed out in other responses, this is not very efficient, I use it only in an admin interface where the code is called infrequently and speed is not as critical.
I would not use such a recursive process in a regular web page.
Either rework the code to get all data in one call from the database, or make the call once and save it to a local array and save the array in an application variable.

Resources