ADODB connection to firebird - adodb

How to I connect to firebird database with ADODB connection?
I'm using testcomplete to test application runing on firebird.
following doesn't work...
Conn = new ActiveXObject("ADODB.Connection");
constr = 'User=SYSDBA;Password=masterkey;Database=C:\..\Sample.fdb;DataSource=Local;Port=3050;Dialect=3;Charset=NONE;Role=;Connection lifetime=15;Pooling=true;MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;ServerType=0;'
Conn.ConnectionString = constr;
qrery = 'Select Count(*) FROM XX ';
recSet = new ActiveXObject("ADODB.Recordset");
recSet.Open(qrery,Conn,3 /* adOpenStatic */, 1 /* adLockReadOnly */,1);
Log.Message(recSet.Fields.Item(0).Value );
recSet.Close();
Get following error
The connection cannot be used to perform this operation. It is either closed or invalid in this context

You need to open the connection after creating it and before making a query:
var Conn = new ActiveXObject("ADODB.Connection");
var constr = 'User=SYSDBA;Password=masterkey;Database=C:\\..\\Sample.fdb;DataSource=Local;Port=3050;Dialect=3;Charset=NONE;Role=;Connection lifetime=15;Pooling=true;MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;ServerType=0;'
Conn.ConnectionString = constr;
var qrery = 'Select Count(*) FROM XX ';
Conn.Open(); // Openning the connection
var recSet = new ActiveXObject("ADODB.Recordset");
recSet.Open(qrery,Conn,3 /* adOpenStatic */, 1 /* adLockReadOnly */,1);
Log.Message(recSet.Fields.Item(0).Value );
recSet.Close();

Related

Oracle VBAscript connection error

Dim strDBDesc As String
strDBDesc = "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = ##)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = ##)))"
cn.Open "Provider=OraOLEDB.Oracle;Data Source=" & strDBDesc & ";User ID=##;Password=##;"
query1 = ""
query1 = ERM.Sheets("query").Range("A10")
query1 = Replace(query1, "v_job_name", v_field1)
'Set OraDynaSet = objdatabase.DBCreateDynaset(query1, 0&)
Dim OraDynaSet As ADODB.Recordset
Set OraDynaSet = CreateObject("ADODB.Recordset")
OraDynaSet.ActiveConnection = cn
OraDynaSet.Open query1, cn, adOpenStatic
I am getting error message as ORA-00933 sql command not ended properly
Error Message
Query 1
select
job_name,Status
from (
select Distinct a.job_name,
a.description description,Decode (job_type,98,'Box',99,'Command Job',102,'File watcher job',job_type) job_type,
substr(decode(d.status,1,'Running',
3,'Starting',
4,'Success',
5,'Failed',
6,'Terminated',
7,'On Ice',
8,'Inactive',
9,'Activated',
11,'On Hold',
12,'Que Wait',
d.status),1,9) status,
a.mach_name,a.owner,g.command,g.std_err_file,g.std_out_file,f.days_of_week,f.start_times,f.start_mins,f.run_calendar,f.max_run_alarm,profile
from AEDBADMIN.ujo_job a,
AEDBADMIN.ujo_job_runs c,
AEDBADMIN.ujo_job_status d,
(select joid,max(STARTIME) startime,
max(endtime) endtime
from AEDBADMIN.ujo_job_runs group by joid) e,
AEDBADMIN.ujo_command_job g,
AEDBADMIN.ujo_sched_info f
where a.joid = c.joid(+)
and a.joid = d.joid(+)
and a.joid = e.joid(+)
and a.joid = f.joid(+)
and a.joid = g.joid(+)
and (c.startime = e.startime or c.startime is null)
and job_name ='v_job_name'// job name replaces
and a.is_active =1
);
I think the problem is this one
and job_name ='v_job_name'// job name replaces
In SQL comments are done by -- ... (single line) or /* ... */ (multi line).
However, perhaps ADODB does not support comments at all, I recommend to remove it entirely.
Or it is the semicolon ; at the end - remove it.
Just a note, you should rewrite your query and use ANSI join syntax instead of old Oracle join syntax - especially for OUTER JOINS.

VBA Recordset / Oracle and Excel Connect

