Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 개발
- PostgreSQL
- vue환경세팅
- 장고웹프로젝트
- vue환경설정
- forof문
- sqlite
- Python
- 오류종류
- 파이썬
- 장고프로젝트
- 자바스크립트for문
- R데이터분석
- 자바스크립트날짜형식
- vue프로젝트
- 청소년복지론
- 자바스크립트수학
- 사례관리
- javaScriptError
- 스택개념
- 장고
- cmd명령어
- 자바스크립트날짜
- 스택과큐의차이점
- Android
- 자바스크립트날짜get
- 안드로이드
- 장고웹
- 큐개념
- 자바스크립트forinforof차이
Archives
- Today
- Total
지금도 개발중
C# : SQLite 사용 ExecuteNonQuery(), ExecuteReader() 본문
1-1. SQLiteCommand, SQLiteDataReader를 이용해서 ExecuteReader() 실행
string szDBFileName = "데이터베이스파일이름.db";
string szExecutablePath = Path.GetDirectoryName(Application.ExecutablePath);
// 애플리케이션이 있는 위치 경로
string szDBFile = String.Format(@"{0}\{1}\{2}", szExecutablePath, "Database", szDBFileName);
// DB접속정보
string constring = string.Format("Data Source={0};Version=3;", szDBFile);
SQLiteConnection dbConnection = new SQLiteConnection();
dbConnection.ConnectionString = constring;
dbConnection.Open();
string sqlCom = "SELECT * FROM 테이블명";
SQLiteCommand scdCommand = new SQLiteCommand(sqlCom, dbConnection);
SQLiteDataReader reader = scdCommand.ExecuteReader();
while (reader.Read())
{
string Value1 = (string)reader["컬럼명1"];
string Value2 = (string)reader["컬럼명2"];
}
reader.Close();
dbConnection.Close();
우리는 SQLiteDataReader를 통해 데이터를 하나 하나씩 가져올 수 있습니다. 만약 데이터를 한꺼번에 가져오고 싶다면, SQLiteDataReader말고 SQLiteDataAdapter를 이용하세요. SQLiteDataAdapter는 DataSet, DataTable 등으로 가져올 수 있습니다.
1-2. SQLiteCommand, SQLiteDataAdapter를 이용해서 ExecuteReader() 실행
string szDBFileName = "데이터베이스파일이름.db";
string szExecutablePath = Path.GetDirectoryName(Application.ExecutablePath);
// 애플리케이션이 있는 위치 경로
string szDBFile = String.Format(@"{0}\{1}\{2}", szExecutablePath, "Database", szDBFileName);
// DB접속정보
string constring = string.Format("Data Source={0};Version=3;", szDBFile);
SQLiteConnection dbConnection = new SQLiteConnection();
dbConnection.ConnectionString = constring;
dbConnection.Open();
string sqlCom = "SELECT * FROM 테이블명";
SQLiteCommand scdCommand = new SQLiteCommand(sqlCom, dbConnection);
SQLiteDataAdapter sdAdapter = new SQLiteDataAdapter();
sdAdapter.SelectCommand = scdCommand;
DataTable dtOut = new DataTable();
sdAdapter.Fill(dtOut);
dbConnection.Close();
2. SQLiteCommand를 이용해서 ExcuteNonQuery() 실행
string szDBFileName = "데이터베이스파일이름.db";
string szExecutablePath = Path.GetDirectoryName(Application.ExecutablePath);
// 애플리케이션이 있는 위치 경로
string szDBFile = String.Format(@"{0}\{1}\{2}", szExecutablePath, "Database", szDBFileName);
// DB접속정보
string constring = string.Format("Data Source={0};Version=3;", szDBFile);
SQLiteConnection dbConnection = new SQLiteConnection();
dbConnection.ConnectionString = constring;
dbConnection.Open();
string sqlCom = "DELETE FROM 테이블명";
SQLiteCommand scdCommand = new SQLiteCommand(sqlCom, dbConnection);
scdCommand.ExecuteNonQuery();
dbConnection.Close();
'언어 > C#' 카테고리의 다른 글
C# : DataGridView Header Sort 막기 (0) | 2020.04.17 |
---|---|
C# : Windows Forms ComboBox 수정 못하게 막는 방법 (0) | 2020.04.08 |
C# : SQLite Create Database 데이터베이스 파일 생성하는 방법 (0) | 2020.04.03 |
C# : 공인인증서 NPKI 폴더 위치 찾기 및 콤보박스에 목록 보여주기 (2) | 2020.04.01 |
C# : DBConnection 이용해서 SQLite 연결 및 쿼리 실행 (0) | 2020.03.31 |
Comments