Microsoft MVP성태의 닷넷 이야기
.NET Framework: 212. Firebird 데이터베이스와 ADO.NET [링크 복사], [링크+제목 복사],
조회: 23641
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 2개 있습니다.)
Firebird 데이터베이스와 ADO.NET

지난번 글에서 Firebird의 간편한 설치를 알아봤지요.

.NET 프로그래머에게도 유용한 Firebird 무료 데이터베이스
; https://www.sysnet.pe.kr/2/0/1038

물론 설치만 알아봐서는 안되죠. ^^ ADO.NET으로 접근하는 방법을 살펴봐야겠습니다. 우선 Firebird용 ADO.NET Provider를 구해야 하는데 이는 firebird 공식 홈페이지에 공개되어 있습니다.

DotNetFirebird - Using Firebird SQL in .NET.
; http://www.firebirdsql.org/dotnetfirebird/index.html

Firebird - Browse /firebird-net-provider at SourceForge.net
; http://sourceforge.net/projects/firebird/files/firebird-net-provider/

위의 폴더에 가면 현재(2011-05-16) 2.6.0 버전이 릴리스 된 것을 확인할 수 있고, MSI 파일과 ZIP 파일로 제공되는데 제 경우에는 NETProvider-2.6.0.zip 파일을 다운로드 받았습니다.

ZIP 압축을 풀면 다음과 같이 4개의 파일을 볼 수 있습니다.

  • FirebirdSql.Data.FirebirdClient.dll
  • FirebirdSql.Data.FirebirdClient.pdb
  • FirebirdSql.Data.UnitTests.dll
  • FirebirdSql.Data.UnitTests.dll.config

척 보면 아시겠지만, 실제로 사용하는 파일은 단지 "FirebirdSql.Data.FirebirdClient.dll" 하나입니다.




자, 이제 코드를 만들어봐야죠. ^^ 빈 프로젝트를 하나 만들고 "FirebirdSql.Data.FirebirdClient.dll" 파일을 참조 추가한 후, 지난번 글에서 실습했던 test.fdb로의 연결 및 CRUD 테스트를 해보겠습니다.

DB 파일: c:\temp\test.fdb
user 'SYSDBA' password 'masterkey'
테이블: TESTTABLE

코드는 그 자체가 설명서이니, 더 언급할 필요가 없겠죠. ^^

static void Main(string[] args)
{
    using (FbConnection fbConnection =
        new FbConnection(@"DataSource=localhost;Port=3050;Database=c:\temp\test.fdb;User=SYSDBA;Password=masterkey"))
    {
        fbConnection.Open();

        // Create
        FbCommand insertCommand = new FbCommand();
        insertCommand.Connection = fbConnection;
        insertCommand.CommandText = "INSERT INTO TESTTABLE(NAME, DESCRIPTION) VALUES (@NAME, @DESCRIPTION)";

        insertCommand.Parameters.Add("@NAME", FbDbType.VarChar, 50);
        insertCommand.Parameters.Add("@DESCRIPTION", FbDbType.VarChar, 150);

        string nameValue = "Name" + Guid.NewGuid().ToString(); ;
        insertCommand.Parameters[0].Value = nameValue;
        insertCommand.Parameters[1].Value = nameValue + "_Description";

        int affected = insertCommand.ExecuteNonQuery();
        Console.WriteLine("# of affected row: " + affected);

        // Update
        FbCommand updateCommand = new FbCommand();
        updateCommand.Connection = fbConnection;
        updateCommand.CommandText = "UPDATE TESTTABLE SET DESCRIPTION=@DESCRIPTION WHERE NAME=@NAME";

        updateCommand.Parameters.Add("@NAME", FbDbType.VarChar, 50);
        updateCommand.Parameters.Add("@DESCRIPTION", FbDbType.VarChar, 150);

        updateCommand.Parameters[0].Value = nameValue;
        updateCommand.Parameters[1].Value = nameValue + "_Description2";

        affected = updateCommand.ExecuteNonQuery();
        Console.WriteLine("# of affected row: " + affected);

        // Select - ExecuteScalar
        FbCommand selectCommand = new FbCommand();
        selectCommand.Connection = fbConnection;
        selectCommand.CommandText = "SELECT count(*) FROM TESTTABLE";
                
        object result = selectCommand.ExecuteScalar();
        Console.WriteLine("# of records: " + result);

        // Select - DataTable
        DataSet ds = new DataSet();
        FbDataAdapter da = new FbDataAdapter("SELECT * FROM TESTTABLE", fbConnection);
        da.Fill(ds, "testTable");

        DataTable dt = ds.Tables["testTable"];
        foreach (DataRow dr in dt.Rows)
        {
            Console.WriteLine(string.Format("Name = {0}, Desc = {1}", dr["NAME"], dr["DESCRIPTION"]));
        }

        // Delete
        FbCommand deleteCommand = new FbCommand();
        deleteCommand.Connection = fbConnection;
        deleteCommand.CommandText = "DELETE FROM TESTTABLE WHERE NAME=@NAME";

        deleteCommand.Parameters.Add("@NAME", FbDbType.VarChar, 50);
        deleteCommand.Parameters[0].Value = nameValue;

        affected = deleteCommand.ExecuteNonQuery();
        Console.WriteLine("# of affected row: " + affected);
    }
            
}