I've installed tries recordset in my vba statement.
Unfortunately he accesses only the first line in my database. Who can help me?
I'm not very good in VBA it's my first porject. I hope someone can help me with my code. Thank you
Sub Testbox()
Dim conn, Rs
Dim strSQL As String
Dim auswahl As Integer
auswahl = MsgBox("Die Daten werden geladen", vbOKCancel, "Bitte auswählen")
If auswahl = 1 Then
connstring = "UID=user;PWD=passwort;DRIVER={Microsoft ODBC For Oracle};SERVER=server.WORLD;"
Set conn = New ADODB.Connection
With conn
.ConnectionString = connstring
.CursorLocation = adUseClient
.Mode = adModeRead
.Open
End With
Set Rs = CreateObject("ADODB.Recordset")
strSQL = "select * from table where logdatum =1507"
Rs.Open strSQL, conn, 3, 3
Range("A2:A5000") = Rs("scanclient")
Range("B2:B500") = Rs("Sum")
Range("C2:C500") = Rs("batchclass")
Rs.Close
Set Rs = Nothing
conn.Close
Set conn = Nothing
Else
Exit Sub
End If
End Sub
Unfortunately, it is not possible to print data from Recordset into worksheet like that:
Range("A2:A5000") = Rs("scanclient")
Range("B2:B500") = Rs("Sum")
Range("C2:C500") = Rs("batchclass")
You need to replace this code with the below:
Dim i As Long: i = 1
Do Until Rs.EOF
i = i + 1
Cells(i, 1) = Rs("scanclient")
Cells(i, 2) = Rs("Sum")
Cells(i, 3) = Rs("batchclass")
Call Rs.MoveNext
Loop

types mismatch in VBScript

I am trying to get number of table's rows in db and output it to console using VBScript, but when I execute following code I get type mismatch error, what should I change in my code to force it execute without errors
Dim loop_lim
Const DB_CONNECT_STRING = "Provider=SQLOLEDB.1;Data Source=BUG\SQLSERVER2005;Initial Catalog=test;user id ='sa';password='111111'"
Set myConn = CreateObject("ADODB.Connection")
Set myCommand = CreateObject("ADODB.Command" )
myConn.Open DB_CONNECT_STRING
Set myCommand.ActiveConnection = myConn
myCommand.CommandText = "select count(*) from oferty o inner join rep_oferta ro on o.indeks = ro.srcdoc inner join rep_pozycje rp on o.indeks = rp.srcdoc"
loop_lim = myCommand.Execute
WScript.Echo loop_lim
Change
loop_lim = myCommand.Execute
to
Set loop_lim = myCommand.Execute
because .Execute returns a recordset object. Then think about how to get values from the recordset rsp. it's fields.

How to get image from SQL Server

I have table called IMG and there are columns ID and Content that holds pictures.
How is it possible to get picture from there?
I googled for this problem and what all I got was vb.net, c# and php - mysql.
Maybe someone can say what is the best and easiest way to get picture (sample, copy/paste code, program)?
Thank you!
try something like this (from d_r_w's answer):
SqlDataAdapter dataAdapter = new SqlDataAdapter(
new SqlCommand("SELECT pic FROM imageTest WHERE pic_id = 1",
yourConnectionReference));
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count == 1)
{
Byte[] data = new Byte[0];
data = (Byte[])(dataSet.Tables[0].Rows[0]["pic"]);
MemoryStream mem = new MemoryStream(data);
yourPictureBox.Image= Image.FromStream(mem);
}
Take alook at the complete answer
More options to do this:
Uploading / Downloading Pictures to / from a SQL Server
http://www.sqlusa.com/bestpractices/imageimportexport/
I found this post regards PHP but with MySQL(you need to change the connection to your server):
<?php
// image.php - by Hermawan Haryanto <hermawan#dmonster.com>
// Example PHP Script, demonstrating Storing Image in Database
// Detailed Information can be found at http://www.codewalkers.com
// database connection
$conn = mysql_connect("localhost", "user", "password")
OR DIE (mysql_error());
#mysql_select_db ("hermawan", $conn) OR DIE (mysql_error());
$sql = "SELECT * FROM image WHERE image_id=".$_GET["iid"];
$result = mysql_query ($sql, $conn);
if (mysql_num_rows ($result)>0) {
$row = #mysql_fetch_array ($result);
$image_type = $row["image_type"];
$image = $row["image"];
Header ("Content-type: $image_type");
print $image;
}
?>
after a lot of reading and work found the solution!
Here it is! Works 100%
Dim conn, sql, a, filename
Dim dir
dir = "c:\images\" //saves files into this directory
Dim fileObj
Set fileObj = CreateObject("Scripting.FileSystemObject")
Set conn = CreateObject("ADODB.Connection")
Dim strStream
Set strStream = CreateObject("ADODB.Stream")
Dim rstRecordset
Set rstRecordset = CreateObject("ADODB.Recordset")
conn.Open "Provider=SQLOLEDB;Data Source=yourserver;Integrated Security = SSPI","username","password"
rstRecordset.Open "Select xxx, yyy, zzz from table (nolock) where xxx = '' order by xxx desc", conn ', adOpenKeyset, adLockOptimistic
Set fso = CreateObject("Scripting.FileSystemObject")
While Not rstRecordset.EOF
filename = rstRecordset.Fields(0)
er = 0
Do
er = er+1
Loop While (fso.FileExists(dir & filename & "_" & er & ".JPG"))
filename = dir & filename & "_" & er & ".JPG"
strStream.Type = 1
strStream.Open
strStream.Write rstRecordset.Fields(2).Value
strStream.SaveToFile filename
strStream.Close
rstRecordset.MoveNext
Wend
Here it is!

