Tuesday, October 9, 2012

Connect MySQL using C# (ODBC Connection)

Connect MySQL using C# (ODBC Connection)

//using System.Data.Odbc;
OdbcCommand ocmd;
OdbcDataAdapter da;

OdbcConnection dbcon = new OdbcConnection("DRIVER={MySQL ODBC 5.1 Driver};server=127.0.0.1;UID=admin;Password=admin;Persist Security Info=True;port=3308;Connection Reset=False;database=dbdata");

dbcon.Open();

da=new OdbcDataAdapter("SELECT * FROM `tblstudent` ORDER BY `Last_Name`,`First_Name`, `Middle_Name` ASC",dbcon);

ds = new DataSet();
da.Fill(ds,"tblstudent");
dbcon.Close();
MessageBox.Show(ds.Tables["tblstudent"].Rows[5].ItemArray[3].ToString());




// INSERT, UPDATE, DELETE
 OdbcCommand ocmd;
OdbcDataAdapter da;  
OdbcConnection dbcon = new OdbcConnection("DRIVER={MySQL ODBC 5.1 Driver};server=127.0.0.1;UID=admin;Password=admin;Persist Security Info=True;port=3308;Connection Reset=False;database=dbdata");   
dbcon.Open();         
           
ocmd=new OdbcCommand("INSERT INTO tblstudent (`Stud_ID`) VALUES('00-99999')",dbcon);
ocmd.ExecuteNonQuery();
dbcon.Close();

Sunday, October 7, 2012

Connect MS Access using C# (OleDB Connection)

Connect MS Access using C#  (OleDB Connection)

//Add References
//using System.Data.OleDb;
//Retrieving Record

            DataSet ds;
            OleDbCommand ocmd;
            OleDbDataAdapter da;
            OleDbConnection dbcon = new OleDbConnection("PROVIDER=microsoft.jet.oledb.4.0;data source =" + System.AppDomain.CurrentDomain.BaseDirectory + "\\dbase\\dbase.mdb");
            dbcon.Open();
            da = new OleDbDataAdapter("SELECT * FROM `tblstudent` ORDER BY `Last_Name`,`First_Name`, `Middle_Name` ASC", dbcon);
            ds = new DataSet();
            da.Fill(ds, "tblstudent");
            dbcon.Close();
            MessageBox.Show(ds.Tables["tblstudent"].Rows[5].ItemArray[3].ToString());

//INSERT, UPDATE, DELETE Record

OleDbCommand ocmd;

OleDbDataAdapter da;

 OleDbConnection dbcon = new OleDbConnection("PROVIDER=microsoft.jet.oledb.4.0;data source =" + System.AppDomain.CurrentDomain.BaseDirectory + "\\dbase\\dbase.mdb");

    dbcon.Open();

ocmd=new OleDbCommand("INSERT INTO tblstudent (`Stud_ID`) VALUES('00-99999')",dbcon);          

ocmd.ExecuteNonQuery();       

 dbcon.Close();

Saturday, October 6, 2012

How to add ResourceDictionary.xaml to your existing c# WPF Application

//How to add ResourceDictionary.xaml to your existing c# WPF Application
 //Goto ->Project, Add New Existing Project, then browse your ResourceDictionary
//-> copy the highlighted text inside app.xaml between Application.Resources
//please note that my ResourceDictionary1.xaml is inside a folder name Resources
//remove ResourceDictionary2.xaml if it is not available and rename ResourceDictionary1.xaml according
//to your Resource Dictionary file.

   
       
       
           
               
               
           

       

   

Friday, October 5, 2012

How do i escape single qoute (') in MySQL using c#

How do i escape single qoute (') in MySQL using c#

if you want to insert a data with a single qoue (') like the example below:

INSERT INTO tblname (`Field1`) VALUES ('5'4');

then you need to escape the single qoute (') using backslash (\), the correct SQL would be,


INSERT INTO tblname (`Field1`) VALUES ('5\'4');

to do that in c# assuming 5'4 is inside a textbox name txtheight, then the code will look like this,

 INSERT INTO tblname (`Field1`) VALUES ('" + txtheight.Text.Replace("'","\'") + "');

or

String.Format("INSERT INTO tbl (`Name`) VALUES ('{0}')", txtheight.Text.Replace("'","\'"))

Wednesday, October 3, 2012

Retrieve Local IP using c#

Retrieve Local IP using c#, Get Local IP using c#

//add System.Net namespace
//using System.Net;
  
            string localIP = "";
            foreach (IPAddress ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip.ToString();
                }
            }
            MessageBox.Show(localIP);

Monday, October 1, 2012

Read and Write Text File Line by Line using c#


//Read and Write Text File Line by Line using c#
//TextBlock  property:
//AcceptsReturn :checked
//AcceptsTab: checked

//Add Reference

using Microsoft.Win32;

private void btnbrowse_Click(object sender, RoutedEventArgs e)
        {
            String AppPath = System.AppDomain.CurrentDomain.BaseDirectory;
            OpenFileDialog dlg = new OpenFileDialog() { DefaultExt = ".jpg" };
          

            dlg.Filter = "txt (.txt)|*.txt";
            dlg.ShowDialog();

            try
            {
                //if (System.IO.Directory.Exists(AppPath + "images") == false)
                //{
                //    System.IO.Directory.CreateDirectory(AppPath + "images");
                //}

                txtfile.Text = dlg.FileName.ToString();
                             

                //throw new Exception("");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            //btnNew.Visibility = Visibility.Hidden;
        }

        private void btnRead_Click(object sender, RoutedEventArgs e)
        {
            int counter = 0;
            string line;
            txt.Text = "";
            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(txtfile.Text);
            while ((line = file.ReadLine()) != null)
            {
                txt.Text = txt.Text + line +"\n";
                counter++;
            }

            file.Close();

        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            txt.Text = "";
        }

        private void btnWrite_Click(object sender, RoutedEventArgs e)
        {
          
            // Read the file and display it line by line.
            try
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(txtfile.Text);

                file.WriteLine(txt.Text);
                file.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
          
        }