보시는 바와 같이, SQL Server 접근과 거의 다르지 않습니다. 그 외에, 몇 가지 예제 코드는 다음의 사이트에서 볼 수 있습니다.

DotNetFirebird - C# Sample Code
; http://www.firebirdsql.org/dotnetfirebird/sample-code.html

참고로, FirebirdSql.Data.FirebirdClient 파일이 .NET 3.5용이라고는 하지만 .NET 4.0 응용 프로그램 및 x86/x64 모두 정상적으로 동작했습니다.

(첨부한 파일은 위의 예제 코드를 담고 있습니다.)



[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 6/27/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2011-05-18 10시36분
[lancers] 오.. 매우 SQL Server스럽군요. 파라미터에 @가 들어가는거까지..
[guest]
2011-05-18 01시20분
그러게요. 서로 이렇게 맞춰주는 맛이 있어야 하는데... ^^
정성태

... [61]  62  63  64  65  66  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12113정성태1/12/202013485오류 유형: 590. Visual C++ 빌드 오류 - fatal error LNK1104: cannot open file 'atls.lib' [1]
12112정성태1/12/202010045오류 유형: 589. PowerShell - 원격 Invoke-Command 실행 시 "WinRM cannot complete the operation" 오류 발생
12111정성태1/12/202013316디버깅 기술: 155. C# - KernelMemoryIO 드라이버를 이용해 실행 프로그램을 숨기는 방법(DKOM: Direct Kernel Object Modification) [16]파일 다운로드1
12110정성태1/11/202011938디버깅 기술: 154. Patch Guard로 인해 블루 스크린(BSOD)가 발생하는 사례 [5]파일 다운로드1
12109정성태1/10/20209841오류 유형: 588. Driver 프로젝트 빌드 오류 - Inf2Cat error -2: "Inf2Cat, signability test failed."
12108정성태1/10/20209887오류 유형: 587. Kernel Driver 시작 시 127(The specified procedure could not be found.) 오류 메시지 발생
12107정성태1/10/202010829.NET Framework: 877. C# - 프로세스의 모든 핸들을 열람 - 두 번째 이야기
12106정성태1/8/202012218VC++: 136. C++ - OSR Driver Loader와 같은 Legacy 커널 드라이버 설치 프로그램 제작 [1]
12105정성태1/8/202010901디버깅 기술: 153. C# - PEB를 조작해 로드된 DLL을 숨기는 방법
12104정성태1/7/202011583DDK: 9. 커널 메모리를 읽고 쓰는 NT Legacy driver와 C# 클라이언트 프로그램 [4]
12103정성태1/7/202014315DDK: 8. Visual Studio 2019 + WDK Legacy Driver 제작- Hello World 예제 [1]파일 다운로드2
12102정성태1/6/202011897디버깅 기술: 152. User 권한(Ring 3)의 프로그램에서 _ETHREAD 주소(및 커널 메모리를 읽을 수 있다면 _EPROCESS 주소) 구하는 방법
12101정성태1/5/202011237.NET Framework: 876. C# - PEB(Process Environment Block)를 통해 로드된 모듈 목록 열람
12100정성태1/3/20209269.NET Framework: 875. .NET 3.5 이하에서 IntPtr.Add 사용
12099정성태1/3/202011584디버깅 기술: 151. Windows 10 - Process Explorer로 확인한 Handle 정보를 windbg에서 조회 [1]
12098정성태1/2/202011169.NET Framework: 874. C# - 커널 구조체의 Offset 값을 하드 코딩하지 않고 사용하는 방법 [3]
12097정성태1/2/20209718디버깅 기술: 150. windbg - Wow64, x86, x64에서의 커널 구조체(예: TEB) 구조체 확인
12096정성태12/30/201911701디버깅 기술: 149. C# - DbgEng.dll을 이용한 간단한 디버거 제작 [1]
12095정성태12/27/201913114VC++: 135. C++ - string_view의 동작 방식
12094정성태12/26/201911292.NET Framework: 873. C# - 코드를 통해 PDB 심벌 파일 다운로드 방법
12093정성태12/26/201911313.NET Framework: 872. C# - 로딩된 Native DLL의 export 함수 목록 출력파일 다운로드1
12092정성태12/25/201910752디버깅 기술: 148. cdb.exe를 이용해 (ntdll.dll 등에 정의된) 커널 구조체 출력하는 방법
12091정성태12/25/201912248디버깅 기술: 147. pdb 파일을 다운로드하기 위한 symchk.exe 실행에 필요한 최소 파일 [1]
12090정성태12/24/201910915.NET Framework: 871. .NET AnyCPU로 빌드된 PE 헤더의 로딩 전/후 차이점 [1]파일 다운로드1
12089정성태12/23/201911592디버깅 기술: 146. gflags와 _CrtIsMemoryBlock을 이용한 Heap 메모리 손상 여부 체크
12088정성태12/23/201910574Linux: 28. Linux - 윈도우의 "Run as different user" 기능을 shell에서 실행하는 방법
... [61]  62  63  64  65  66  67  68  69  70  71  72  73  74  75  ...