using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient;// جهت استفاده و اتصال به بانک اطلاعات sql using System.Windows.Forms; using System.Data; using System.IO; using System; using System.Security.Cryptography; namespace Connection { public class Connection { public Connection() { string a = null, b = null, c = null, d = null, e = null, f = null; { if (File.Exists("Connection\\maysam.txt")) { //باز کردن فایل متنی حاوی اطلاعات تنظیمات دیتابیس using (StreamReader streamReader = File.OpenText("Connection\\maysam.txt")) { //فراخوانی سطر اول فایل متنی برای آدرس سرور a = streamReader.ReadLine(); //فراخوانی سطر دوم نوع احراز هویت b = streamReader.ReadLine(); if (b != "True") { f = "True"; } //فراخوانی سطر سوم فایل متنی برای نام دیتا بیس c = streamReader.ReadLine(); //فراخوانی سطر چهارم فایل متنی برای نام کاربری d = streamReader.ReadLine(); //فراخوانی سطر پنجم فایل متنی برای رمز عبور e = streamReader.ReadLine(); } } else { MessageBox.Show("فایل تنظیمات آدرس دیتابیس موجود نیست"); } } } } } /* این دستورات رو چطور جای گذین کنم تو این کلاس که آدرس سرور رو بگم از a بخونه نوع تنظیمات رو از اگر لوکال بود از b و اگر شبکه بود از f نام دیتا بیس رو از c و نام کاربری رو از d و رمز عبور رو از e //--------------------------------------------------------------- static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool"); // در صورتیکه به صورت شبکه اتصال دارید از رشته اتصال زیر استفاده نمایید // public string ConnectionString ="Data Source=172.17.1.147;Initial Catalog=tell;User ID=admin;Password=2261"; // در صورتیکه به صورتی لوکال و خارج از شبکه اتصال دارید از رشته ااتصال زیر استفاده نمایید public string ConnectionString = "Data Source=.;Initial Catalog=Hoze_Meqdad;Integrated Security=True"; DataGridView dt = new DataGridView(); SqlCommand command = new SqlCommand(); SqlConnection connection = new SqlConnection(); internal void BindDataTable(string SelectQuery, DataTable dt) { connection.Close(); connection.ConnectionString = ConnectionString; connection.Open(); SqlDataAdapter adaptor = new SqlDataAdapter(SelectQuery, connection); adaptor.Fill(dt); connection.Close(); } public string Encrypt(string originalString) { if (String.IsNullOrEmpty(originalString)) { throw new ArgumentNullException ("The string which needs to be encrypted can not be null."); } DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write); StreamWriter writer = new StreamWriter(cryptoStream); writer.Write(originalString); writer.Flush(); cryptoStream.FlushFinalBlock(); writer.Flush(); return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length); } public string Decrypt(string cryptedString) { if (String.IsNullOrEmpty(cryptedString)) { throw new ArgumentNullException ("The string which needs to be decrypted can not be null."); } DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream (Convert.FromBase64String(cryptedString)); CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read); StreamReader reader = new StreamReader(cryptoStream); return reader.ReadToEnd(); } public string ExecuteNonQuery(string CommandText,int par) { try { connection.Close(); connection.ConnectionString = ConnectionString; connection.Open(); command.CommandText = CommandText; command.Connection = connection; command.ExecuteNonQuery(); command.Dispose(); connection.Close(); if(par==1) MessageBox.Show("عمليات با موفقيت انجام گرديد"); return "1"; } catch { MessageBox.Show("خطایی در ثبت به وجود آمده لطفا داده های خود را بررسی نمایید"); return "0"; } } public void View(string SelectQuery, DataGridView dt) { try { DataTable dtt = new DataTable(); connection.Close(); connection.ConnectionString = ConnectionString; connection.Open(); SqlDataAdapter adaptor = new SqlDataAdapter(SelectQuery, connection); adaptor.Fill(dtt); dt.DataSource = dtt; connection.Close(); } catch (Exception e) { MessageBox.Show(e.Message.ToString(), "خطای خواندن اطلاعات از بانک اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public void BindComboBox(string Query, System.Windows.Forms.ComboBox cb) { try { connection.Close(); DataGridView dt = new DataGridView(); connection.ConnectionString = ConnectionString; connection.Open(); SqlDataAdapter adaptor = new SqlDataAdapter(Query, connection); DataTable dtt = new DataTable(); adaptor.Fill(dtt); cb.DataSource = dtt; cb.DisplayMember = dtt.Columns[0].ColumnName; connection.Close(); } catch { } } public void Load_Combo(string Query, System.Windows.Forms.ComboBox cb) { try { cb.Items.Clear(); DataGridView dt = new DataGridView(); connection.ConnectionString = ConnectionString; connection.Open(); SqlDataAdapter adaptor = new SqlDataAdapter(Query, connection); DataTable dtt = new DataTable(); adaptor.Fill(dtt); for (int i = 0; i < dtt.Rows.Count; i++) cb.Items.Add(dtt.Rows[i][0].ToString()); connection.Close(); } catch { } } public void Load_ContexMneuStrip(string Query,ToolStripComboBox cb) { cb.Items.Clear(); DataGridView dt = new DataGridView(); connection.ConnectionString = ConnectionString; connection.Open(); SqlDataAdapter adaptor = new SqlDataAdapter(Query, connection); DataTable dtt = new DataTable(); adaptor.Fill(dtt); for (int i = 0; i < dtt.Rows.Count; i++) cb.Items.Add(dtt.Rows[i][0].ToString()); connection.Close(); } public string Back_Query(string Query) { try { string back_string; // این رشته نتیجه کوئری را بر می گرداند connection.ConnectionString = ConnectionString;// رشته اتصال به بانک را ست می کند connection.Open();// بانک را باز می کند SqlDataAdapter adaptor = new SqlDataAdapter(Query, connection);// یک شی آداپتور برای دستورات کوئری دو ورودی یک رشته کوئری دوم رشته اتصال DataTable dtt = new DataTable();// یک دیتا تیبل برای اینکه نتجیه کوئری در آن ریخته شود adaptor.Fill(dtt);// نتایه از آداپتور به دیتا تیبل بارگذاری می گردد back_string = dtt.Rows[0][0].ToString();// حال از دیتا گرید ردیف اول ستون اول زا انتخاب می کند چون ایت تابع فقط یک مقدار را بر می گرداند connection.Close(); return back_string;// اگر مقداری نداشت و خطا داد مقدار صفر بر می گردد } catch (Exception e) { connection.Close(); return "0"; } //------------------------------------------------------- */