-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigurationForm.cs
304 lines (273 loc) · 12.4 KB
/
ConfigurationForm.cs
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
using FinancialCrm.Models;
using FinancialCrm.ValidationRules;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FinancialCrm
{
public partial class ConfigurationForm : Form
{
public ConfigurationForm()
{
InitializeComponent();
}
bool IsChild;
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationForm"/> class.
/// Determines whether the form is displayed as a child form and adjusts the visibility of the header panel accordingly.
/// </summary>
/// <param name="IsChild">A boolean value indicating whether the form is a child form. If true, the header panel will be visible; otherwise, it will be hidden.</param>
public ConfigurationForm(bool IsChild)
{
InitializeComponent();
this.IsChild = IsChild;
headerPanel.Visible = !IsChild;
}
private void ConfigurationForm_Load(object sender, EventArgs e)
{
GetDatabaseValues();
}
#region Drag Event WinAPI
// Windows API çağrısı
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HTCAPTION = 0x2;
private void dragForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
}
#endregion
#region Database Settings
private void GetDatabaseValues()
{
// DataTable oluştur
var table = new DataTable();
table.Columns.Add("Key", typeof(string)); // Key sütunu
table.Columns.Add("Value", typeof(string)); // Value sütunu
// Key-Value çiftlerini tabloya ekle
table.Rows.Add("DataSource", Properties.Settings.Default.DataSource);
table.Rows.Add("InitialCatalog", Properties.Settings.Default.InitialCatalog);
table.Rows.Add("UserId", Properties.Settings.Default.UserId);
table.Rows.Add("Password", Properties.Settings.Default.Password);
table.Rows.Add("IntegratedSecurity", Properties.Settings.Default.IntegratedSecurity);
table.Rows.Add("TrustedServerCertificate", Properties.Settings.Default.TrustedServerCertificate);
// DataTable'ı DataGridView'e bağla
dataGridViewDatabase.DataSource = table;
// Opsiyonel: Sütun başlıklarını daha okunabilir hale getirmek için yeniden adlandırabilirsiniz
dataGridViewDatabase.Columns["Key"].HeaderText = "Ayar";
dataGridViewDatabase.Columns["Value"].HeaderText = "Değer";
}
private void dataGridViewDatabase_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 1 && dataGridViewDatabase.Rows[e.RowIndex].Cells[0].Value.ToString() == "Password")
{
e.Value = "•••••••"; // Şifreyi maskele
e.FormattingApplied = true;
}
}
private void TextBoxPlaceHolder_Enter(object sender, EventArgs e)
{
if (sender is TextBox textBox && textBox.Text == textBox.Tag.ToString())
{
textBox.Text = string.Empty;
}
}
private void TextBoxPlaceHolder_Leave(object sender, EventArgs e)
{
if (sender is TextBox textBox && string.IsNullOrEmpty(textBox.Text))
{
textBox.Text = textBox.Tag.ToString();
}
}
private void TextBoxPassword_Enter(object sender, EventArgs e)
{
if (sender is TextBox textBox)
{
if (textBox.Text == textBox.Tag.ToString())
{
textBox.Text = string.Empty;
}
textBox.UseSystemPasswordChar = true;
}
}
private void TextBoxPassword_Leave(object sender, EventArgs e)
{
if (sender is TextBox textBox)
{
if (string.IsNullOrEmpty(textBox.Text) || textBox.Text == textBox.Tag?.ToString())
{
textBox.Text = textBox.Tag?.ToString();
textBox.UseSystemPasswordChar = false;
}
else
{
textBox.UseSystemPasswordChar = true;
}
}
}
public void SetTextBoxPasswordValue(TextBox textBox, string value)
{
textBox.Text = value;
textBox.UseSystemPasswordChar = true;
}
private void LoadDatabaseValuesToTextBoxes()
{
// Tablodaki değerleri TextBox'lara yükle
foreach (DataGridViewRow row in dataGridViewDatabase.Rows)
{
string key = row.Cells["Key"].Value.ToString();
string value = row.Cells["Value"].Value?.ToString() ?? "";
switch (key)
{
case "DataSource":
txtDataSource.Text = value;
break;
case "InitialCatalog":
txtInitialCatalog.Text = value;
break;
case "UserId":
txtUserId.Text = value;
break;
case "Password":
SetTextBoxPasswordValue(txtPassword, value); // Şifreyi gizli tut
break;
case "IntegratedSecurity":
checkIntegratedSecurity.Checked = bool.Parse(value);
break;
case "TrustedServerCertificate":
checkTrustedServerCertificate.Checked = bool.Parse(value);
break;
}
}
}
private void SaveValuesToProperties()
{
// TextBox'lardaki değerleri Properties.Settings.Default'a kaydet
Properties.Settings.Default.DataSource = txtDataSource.Text;
Properties.Settings.Default.InitialCatalog = txtInitialCatalog.Text;
Properties.Settings.Default.UserId = txtUserId.Text;
Properties.Settings.Default.Password = txtPassword.Text;
Properties.Settings.Default.IntegratedSecurity = checkIntegratedSecurity.Checked;
Properties.Settings.Default.TrustedServerCertificate = checkTrustedServerCertificate.Checked;
// Değişiklikleri kaydet
Properties.Settings.Default.Save();
MessageBox.Show("Değerler başarıyla kaydedildi.\nUygulamayı yeniden başlatılacaktır.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void dataGridViewDatabase_CellClick(object sender, DataGridViewCellEventArgs e) => LoadDatabaseValuesToTextBoxes();
private void btnSaveProperties_Click(object sender, EventArgs e)
{
var values = new DBConnection
{
DataSource = txtDataSource.Text,
InitialCatalog = txtInitialCatalog.Text,
UserId = txtUserId.Text,
Password = txtPassword.Text,
IntegratedSecurity = checkIntegratedSecurity.Checked,
TrustedServerCertificate = checkTrustedServerCertificate.Checked,
};
var validator = new DBConnectionValidator();
var validationResult = validator.Validate(values);
if (validationResult.IsValid)
{
SaveValuesToProperties();
GetDatabaseValues();
lblChildInformation.Visible = IsChild;
lblChildInformation.ForeColor = Color.Green;
lblChildInformation.Text = "Bağlantı başarılı.\nAyarlar default olarak kayıt edildi.";
UpdateEntityFrameworkConnectionString();
Application.Restart();
}
else
{
btnInformation.Text = string.Join("\n", validationResult);
btnInformation.ForeColor = Color.Yellow;
btnInformation.IconColor = Color.Yellow;
lblChildInformation.Visible = IsChild;
lblChildInformation.Text = string.Join ("\n", validationResult);
lblChildInformation.ForeColor = Color.Red;
return;
}
}
private void UpdateEntityFrameworkConnectionString()
{
// Yeni connection string değerlerini oluştur
string newProviderConnectionString = $"data source={Properties.Settings.Default.DataSource};" +
$"initial catalog={Properties.Settings.Default.InitialCatalog};" +
$"integrated security={Properties.Settings.Default.IntegratedSecurity};" +
$"user id={Properties.Settings.Default.UserId};" +
$"password={Properties.Settings.Default.Password};" +
"trustservercertificate=True;MultipleActiveResultSets=True;App=EntityFramework";
// Metadata bilgilerini koruyarak yeni connection string'i birleştir
string newConnectionString = $"metadata=res://*/Models.FinancialCrmDb.csdl|res://*/Models.FinancialCrmDb.ssdl|res://*/Models.FinancialCrmDb.msl;" +
$"provider=System.Data.SqlClient;provider connection string=\"{newProviderConnectionString}\"";
// Yeni bağlantı dizgesinin doğruluğunu test et
if (!TestDatabaseConnection(newProviderConnectionString))
{
//MessageBox.Show("Veritabanı bağlantısı başarısız! Lütfen bilgilerinizi kontrol edin.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// app.config'i yükle
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Mevcut connection string'i güncelle
var connectionStringsSection = config.ConnectionStrings.ConnectionStrings["FinancialCrmDbEntities"];
if (connectionStringsSection != null)
{
connectionStringsSection.ConnectionString = newConnectionString;
}
else
{
// Eğer yoksa yeni bir bağlantı ekleyin
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings
{
Name = "FinancialCrmDbEntities",
ConnectionString = newConnectionString,
ProviderName = "System.Data.EntityClient"
});
}
// Değişiklikleri kaydet
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
//MessageBox.Show("Connection string başarıyla güncellendi.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private bool TestDatabaseConnection(string connectionString)
{
try
{
// Bağlantıyı kontrol et
using (var connection = new SqlConnection(connectionString))
{
connection.Open(); // Bağlantıyı açmayı dener
}
return true; // Başarılı
}
catch (Exception ex)
{
// Hata oluşursa false döner
//MessageBox.Show($"Veritabanı bağlantı testi başarısız: {ex.Message}", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
#endregion
private void btnFormClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}