|
|
 |
下のエクセルVBAコードは実際に動作しているリナックスサーバーに搭載しているMySQL3データベースを操作しているプログラムから抜き出している。
自分のメモとして作っているので詳しい説明はしていない。
| 1 |
'-----DB_ServiceCodeに接続し、既登録データを検索する あらかじめシステムDNSとしてMySQL_POST2を設定している
Set cN = New ADODB.Connection
Set rS = New ADODB.Recordset
cNstr = "DSN=MySQL_POST2"
With cN
.ConnectionString = cNstr
.Open
End With
mySQLstr = "Select * From DB_ServiceCode ;"
Set rS = New ADODB.Recordset
rS.Open mySQLstr, cN, adOpenKeyset, adLockOptimistic
'' rS.Open mySQLstr, cN, adOpenDynamic, adLockOptimistic
' '-----既存データがあれば削除する。なぜかrS.RecordsetCountはうまく動作しないのでDoLoopでデータ数をカウントした
CnT = 0
If rS.EOF = False Then
Do Until rS.EOF = True
CnT = CnT + 1
rS.MoveNext
Loop
MsgBox CnT & "既存データを削除します"
' rS.Close
End If
rS.Close '----rSをいったん閉じる
|
| 2 |
'------データ数が一件以上あればすべて削除する
If CnT > 0 Then
Dim rS_Sakujo As ADODB.Recordset
Set rS_Sakujo = New ADODB.Recordset
mySQLstr = "Delete from DB_ServiceCode ;"
rS_Sakujo.Open mySQLstr, cN, adOpenKeyset, adLockOptimistic
'-----削除したのでClose処理は不要となる
' rS_Sakujo.Close
MsgBox "既存のデータを削除しました"
End If
|
| 3 |
'-----ワークシートDB_Kaigoのサービスコード表をインポートする
MsgBox "ワークシートDB_Kaigoのサービスコード表をインポートします"
Set rS = New ADODB.Recordset
Dim setCell As Range
Dim rowNo As Long
Worksheets("DB_Kaigo").Visible = True
Worksheets("DB_Kaigo").Select
Set setCell = Range("A1")
rowNo = 1
' Dim Tanisuu As Double
Dim TaniSuu As String '----空白セルがあるので文字列で入力する
Do Until setCell.Offset(rowNo, 0) = ""
If IsNull(setCell.Offset(rowNo, 1).Value) = True Then
'-----Null値を入力する
' TaniSuu = 0 '-----Nullを指定してもDoubleの場合 0で入力されるので
TaniSuu = Null
Else
TaniSuu = setCell.Offset(rowNo, 1).Value
End If
' mySQLstr = "INSERT INTO DB_ServiceCode(rowNo,ServiceCode,単位数,ServiceName)" _
' & "VALUES(" & rowNo & ",'" & setCell.Offset(rowNo, 0).Text & "'," _
' & TaniSuu & ",'" & setCell.Offset(rowNo, 2).Text & "'" & ");"
mySQLstr = "INSERT INTO DB_ServiceCode(rowNo,ServiceCode,単位数,ServiceName)" _
& "VALUES(" & rowNo & ",'" & setCell.Offset(rowNo, 0).Text & "'," _
& "'" & TaniSuu & "'" & ",'" & setCell.Offset(rowNo, 2).Text & "'" & ");"
rS.Open mySQLstr, cN, adOpenKeyset, adLockOptimistic
'-----InsertではClose処理不要
rowNo = rowNo + 1
Loop
MsgBox "サービスコードDBをインポートしました"
' rS.Close
cN.Close
Set rS = Nothing
Set cN = Nothing
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
|
 |
|
 |
|
|