Friday, November 30, 2012

Database connectivity.

Connection string for Sql and msacces 
  • SQL connection string:Data Source=(local) ;Initial Catalog=myDataBase.mdb ;Integrated Security=SSPI;
  •  MSacces connection string (2003): Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;
  •  MSacces connection string (2007): Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\mydatabase.mdb;

Sample code Sql:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace database_manupulation
{
    public partial class Form1 : Form
    {
        public string constring = "Data Source=TAHMED\\SQLEXPRESS;Initial Catalog=hospital;Integrated Security=TRUE";
        bool max = true;
        bool min = false;
        SqlConnection con = new SqlConnection("Data Source=(local) ;Initial Catalog=hospital;Integrated Security=TRUE");
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        public Form1()
        {
           
            InitializeComponent();
            timer1.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            da.SelectCommand = new SqlCommand("SELECT * FROM patient", con);
            ds.Clear();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                da.InsertCommand = new SqlCommand("INSERT INTO patient VALUES(@patient_name,@phy_id)", con);
                //da.InsertCommand = new SqlCommand("INSERT INTO treatment VALUES(@patient_id,@phy_id)", con);


                da.InsertCommand.Parameters.Add("@patient_name", SqlDbType.VarChar).Value = textBox1.Text;

                //da.InsertCommand.Parameters.Add("@phy_id", SqlDbType.Int).Value = Convert.ToInt32(textBox2.Text);

                //da.InsertCommand.Parameters.Add("@patient_id", SqlDbType.Int).Value = Convert.ToInt32(textBox3.Text);

                da.InsertCommand.Parameters.Add("@phy_id", SqlDbType.Int).Value = Convert.ToInt32(textBox2.Text);

                con.Open();
                da.InsertCommand.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception v)
            {
                con.Close();
                MessageBox.Show(v.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            da.DeleteCommand = new SqlCommand("DELETE FROM patient WHERE patient_id="+Convert.ToInt32( textBox2.Text), con);
          //  da.DeleteCommand = new SqlCommand("DELETE FROM treatment WHERE treatment_id=" +Convert.ToInt32( textBox2.Text), con);

            con.Open();
            da.DeleteCommand.ExecuteNonQuery();
            con.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           label2.Text = Convert.ToString( DateTime.Now.ToUniversalTime());
            //label2.Text = DateTime.Now.ToLongTimeString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
           //dataGridView2.Columns.Add("this", "THIS");
          // dataGridView2.Rows.Add("THis", 3);
            double a = Convert.ToDouble(dataGridView2.Rows[0].Cells[1].Value.ToString());
            double b = Convert.ToDouble(dataGridView2.Rows[1].Cells[1].Value.ToString());
            double c = Convert.ToDouble(dataGridView2.Rows[2].Cells[1].Value.ToString());
            double d = Convert.ToDouble(dataGridView2.Rows[3].Cells[1].Value.ToString());
            double aa = Convert.ToDouble(dataGridView2.Rows[4].Cells[1].Value.ToString());
            double zz = a + b +c+d+aa;
            ///dataGridView2.Columns.Add("1", "want check");
            label3.Text = (zz/5).ToString();
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //DataTable dt = new DataTable();
            //dt.Columns.Add("this");
            //DataRow a = dt.NewRow();
            //a[1] = "this and no regret";
            //label3.Text = a[1].ToString();
        }

        private void button5_Click(object sender, EventArgs e)
        {

            if (max)
            {
                this.WindowState = FormWindowState.Maximized;
                min = true;
                max = false;
            }
            else  { this.WindowState = FormWindowState.Normal; max = true; min = false;}
           
            //dataGridView2.Rows.Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView2.Rows.Add();
            dataGridView2.Rows[0].Cells[0].Value = "user";
            dataGridView2.Rows[0].Cells[1].Value = "3.5";
            dataGridView2.Rows[0].Cells[2].Value = "good";
            toolStripProgressBar1.Value = 20;
dataGridView2.Rows.Add();
            dataGridView2.Rows[1].Cells[0].Value = "user";
            dataGridView2.Rows[1].Cells[1].Value = "4";
            dataGridView2.Rows[1].Cells[2].Value = "good";
            toolStripProgressBar1.Value = 40;
            dataGridView2.Rows.Add();
            dataGridView2.Rows[2].Cells[0].Value = "user";
            dataGridView2.Rows[2].Cells[1].Value = "3";
            dataGridView2.Rows[2].Cells[2].Value = "good";
            toolStripProgressBar1.Value = 60;
            dataGridView2.Rows.Add();
            dataGridView2.Rows[3].Cells[0].Value = "user";
            dataGridView2.Rows[3].Cells[1].Value = "2.5";
            dataGridView2.Rows[3].Cells[2].Value = "good";
            toolStripProgressBar1.Value = 80;
            dataGridView2.Rows[4].Cells[0].Value = "user";
            dataGridView2.Rows[4].Cells[1].Value = "1.5";
            dataGridView2.Rows[4].Cells[2].Value = "good";
            toolStripProgressBar1.Value = 100;
            toolStripStatusLabel1.Text = "data loaded to datagrid view";
        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            toolStripStatusLabel1.Text = "windows color red";
            toolStripProgressBar1.Value = 0;
            this.BackColor = Color.Red;
        }
    }
}
 

No comments:

Post a Comment

used in operatonal research LP(linear programming) The Simplex Algorithm Simplex method Resolve using the Simple...