...
Code Block |
---|
title | Visual C++ example source |
---|
theme | DJangoEmacs |
---|
language | cpp |
---|
|
#include <Afx.h>
#include <Afxdb.h>
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
CDatabase db;
try
{
db.OpenEx(_T("ODBC connection string”), CDatabase::noOdbcDialog);
AfxMessageBox (_T("Connect OK"));
}catch (CDBException *e)
{
AfxMessageBox(e->m_strError);
}
return 0;
}
|
...
Code Block |
---|
title | Visual C# example source |
---|
theme | DJangoEmacs |
---|
language | c# |
---|
|
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
OdbcConnection cn = new OdbcConnection();
try
cn.ConnectionString = “ODBC connection string”;
cn.Open();
Console.WriteLine("connect ok");
}
catch (OdbcException ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
} |
...
Code Block |
---|
title | Visual Basic example source |
---|
language | vb |
---|
|
Sub Main()
Dim cn As Odbc.OdbcConnection
Dim cmd As Odbc.OdbcCommand
Dim dr As Odbc.OdbcDataReader
cn = New Odbc.OdbcConnection
cmd = New Odbc.OdbcCommand
cn.ConnectionString = “ODBC connection string”
Try
cn.Open()
Console.WriteLine("Successfully connected.")
cmd.Connection = cn
cmd.CommandText = "SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD HH:MI:SS')FROM DUAL"
dr = cmd.ExecuteReader()
While (dr.Read())
Console.WriteLine(dr.GetString(0))
End While
Catch ex As Odbc.OdbcException
Console.WriteLine("Error in the connection" + ex.Message)
End Try
Console.ReadLine()
End Sub |
...
If not, NULL for LOB data type is brought when retrieving, or the following error occurs when insert/update time.
Code Block |
---|
theme | DJangoEmacs |
---|
language | bash |
---|
|
Connection is in autocommit mode. One can not operate on LOB datas with autocommit mode on. |
...
Code Block |
---|
title | Example of BLOB insert |
---|
theme | DJangoEmacs |
---|
language | c# |
---|
|
// BLOB INSERTFileStream fs = new FileStream("c:\\test.dat", FileMode.Open, FileAccess.Read);
Byte[] blob = new byte[fs.Length];
fs.Read(blob, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
OdbcTransaction tx = cn.BeginTransaction();
cmd.Transaction = tx;
cmd.CommandText = "INSERT INTO T1 (C1, C2) VALUES (?, ?)";
cmd.Parameters.Add("C1", OdbcType.Int);
cmd.Parameters.Add("C2", OdbcType.Binary);
cmd.Parameters[0].Value = 1;
cmd.Parameters[1].Value = blob;
cmd.ExecuteNonQuery();
tx.Commit(); |
Code Block |
---|
title | Example of BLOB select |
---|
theme | Emacs |
---|
language | c# |
---|
|
// BLOB SELECTcmd.CommandText = "SELECT binary_length(C2), C2 FROM T1";
tx = cn.BeginTransaction();
cmd.Transaction = tx;
OdbcDataReader dr = cmd.ExecuteReader();
int len;
while (dr.Read())
{
len = dr.GetInt32(0);
Byte[] ff = new Byte[len];
dr.GetBytes(1, 0, ff, 0, len);
fs = new FileStream("c:\\test.dat", FileMode.CreateNew, FileAccess.Write);
fs.Write(ff, 0, len);
fs.Close();
} |
...