EMDI는 지금도 개발중

C# : SQLite를 통해 전자세금계산서 XML 파일 생성 (1) 본문

IT/전자세금계산서

C# : SQLite를 통해 전자세금계산서 XML 파일 생성 (1)

EMDI 2020. 4. 6. 14:08

SQLite를 통해 전자세금계산서 XML파일 생성을 해보도록 하겠습니다. 아마 전자세금계산서 파일을 생성하는 것에 필요한 순서는 대략적으로 위와 같을 것 같습니다.

 

1. SQLite Database File 생성 

보통 프로젝트를 시작하기 전 Database와 Table은 이미 생성을 해놓으셨을거에요. 만약 미리 생성 하신분들은 C# : SQLite를 통해 전자세금계산서 XML 파일 생성 (1) 해당 글을 패스하시면 되는데 아직 생성을 못하신 분들은 아래의 내용을 참고해주세요.

using System.IO;            // Exists 체크할 때 필요
using System.Windows.Forms; // Application 사용할 때 필요

// 만약 SQLite로 쓰실 분들은 아래의 using 
using System.Data.SQLite;   // SQLite 사용할 때 필요

// SQLite 아닌 MS-SQL이나 다른걸 사용하시는 분들은 아래의 using
using System.Data.Common;   // DbConnection
using System.Reflection;    // Assembly 사용할 때 필요

// SQLite Database Create 메소드
public int CreateDatabase()
{
    try
    {
        // Database file 이름
        string szDBFileName = "taxinvoice.db";
        // 애플리케이션이 있는 위치 경로
        string szExecutablePath = Path.GetDirectoryName(Application.ExecutablePath);

        // 저는 Database폴더 안에 taxinvoice.db를 넣을 예정이라 따로 폴더경로를 땄습니다.
        string szFolderPath = String.Format(@"{0}\{1}", szExecutablePath, "Database");

        // 폴더관련된 생성자 
        DirectoryInfo dic = new DirectoryInfo(szFolderPath);

        // 만약 ExecutablePath 안에 Database폴더가 없으면 생성합니다. 
        if (dic.Exists == false) 
        {
        	dic.Create();
        }

        // Database 지정할 경로 +Database 이름
        string szDBFile = String.Format(@"{0}\{1}\{2}", szExecutablePath, "Database", szDBFileName);

        // DB접속정보 
        string constring = string.Format("Data Source={0};Version=3;", szDBFile);

        // Database file이 있는지 체크
        if (!System.IO.File.Exists(szDBFile))
        {
        	// file이 없으면 SQLite DB file을 생성합니다.
        	SQLiteConnection.CreateFile(szDBFile);
        }

    }
    catch (Exception e)
    {
    	Trace.WriteLine("[Exception] DB Create Error : " + e.ToString());
    	return -99;
    }

    return 0;
}

 

해당 소스를 실행해보면 위와 같이 taxinvoice.db 파일이 생긴 것을 확인할 수 있습니다. 

 

2. SQLite Database 연결 및 Table 생성

첫 번째 데이터베이스 생성 단계를 통해 우리는 기준이 되는 데이터베이스를 만들었습니다. 이제 2단계에서는 해당 데이터베이스를 통해 Table를 생성해봅시다. 테이블을 생성할 때는 Database 연결을 우선 해야합니다.

1) SQLiteConnection을 이용해서 연결

public int connect()
{
    try
    {
        // 애플리케이션이 있는 위치 경로
        string szExecutablePath = Path.GetDirectoryName(Application.ExecutablePath);
        string szDBFileName = "taxinvoice.db";
        string szDBFile = String.Format(@"{0}\{1}\{2}", szExecutablePath, "Database", szDBFileName);
        // DB접속정보 
        string constring = string.Format("Data Source={0};Version=3;", szDBFile);

        sqliteConnection = new SQLiteConnection();
        sqliteConnection.ConnectionString = constring;
        sqliteConnection.Open();

        return 0;
    }
    catch (Exception ex)
    {
    	dbConnection.Close();
    	return -99;
    }

}

 

3) Table 생성

public int CreateTable()
{
    try
    {
        // 테이블을 생성하는 쿼리문 
        string sqlCreate = "CREATE TABLE CERT_INFO ( cert_key int, cert_nm nvarchar(200),cert_pwd nvarchar(100),cert_regorg nvarchar(100),cert_regdate nvarchar(8),cert_expdate nvarchar(8) ) ";

        SQLiteCommand command = new SQLiteCommand(sqlCreate, sqliteConnection);
        int result = command.ExecuteNonQuery();


    }
    catch (Exception e)
    {
        Trace.WriteLine("[Exception] Table Create Error : " + e.ToString());
        return -99;
    }

    return 0;
}

전자세금계산서는 관리정보, 상품정보 등 테이블이 다양하게 있으니 위의 소스를 참고해서 만들거나 미리 테이블을 생성하시면 좋을 듯 싶습니다.

Comments