Parameterized query in Classic Asp

My db access code is like following:
set recordset = Server.CReateObject("ADODB.Recordset")
set cmd1 = Server.CreateObject("ADODB.Command")
cmd1.ActiveConnection = Conn //connection object already created
cmd1.CommandText = "SELECT * FROM lbr_catmaster where catname = ?"
cmd1.CommandType = adCmdText
set prm = cmd1.CreateParameter("#prm", 200, 1,200 , "development")
cmd1.Parameters.Append prm
set recordset = cmd1.Execute
But there is no db hit going. Please help with this. I am using sql server 2005.
Thanks.
In my code, this is how I get a recordset from a command:
Set rs = server.createobject("ADODB.Recordset")
Set cmd = server.createobject("ADODB.Command")
cmd.ActiveConnection = Conn //connection object already created
cmd.CommandText = "SELECT * FROM lbr_catmaster where catname = ?"
cmd.CommandType = adCmdText
cmd.CommandTimeout = 900
set prm = cmd.CreateParameter("#prm", 200, 1, 200, "development")
cmd.Parameters.Append prm
' Execute the query for readonly
rs.CursorLocation = adUseClient
rs.Open cmd, , adOpenForwardOnly, adLockReadOnly
Hope it helps
I like using Parameters.Refresh, i.e.
set recordset = Server.CReateObject("ADODB.Recordset")
set cmd1 = Server.CreateObject("ADODB.Command")
cmd1.ActiveConnection = Conn ' connection object already created
cmd1.CommandText = "SELECT * FROM lbr_catmaster where catname = ?"
cmd1.CommandType = adCmdText
cmd1.Prepared = True ' only needed if u plan to reuse this command often
cmd1.Parameters.Refresh
cmd1.Parameters(0).Value = "development"
set recordset = cmd1.Execute
Looks like you aren't referencing your named parameter correctly in your query.
Try replacing:
cmd1.CommandText = "SELECT * FROM lbr_catmaster where catname = ?"
with:
cmd1.CommandText = "SELECT * FROM lbr_catmaster where catname = #prm"
and see if that helps.
If you have a complex criteria using parameters here is an example I had to create based on my requirements
declare #loc smallint = ? , #dt1 date = ? SET #loc = ISNULL(#loc, 999)
SELECT m.* , c.*
FROM Costs c INNER JOIN MbrData m ON c.SN = m.SN and c.startDT = m.startDT
WHERE (m.LocationID = #loc OR #loc = 999) AND (MonthYear = #dt1 OR #dt1 IS NULL)
ORDER BY m.LocationID
then in your asp
cmd.CommandText = strSQL ' the string above
cmd.CommandType = 1 ' adCmdText
cmd.Parameters.Append cmd.CreateParameter("#loc",2,1) 'adSmallInt=2, adParamInput=1
cmd.Parameters("#loc") = rptlocation ' scrubbed location ID
cmd.Parameters.Append cmd.CreateParameter("#dt1",7,1) 'adDate=7, adParamInput=1
cmd.Parameters("#dt1") = scrubbed formatted date
set rst = cmd.Execute
Try leaving off the parameter name:
set prm = cmd1.CreateParameter(, 200, 1,200 , "development")

Resources