بعد سنوات طوال مع الجافا قررت التجديد فانخت الرحال في تقنيات مايكروسوفت و c# تحديدا لقربها من الجافا
حقيقة مايكروسوفت ابدعت ابداع كبير في فيجول ستوديو 2008 فلم تبق شئ للمبرمج ليعمله … خطوات بسيطه ويكون التطبيق جاهز
يمكنكم تحميل نسخة تجريبية لـ VS.net 2008 لمده 90 يوم من هنا (حجم الملف تقريبا 3 قيقا )
قمت بعمل تطبيق اضاف لي الكثير حيث لم يكن بالتطبيق البسيط بل كان يحوي عدة اجزاء مهمه فالبيانات يتم جلبها من قاعدة بيانات اوراكل و Microsoft Active Directory لقراءة معلومات المستخدمين وتقارير ديناميكية حسب رغبة المستخدم … كل هذا جعل من التجربة رائعه وممتعه لكثرة التعقيد والتجارب وخصوصا على مبتدئ مثلي فتعلمت الكثير في ايام قليلة … مع خالص التحية و التقدير للعم قوقل على مساعداته الجباره
كانت هناك مشاكل استعصت علي ووجدت حلول لها بحمد الله واضعها هنا للفائده
1- الاتصال باوراكل
كما هو معروف ان معلومات الاتصال بقواعد البيانات في اوراكل تخزن في الملف tnsnames.ora لكن مالعمل ان كانت معلومات قاعدة البيانات التي يتصل بها برنامجك غير معده عند المستخدم؟ طبعا انت لاتريد ان تشغل المستخدمين باعدادات معقده لاسيما ان بعضهم ليس عنده خبره كافيه فاليك الحل
معلومات الاتصال المخزنه في tnsnames.ora هي بهذه الصيغه
myserver.server.com = (DESCRIPTION = (ADDRESS = (PROTOCOL= TCP)(Host=
myserver.server.com)(Port= yourPort#))(CONNECT_DATA = (SID = yourSID)) )
كل ماعليك هو وضع نفس الصيغه في الكود الخاص بالاتصال بقاعده البيانات ليصبح كالتالي
1
| private static string CONNECTION_STRING = "User Id=myUserID;Password=myPassword;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=myserver.server.com)(PORT=yourPort#))(CONNECT_DATA=(SID=yourSID)));"; |
وهذا كود كامل يشرح طريقة الاتصال
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
| using System.Data.OracleClient;   static private string GetConnectionString()
{
return "User Id=myUserID;Password=myPassword;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=myserver.server.com)(PORT=yourPort#))(CONNECT_DATA=(SID=yourSID)));";
}
// This will open the connection and query the database
static private void ConnectAndQuery()
{
string connectionString = GetConnectionString();
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
OracleCommand command = connection.CreateCommand();
string sql = "SELECT * FROM MYTABLE";
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string myField = (string)reader["MYFIELD"];
Console.WriteLine(myField);
}
}
} |
ايضا يمكن وضع هذا المتغير في الملف app.config لتتمكن من الوصول اليه في اي وقت عن طريق
1
| string connectionSting = ConfigurationManager.ConnectionStrings["Your_var_Name"].ConnectionString; |
2- تحديث جدول البيانات من نافذه اخرى
قمت بانشاء نافذه تحوي DataGridView فيها بعض الاسماء وعندما يريد المستخدم اضافة اسم جديد للقائمة تظهر نافذه جديده ومن خلالها يبحث عن المستخدم في دليل الشركه (MS Active Directory)

كان الاشكال كيف اضيف الاسم الجديد الى DataGridView في الفورم الاصلي من الفورم الحالي بدون ان يشعر المستخدم ؟ كان هناك عده حلول لكن الحل الذي عمل معي بشكل رائع هو ان اضيف حدث في الفورم الاصلي ليستمع الى اغلاق الفورم المفتوح وعندها يقوم باجراء التحديث وكان الكود بهذا الشكل
1
2
3
4
5
6
7
| Form2 childForm = new Form2();
childForm.FormClosed += newFormClosedEventHandler(childForm_FormClosed);
childForm.Show();
void childForm_FormClosed(object sender,FormClosedEventArgs e)
{
//Your logic to refresh the DataGridView goes here
} |
وهذا الكود الخاص بقراءة الاسم من مايكروسوفت(ActiveDirectory)
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
| public static string GetFullName(string strLogin)
{
strLogin = "aramco\\" + strLogin;
string str = "";
string strDomain;
string strName;
// Parse the string to check if domain name is present.
int idx = strLogin.IndexOf('\\');
if (idx == -1)
{
idx = strLogin.IndexOf('@');
}
if (idx != -1)
{
strDomain = strLogin.Substring(0, idx);
strName = strLogin.Substring(idx + 1);
}
else
{
strDomain = Environment.MachineName;
strName = strLogin;
}
DirectoryEntry obDirEntry = null;
try
{
obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
object obVal = coll["FullName"].Value;
str = obVal.ToString();
}
catch (Exception ex)
{
str = ex.Message;
}
return str;
} |
3- تحديث التقارير
من ضمن التطبيق كان هناك جزء خاص بالتقارير وكان انشاء التقارير سهلا وبسيطا حيث قام الفيجول ستوديو بعمل جميع المهام وربط التقارير بجداول البيانات وانشاء الاكواد تلقائيا بضغطات ازرار بسيطه … لكن انا لا اريد تقريرا ثابتا لكن اريد تقرير حسب رغبة المستخدم فهو الذي يحدد مثلا المدة الزمنية للمعلومات التي يريدها فما الحل؟
الحل كان البحث عن الدوال التي توفر المعلومه للتقرير بعدها ياتي دور انشاء الاستعلام من جديد وتحديث جدول البياناتTableAdapter المربوطه في التقرير
1
2
3
| YourTableAdapter._commandCollection = this.YourTableAdapter.CommandCollection;
this.YourTableAdapter._commandCollection[0].CommandText = "Select * From.......";<p></p>
<p></p> |
_commandCollection تلقائيا هي private فغيرها الى public لتستطيع الوصول اليها
بعدها قم باعادة ربطها من جديد بالتقرير
1
2
3
4
5
| Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
reportDataSource1.Name = "Name_OF_Your_Data_Source";
reportDataSource1.Value = this.YourBindingSource;
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.RefreshReport(); |
4- عامود الوقت في جدول البيانات
في جدول البيانات وتسهيلا على المستخدم يفضل ان تجعله يختار الوقت والتاريخ عن طريق dateTimePicker حتى تكون الفورمات متناسقه والاختيار بلا شك اسهل من الكتابة اليدويه للمستخدم
للحصول على هذه اضف هذا الكلاس في مشروعك ومن ثم اعمل له Build وبعدها ستجد نوع من الاعمده اسمه microsoftDateTimePicker
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
| using System;
using System.Collections.Generic;
using System.Text;
namespace YourAppNameSpace
{
using System;
using System.Windows.Forms;
public class MicrosoftDateTimePicker : DataGridViewColumn
{
public MicrosoftDateTimePicker()
: base(new CalendarCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CalendarCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CalendarCell)))
{
throw new InvalidCastException("Must be a CalendarCell");
}
base.CellTemplate = value;
}
}
}
public class CalendarCell : DataGridViewTextBoxCell
{
public CalendarCell()
: base()
{
// Use the short date format.
this.Style.Format = "d";
}
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
CalendarEditingControl ctl =
DataGridView.EditingControl as CalendarEditingControl;
ctl.Value = (DateTime)this.Value;
}
public override Type EditType
{
get
{
// Return the type of the editing contol that CalendarCell uses.
return typeof(CalendarEditingControl);
}
}
public override Type ValueType
{
get
{
// Return the type of the value that CalendarCell contains.
return typeof(DateTime);
}
}
public override object DefaultNewRowValue
{
get
{
// Use the current date and time as the default value.
return DateTime.Now;
}
}
}
class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
public CalendarEditingControl()
{
this.Format = DateTimePickerFormat.Short;
}
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get
{
return this.Value.ToShortDateString();
}
set
{
if (value is String)
{
this.Value = DateTime.Parse((String)value);
}
}
}
// Implements the
// IDataGridViewEditingControl.GetEditingControlFormattedValue method.
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
}
// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}
// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}
// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
protected override void OnValueChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell
// have changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
} |