16-11-16, 10:48 PM
اعذرني لم اراعي انك تستخدم #C
على كل حال هذا الكود بـ #C
سنغير المديول الى Static Class حيث في #C لا يوجد Module
اضف كلاس للمشروع و سميه Connect و هذا كوده
و هذا كود النموذج
و بالنسبة لرسالة الخطأ لو تكون بالانكليزي يكون احسن كوني لا افقه الفرنسية
بكل الاحوال جرب هذا و بعدها شوف اذا استمرت رسالة الخطأ بالظهور اخبرني (انا لم تظهر معي الرسالة)
على كل حال هذا الكود بـ #C
سنغير المديول الى Static Class حيث في #C لا يوجد Module
اضف كلاس للمشروع و سميه Connect و هذا كوده
PHP كود :
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.OleDb;
using System Windows.Forms;
static class Connect
{
public static string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\TestDb.accdb";
public static OleDbConnection Con = new OleDbConnection(ConnectionString);
public static void CloseConnect()
{
if (Con.State == ConnectionState.Open)
{
Con.Close();
}
}
public static void OpenConnect()
{
if (Con.State == ConnectionState.Closed)
{
if (Con.ConnectionString == null)
Con.ConnectionString = ConnectionString;
Con.Open();
}
}
public static DataTable SelectWithString(string TblNmae, string[] Fileds, string CondFiled, string Match)
{
if (Fileds.Length == 0)
{
MessageBox.Show("تاكد من ملئ مصفوفة الحقول");
return null;
}
string SelectStatement = "Select ";
string SetFileds = "";
foreach (string str in Fileds)
{
SetFileds += str + ",";
}
if (SetFileds.EndsWith(","))
SetFileds = SetFileds.Remove(SetFileds.Length - 1, 1);
SelectStatement += SetFileds + " From " + TblNmae + " Where " + CondFiled + " Like '" + Match + "' ";
if (Con.ConnectionString == null)
Con.ConnectionString = ConnectionString;
OleDbCommand Cmd = new OleDbCommand(SelectStatement, Con);
DataTable Dt = new DataTable();
Dt.Clear();
OleDbDataAdapter Da = new OleDbDataAdapter(Cmd);
if (Con.State == ConnectionState.Closed)
Con.Open();
Da.Fill(Dt);
Cmd.Dispose();
return Dt;
}
public static DataTable SelectWithInteger(string TblNmae, string[] Fileds, string CondFiled, int ID)
{
if (Fileds.Length == 0)
{
MessageBox.Show("تاكد من ملئ مصفوفة الحقول");
return null;
}
string SelectStatement = "Select ";
string SetFileds = "";
foreach (string str in Fileds)
{
SetFileds += str + ",";
}
if (SetFileds.EndsWith(","))
SetFileds = SetFileds.Remove(SetFileds.Length - 1, 1);
SelectStatement += SetFileds + " From " + TblNmae + " Where " + CondFiled + " Like '" + ID + "' ";
if (Con.ConnectionString == null)
Con.ConnectionString = ConnectionString;
OleDbCommand Cmd = new OleDbCommand(SelectStatement, Con);
DataTable Dt = new DataTable();
Dt.Clear();
OleDbDataAdapter Da = new OleDbDataAdapter(Cmd);
if (Con.State == ConnectionState.Closed)
Con.Open();
Da.Fill(Dt);
Cmd.Dispose();
return Dt;
}
public static DataTable SelectFTableNormal(string TblNmae, string[] Fileds)
{
if (Fileds.Length == 0)
{
MessageBox.Show("تاكد من ملئ مصفوفة الحقول");
return null;
}
string SelectStatement = "Select ";
string SetFileds = "";
foreach (string str in Fileds)
{
SetFileds += str + ",";
}
if (SetFileds.EndsWith(","))
SetFileds = SetFileds.Remove(SetFileds.Length - 1, 1);
SelectStatement += SetFileds + " From " + TblNmae;
if (Con.ConnectionString == null)
Con.ConnectionString = ConnectionString;
OleDbCommand Cmd = new OleDbCommand(SelectStatement, Con);
DataTable Dt = new DataTable();
Dt.Clear();
OleDbDataAdapter Da = new OleDbDataAdapter(Cmd);
if (Con.State == ConnectionState.Closed)
Con.Open();
Da.Fill(Dt);
Cmd.Dispose();
return Dt;
}
public static string GetFiled(string ReturnFiled, string Tbl, string CondFiled, string Id)
{
string serch = "";
serch = "Select " + ReturnFiled + " From " + Tbl + " Where " + CondFiled + " Like '" + Id + "' ";
OleDbCommand Cmd = new OleDbCommand(serch, Con);
string ReturnResult = "";
OpenConnect();
OleDbDataReader red = Cmd.ExecuteReader();
while (red.Read())
{
if (red.HasRows)
{
if ((red[ReturnFiled] != null) | red[ReturnFiled].ToString().Length > 0)
{
ReturnResult = red[ReturnFiled].ToString();
}
else
{
ReturnResult = null;
}
}
}
red.Close();
Cmd.Dispose();
CloseConnect();
return ReturnResult;
}
}
PHP كود :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class TForm : Form
{
public TForm()
{
InitializeComponent();
}
DataTable TB_Content,TB_F ,TB_M = new DataTable();
string[] Fileds = null;
private void Cmb_F_SelectedIndexChanged(object sender, EventArgs e)
{
Fileds = new string[] {"Cont_ID","Cont_Title"};
TB_Content = Connect.SelectWithInteger("TB_Content", Fileds, "MF_ID", int.Parse(Cmb_F.SelectedValue.ToString() ));
ListContent.DataSource = TB_Content;
ListContent.DisplayMember = "Cont_Title";
ListContent.ValueMember = "Cont_ID";
}
private void ListContent_SelectedIndexChanged(object sender, EventArgs e)
{
if (!(ListContent.SelectedValue is DataRowView))
{
richTextBox1.Text =Connect . GetFiled("Content", "TB_Content", "Cont_ID", ListContent.SelectedValue.ToString());
}
}
private void TForm_Load(object sender, EventArgs e)
{
Fileds = new string[] {"M_ID","M_Name"};
TB_M = Connect.SelectFTableNormal("TB_M", Fileds);
Cmb_M.DataSource = TB_M;
Cmb_M.DisplayMember = "M_Name";
}
private void Cmb_M_SelectedIndexChanged(object sender, EventArgs e)
{
Fileds = new string[] {"MF_ID", "F_Nmae"};
TB_F = Connect.SelectWithString("TB_MF", Fileds, "M_Nmae", Cmb_M.Text);
Cmb_F.DataSource = TB_F;
Cmb_F.DisplayMember = "F_Nmae";
Cmb_F.ValueMember = "MF_ID";
}
}
}
بكل الاحوال جرب هذا و بعدها شوف اذا استمرت رسالة الخطأ بالظهور اخبرني (انا لم تظهر معي الرسالة)
اللهم لك الحمد كما ينبغي لجلال وجهك و عظيم سلطانك
في حل و ترحال


