프로그래밍/c# & VB 등
mdb 데이터베이스 자료 업데이트 하기
낼은어떻게
2018. 1. 31. 09:46
기존 mdb파일에 추가로 데이터를 집어 넣을 경우 Insert into query를 이용해서 추가할수 있습니다.
모조리 다 추가해서 중복되는 자료 삭제하는 방법이 있을 수 있구여
하지만 기존 자료의 마지막 index 를 구하고 그것보다 큰 자료만 Insert 하는 방법이 있습니당.
기존 자료 마지막 index 구하고 그것보다 큰 자료 즉 새로운 데이터만 추가하는 방법을 알아보도록 하겠습니다.
다음코드는 기존 자료에서 DataSet을 만들어 Datatable로 변환후에 마지막 데이터의 특정위치 값을 찾아는 소스입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | private static int LastRowNumber(string targetPath) { DataConn conn1 = new DataConn(); DataSet ds; string DB_path = targetPath; string sql = @"SELECT * from info"; ds = conn1.GetDataset(sql, DB_path); DataTable dt = ds.Tables[0]; DataRow lastRow = dt.Rows[dt.Rows.Count - 1]; int Lastinfo = Convert.ToInt32(lastRow[0]); return Lastinfo; } | 위cs |
다음 코드는 위에서 마지막 데이터의 index 를 이용하여 새로운 데이터가 있으면 추가하는 함수입니다.
추가할 데이터가 있는 위치는 db_path2
업데이트 되어야 할 데이터 위치는 db_path 입니다.
즉 db_path 에 db_path2 데이터 중에 새로운 데이터가 추가되는 것이지용.
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 32 | private void mdbInsert() { string db_path2 = System.IO.Path.Combine(sourcePath, fileName); int RowNuber = LastRowNumber(db_path); string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + db_path + ";Jet OLEDB:Database Password="; OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connStr); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = conn; conn.Open(); string sql = @"insert into simple select * from [" + db_path2 + "; PWD =].[simple] Where simple.info_index > " + RowNuber; cmd.CommandText = sql; cmd.ExecuteNonQuery(); conn.Close(); conn.Open(); string sql1 = @"insert into info select * from [" + db_path2 + "; PWD =].[info] Where info.index > " + RowNuber; cmd.CommandText = sql1; cmd.ExecuteNonQuery(); conn.Close(); } | cs |
위 두가지 함수를 이용해서 업데이트 코드를 완성하면 될것입니다.