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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
| **************************************************************************
** VFP de MySQL kullanimina ornek **
** (c)1993-2007 Yapay Zeka Ltd. **
** Istediginiz gibi istediginiz uygulamalarinizda kullanabilirsiniz. **
** Son Guncelleme : 24/02/2007 **
**************************************************************************
** Bu ornekte, mySQL de tanimlanmis 'Deneme' isimli tabloda ad,soyad, **
** yas,maas bilgileri bulunmaktadir. Siz dilediginiz gibi degisiklik **
** yapabilirsiniz. **
**************************************************************************
Clear All
Public oForm
oForm = CreateObject('MyForm')
oForm.Show()
Return
******************************************************************
Define Class MyForm as Form
******************************************************************
Height = 400
Width = 650
Caption = 'MySQL Sample'
Name = 'MyForm'
AutoCenter = .t.
tnHandle = 0
tcTable = 'Deneme'
Add Object lblServer as Label With ;
Top = 375, ;
Left = 4, ;
Caption = 'Server'
Add Object txtServer as TextBox With ;
Top = 372, ;
Left = 52, ;
Value = 'localhost', ;
SelectOnEntry = .t.
Add Object lblDatabase as Label With ;
Top = 375, ;
Left = 160, ;
Caption = 'Database'
Add Object txtDatabase as TextBox With ;
Top = 372, ;
Left = 223
Add Object lblUserName as Label With ;
Top = 375, ;
Left = 330, ;
Caption = 'Kullanıcı Adı'
Add Object txtUserName as TextBox With ;
Top = 372, ;
Left = 403
Add Object lblPassword as Label With ;
Top = 375, ;
Left = 511, ;
Caption = 'Şifre'
Add Object txtPassword as TextBox With ;
Top = 372, ;
Left = 545, ;
PasswordChar = '*'
Add Object lblAd as Label With ;
Top = 15, ;
Left = 9, ;
Caption = 'Adı'
Add Object txtAd as myTextBox With ;
Top = 12, ;
Left = 30
Add Object lblSoyad as Label With ;
Top = 15, ;
Left = 135, ;
Caption = 'Soyadı'
Add Object txtSoyad as myTextBox With ;
Top = 12, ;
Left = 177
Add Object lblYas as Label With ;
Top = 15, ;
Left = 284, ;
Caption = 'Yaşı'
Add Object txtYas as myTextBox With ;
Top = 12, ;
Left = 316, ;
Width = 69
Add Object lblMaas as Label With ;
Top = 15, ;
Left = 392, ;
Caption = 'Maaşı'
Add Object txtMaas as myTextBox With ;
Top = 12, ;
Left = 429, ;
Value = 0
Add Object grdListe as myGrid
Add Object shpBorder1 as Shape with ;
Top = 363, ;
Left = 0, ;
Height = 3, ;
Width = 650, ;
SpecialEffect = 0
Add Object shpBorder2 as Shape with ;
Top = 0, ;
Left = 537, ;
Height = 364, ;
Width = 3, ;
SpecialEffect = 0
Add Object cmdConnect as CommandButton With ;
Top = 12, ;
Left = 552, ;
Height = 27, ;
Width = 84, ;
Caption = "Connect"
Add Object cmdDisconnect as CommandButton With ;
Top = 12, ;
Left = 552, ;
Height = 27, ;
Width = 84, ;
Caption = "Disconnect", ;
Visible = .f.
Add Object cmdListe as CommandButton With ;
Top = 46, ;
Left = 552, ;
Height = 27, ;
Width = 84, ;
Caption = 'Listele', ;
Enabled = .f.
Add Object cmdNew as CommandButton With ;
Top = 80, ;
Left = 552, ;
Height = 27, ;
Width = 84, ;
Caption = 'Yeni Kayıt', ;
Enabled = .f.
Add Object cmdSave as CommandButton With ;
Top = 80, ;
Left = 552, ;
Height = 27, ;
Width = 84, ;
Caption = 'Sakla', ;
Visible = .f.
Add Object cmdEdit as CommandButton With ;
Top = 114, ;
Left = 552, ;
Height = 27, ;
Width = 84, ;
Caption = 'Düzelt', ;
Enabled = .f.
Add Object cmdUpdate as CommandButton With ;
Top = 114, ;
Left = 552, ;
Height = 27, ;
Width = 84, ;
Caption = 'Sakla', ;
Visible = .f.
Add Object cmdDelete as CommandButton With ;
Top = 148, ;
Left = 552, ;
Height = 27, ;
Width = 84, ;
Caption = 'Sil', ;
Enabled = .f.
Add Object cmdClose as CommandButton With ;
Top = 182, ;
Left = 552, ;
Height = 27, ;
Width = 84, ;
Caption = 'Kapat'
Add Object cmdSaveServerInfo as CommandButton With ;
Top = 300, ;
Left = 552, ;
Height = 55, ;
Width = 84, ;
Caption = "Server Bilgilerini Kaydet", ;
WordWrap = .t.
Procedure Init
If File('ServerInfo.ini')
m.lcServerInfo = FileToStr('ServerInfo.ini')
With thisform
.txtServer.Value = Mline(lcServerInfo, 1)
.txtDatabase.Value = Mline(lcServerInfo, 2)
.txtUserName.Value = Mline(lcServerInfo, 3)
.txtPassword.Value = Mline(lcServerInfo, 4)
EndWith
EndIf
EndProc
Procedure cmdConnect.Click
mySQLConnect()
EndProc
Procedure cmdDisconnect.Click
mySQLDisconnect()
EndProc
Procedure cmdListe.Click
mySQLList()
EndProc
Procedure cmdNew.Click
mySQLNew()
EndProc
Procedure cmdSave.Click
mySQLSave()
EndProc
Procedure cmdEdit.Click
mySQLEdit()
EndProc
Procedure cmdUpdate.Click
mySQLUpdate()
EndProc
Procedure cmdDelete.Click
mySQLDelete()
EndProc
Procedure cmdClose.Click
thisform.Release
EndProc
Procedure cmdSaveServerInfo.Click
m.lcServerInfo = Alltrim(thisform.txtServer.Value) + Chr(13) +;
Alltrim(thisform.txtDatabase.Value) + Chr(13) +;
Alltrim(thisform.txtUserName.Value) + Chr(13) +;
Alltrim(thisform.txtPassword.Value)
StrToFile(lcServerInfo,'ServerInfo.ini',0)
EndProc
EndDefine
******************************************************************
Define Class myGrid as Grid
******************************************************************
Top = 45
Left = 20
Height = 310
Width = 500
ColumnCount = 4
DeleteMark = .f.
RecordMark = .f.
ScrollBars = 2
GridLineColor = Rgb(192,192,192)
Procedure Init
With this
.Column1.Header1.Caption = 'Adı'
.Column1.Width = 150
.Column2.Header1.Caption = 'Soyadı'
.Column2.Width = 150
.Column3.Header1.Caption = 'Yaşı'
.Column4.Header1.Caption = 'Maaşı'
.Column4.Width = 100
EndWith
EndProc
Procedure AfterRowColChange
Lparameters nColIndex
DoDefault(nColIndex)
With oForm
.txtAd.Value = Ad
.txtSoyad.Value = Soyad
.txtYas.Value = Yas
.txtMaas.Value = Maas
EndWith
EndProc
EndDefine
******************************************************************
Define Class myTextBox as TextBox
******************************************************************
Enabled = .f.
DisabledForeColor = Rgb(0,0,0)
SelectOnEntry = .t.
EndDefine
******************************************************************
Procedure mySQLConnect
******************************************************************
With oForm
m.lcServer = Alltrim(.txtServer.Value)
m.lcDatabase = Alltrim(.txtDatabase.Value)
m.lcUserName = Alltrim(.txtUserName.Value)
m.lcPassword = Alltrim(.txtPassword.Value)
&& Connection bilgisi.
lcStringConn="Driver={MySQL ODBC 3.51 Driver};Port=3306"+;
";Server=" + lcServer +;
";Database=" + lcDatabase +;
";Uid=" + lcUserName +;
";Pwd=" + lcPassWord
SQLSetProp(0,"DispLogin",3)
.tnHandle = SQLStringConnect(lcStringConn)
MessageBox(IIF(.tnHandle < 0, 'Baglanti saglanamadi', 'Baglandi' ), ;
64,'Baglanti durumu',1000)
.Caption = .Caption + ' - Baglanti Aktif'
SwitchBtn(.t.)
EndWith
EndProc
******************************************************************
Procedure mySQLDisconnect
******************************************************************
With oForm
If .tnHandle > 0
SQLDisconnect(.tnHandle)
MessageBox('Baglanti kesildi',64,'Baglanti durumu',1000)
.Caption = 'MySQL Sample'
EndIf
SwitchBtn(.f.)
EndWith
EndProc
******************************************************************
Procedure mySQLList
******************************************************************
With oForm
If .tnHandle > 0
&& Listeleme icin ornek :
&& mySQLExec(tnHandle, "select * from MySQLDB.MySQLTable", lcVFPTableName)
mySQLExec(.tnHandle, ;
"select ad,soyad,yas,maas from " + Alltrim(.txtDatabase.Value) + ;
"." + Alltrim(.tcTable), 'liste')
.grdListe.RecordSource = 'liste'
EndIf
With .grdListe
.Column1.Width = 150
.Column2.Width = 150
.Column3.Width = 75
.Column4.Width = 100
EndWith
EndWith
EndProc
******************************************************************
Procedure mySQLNew
******************************************************************
With oForm
.txtAd.Enabled = .t.
.txtAd.Value = ''
.txtSoyad.Enabled = .t.
.txtSoyad.Value = ''
.txtYas.Enabled = .t.
.txtYas.Value = ''
.txtMaas.Enabled = .t.
.txtMaas.Value = 0
.cmdNew.Visible = .f.
.cmdSave.Visible = .t.
.grdListe.Enabled = .f.
.txtAd.SetFocus()
EndWith
EndProc
******************************************************************
Procedure mySQLSave
******************************************************************
With oForm
If .tnHandle > 0
&& Kayit icin ornek :
&& mySQLExec(tnHandle,
&& "insert into MySQLDB.MySQLTable ('ad','soyad') Values ('Ali','Yuzer')",
&& lcVFPTableName)
mySQLExec(.tnHandle, ;
"insert into " + Alltrim(.txtDatabase.Value) + "." + Alltrim(.tcTable) +;
" (`ad`,`soyad`,`yas`,`maas`) Values ('" + Alltrim(.txtAd.Value) + "','" + ;
Alltrim(.txtSoyad.Value) + "','" + Alltrim(.txtYas.Value) + "','" +;
Transform(.txtMaas.Value) + "')", +;
'crsDummy')
MessageBox('Kayıt işlemi'+Chr(13)+'başarıyla tamamlandı', 64, 'MySQL Sample', 2000)
EndIf
.txtAd.Enabled = .f.
.txtSoyad.Enabled = .f.
.txtYas.Enabled = .f.
.txtMaas.Enabled = .f.
.cmdNew.Visible = .t.
.cmdSave.Visible = .f.
.cmdListe.Click()
.grdListe.Enabled = .t.
.grdListe.SetFocus()
EndWith
EndProc
******************************************************************
Procedure mySQLEdit
******************************************************************
With oForm
&& Bur örnekte sadece Yas veya Maas bilgisinin güncellenebilecegi
&& kabul edilmistir. Siz istediginiz degisiklikleri gerektigi gibi
&& yapabilirsiniz.
.txtYas.Enabled = .t.
.txtMaas.Enabled = .t.
.cmdEdit.Visible = .f.
.cmdUpdate.Visible = .t.
.grdListe.Enabled = .f.
.txtYas.SetFocus()
EndWith
EndProc
******************************************************************
Procedure mySQLUpdate
******************************************************************
With oForm
If .tnHandle > 0
&& Guncelleme icin ornek :
&& mySQLExec(tnHandle,
&& "Update MySQLDB.MySQLTable SET maas='150' Where maas='100'",
&& lcVFPTableName)
mySQLExec(.tnHandle, ;
"Update " + Alltrim(.txtDatabase.Value) + "." + Alltrim(.tcTable) +;
" SET yas='" + Alltrim(.txtYas.Value) + "', maas='" + ;
Transform(.txtMaas.Value) + "' where ad = '" +;
Alltrim(.txtAd.Value) + "' and soyad = '" + Alltrim(.txtSoyad.Value) +;
"'", "crsUpdate")
MessageBox('Güncelleme işlemi'+Chr(13)+'başarıyla tamamlandı', 64, 'MySQL Sample', 2000)
EndIf
.txtYas.Enabled = .f.
.txtMaas.Enabled = .f.
.cmdEdit.Visible = .t.
.cmdUpdate.Visible = .f.
.cmdListe.Click()
.grdListe.Enabled = .t.
.grdListe.SetFocus()
EndWith
EndProc
******************************************************************
Procedure mySQLDelete
******************************************************************
With oForm
If .tnHandle > 0
m.lnResult = MessageBox('Silmek istediğinize'+Chr(13)+'eminmisiniz?', ;
4+32+256, 'MySQL Sample')
If lnResult = 6
&& Silme icin ornek :
&& mySQLExec(tnHandle,
&& "Delete From MySQLDB.MySQLTable Where Ad='Ali'",
&& lcVFPTableName)
mySQLExec(.tnHandle, ;
"Delete From " + Alltrim(.txtDatabase.Value) + "." + Alltrim(.tcTable) +;
" Where ad='" + Alltrim(.txtAd.Value) + "' and soyad='" +;
Alltrim(.txtSoyad.Value) + "'", "crsDelete")
MessageBox('Silme işlemi'+Chr(13)+'başarıyla tamamlandı', 64, 'MySQL Sample', 2000)
.cmdListe.Click()
.grdListe.SetFocus()
EndIf
EndIf
EndWith
EndProc
******************************************************************
Function mySQLExec
Lparameters tnHandle, tcSQL, tcCursorName
******************************************************************
tcCursorName = iif(empty(tcCursorName),'',tcCursorName)
If SQLExec(tnHandle,tcSQL,tcCursorName) < 0
Do errHand with tcSQL
EndIf
EndFunc
******************************************************************
Function errHand
Lparameters tcSQL
******************************************************************
lcError=tcSQL+chr(13)
Aerror(arrCheck)
For ix=1 to 7
lcError = lcError+trans( arrCheck [ix])+ chr(13)
Endfor
Messagebox(lcError,0,'Error def.')
EndFunc
******************************************************************
Procedure SwitchBtn
Lparameters tlState
******************************************************************
With oForm
.cmdDisconnect.Visible = tlState
.cmdConnect.Visible = !tlState
.cmdListe.Enabled = tlState
.cmdNew.Enabled = tlState
.cmdEdit.Enabled = tlState
.cmdDelete.Enabled = tlState
EndWith
EndProc |