แสดงบทความที่มีป้ายกำกับ c# แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ c# แสดงบทความทั้งหมด

วันศุกร์ที่ 25 พฤศจิกายน พ.ศ. 2554

EX Ping c#

 
/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SimplePing
{
   public static void Main(string[] argv)
   {
      byte[] data = new byte[1024];
      int recv;
      Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
                 ProtocolType.Icmp);
      IPEndPoint iep = new IPEndPoint(IPAddress.Parse(argv[0]), 0);
      EndPoint ep = (EndPoint)iep;
      ICMP packet = new ICMP();

      packet.Type = 0x08;
      packet.Code = 0x00;
      packet.Checksum = 0;
      Buffer.BlockCopy(BitConverter.GetBytes((short)1), 0, packet.Message, 0, 2);
      Buffer.BlockCopy(BitConverter.GetBytes((short)1), 0, packet.Message, 2, 2);
      data = Encoding.ASCII.GetBytes("test packet");
      Buffer.BlockCopy(data, 0, packet.Message, 4, data.Length);
      packet.MessageSize = data.Length + 4;
      int packetsize = packet.MessageSize + 4;

      UInt16 chcksum = packet.getChecksum();
      packet.Checksum = chcksum;

      host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
      host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
      try
      {
         data = new byte[1024];
         recv = host.ReceiveFrom(data, ref ep);
      } catch (SocketException)
      {
         Console.WriteLine("No response from remote host");
         return;
      }
      ICMP response = new ICMP(data, recv);
      Console.WriteLine("response from: {0}", ep.ToString());
      Console.WriteLine("  Type {0}", response.Type);
      Console.WriteLine("  Code: {0}", response.Code);
      int Identifier = BitConverter.ToInt16(response.Message, 0);
      int Sequence = BitConverter.ToInt16(response.Message, 2);
      Console.WriteLine("  Identifier: {0}", Identifier);
      Console.WriteLine("  Sequence: {0}", Sequence);
      string stringData = Encoding.ASCII.GetString(response.Message, 4, response.MessageSize - 4);
      Console.WriteLine("  data: {0}", stringData);
      
      host.Close();
   }
}

class ICMP
{
   public byte Type;
   public byte Code;
   public UInt16 Checksum;
   public int MessageSize;
   public byte[] Message = new byte[1024];

   public ICMP()
   {
   }

   public ICMP(byte[] data, int size)
   {
      Type = data[20];
      Code = data[21];
      Checksum = BitConverter.ToUInt16(data, 22);
      MessageSize = size - 24;
      Buffer.BlockCopy(data, 24, Message, 0, MessageSize);
   }

   public byte[] getBytes()
   {
      byte[] data = new byte[MessageSize + 9];
      Buffer.BlockCopy(BitConverter.GetBytes(Type), 0, data, 0, 1);
      Buffer.BlockCopy(BitConverter.GetBytes(Code), 0, data, 1, 1);
      Buffer.BlockCopy(BitConverter.GetBytes(Checksum), 0, data, 2, 2);
      Buffer.BlockCopy(Message, 0, data, 4, MessageSize);
      return data;
   }

   public UInt16 getChecksum()
   {
      UInt32 chcksm = 0;
      byte[] data = getBytes();
      int packetsize = MessageSize + 8;
      int index = 0;

      while ( index < packetsize)
      {
         chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index));
         index += 2;
      }
      chcksm = (chcksm >> 16) + (chcksm & 0xffff);
      chcksm += (chcksm >> 16);
      return (UInt16)(~chcksm);
   }
}

วันพฤหัสบดีที่ 3 พฤศจิกายน พ.ศ. 2554

c# การสร้าง List ข้อมูลเพื่อเป็น item ให้ combo box

ขั้นตอนที่1
  สร้างคลาสของข้อมูลตามโครงสร้างนี้
     public  class reportType
    {       string name;
            int value;
         
      public reportType(string inName, int inValue)
            {
                name = inName;
                this.value = inValue;
            }
            public string reportName
            {
                get
                {
                    return name;
                }
            }
            public int reportValue
            {
                get
                {
                    return value;
                }
            }
        }
 สร้าง list ข้อมูล
               List<reportType> list = new List<reportType>();
            list.Add(new reportType("A", 11));
            list.Add(new reportType("B", 12));
            list.Add(new reportType("C", 13));

ผูกกับ combo box
      cb_reportType.DisplayMember = "reportName";
       cb_reportType.ValueMember = "reportValue";
       cb_reportType.DataSource = list;

วันพุธที่ 2 พฤศจิกายน พ.ศ. 2554

c# กล่องข้อความยืนยัน

 DialogResult dlg = MessageBox.Show("คุณต้องการลบบิลเลขที่ \"" + dgv_delSaleType[0, this.dgv_delSaleType.CurrentCell.RowIndex].Value.ToString() + "\" ใช้หรือไม่", "ยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dlg == DialogResult.Yes){

               //---  คำสั่งเมื่อกดปุ่ม ok
           }

c# ปิด control bar

 this.ControlBox = false;

วันอังคารที่ 1 พฤศจิกายน พ.ศ. 2554

c# เข้ารหัส MD5

using System.Text;
using System.Security.Cryptography;

public void DoMD5(string input)
{
       string encrypted = [color=#4000FF]EnCryptToMD5[/color](txtpassword.text);
       Response.Write(encrypted );
}

public static string EnCryptToMD5(string input)
{
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = md5.ComputeHash(bs);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bs)
            {
                sb.Append(b.ToString("x2").ToLower());
            }

            return sb.ToString();
}

วันศุกร์ที่ 28 ตุลาคม พ.ศ. 2554

c# ป้องกันการป้อนตัวอักษร

 private void txt_moo_KeyPress(object sender, KeyPressEventArgs e)
        {
            int cInt = Convert.ToInt32(e.KeyChar);
            if ((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57 || cInt == 8 )
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

c# การเรียกใช้ Store

  int queryOk;
            //---------- ติดต่อฐานข้อมูล ------------------
            conDB conDB01 = new conDB();
            string conStr = conDB01.getConStr();
            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand("dbo.SP_updateInformationByshopID", conn);

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@inShopID", SqlDbType.VarChar).Value = lb_shopID.Text;
            cmd.Parameters.Add("@inShopName", SqlDbType.NVarChar).Value = txt_shopName.Text;
            cmd.Parameters.Add("@inContact", SqlDbType.NVarChar).Value = txt_contract.Text;
            cmd.Parameters.Add("@inHomeNumber", SqlDbType.VarChar).Value = txt_number.Text;
            cmd.Parameters.Add("@inMoo", SqlDbType.Int).Value = int.Parse(txt_moo.Text);
            cmd.Parameters.Add("@inRoad", SqlDbType.NVarChar).Value = txt_road.Text;
            cmd.Parameters.Add("@inTumbon", SqlDbType.NVarChar).Value = txt_tumbon.Text;
            cmd.Parameters.Add("@inAmper", SqlDbType.NVarChar).Value = txt_amper.Text;
            cmd.Parameters.Add("@inProvince", SqlDbType.NVarChar).Value = txt_province;
            cmd.Parameters.Add("@inPostCode", SqlDbType.Int).Value = int.Parse(txt_zipCode.Text);
            cmd.Parameters.Add("@inPhone", SqlDbType.NVarChar).Value = txt_phone.Text;
            cmd.Parameters.Add("@inMobile", SqlDbType.NVarChar).Value = txt_mobile.Text;
            cmd.Parameters.Add("@inLat", SqlDbType.NVarChar).Value = txt_lat.Text;
            cmd.Parameters.Add("@inLong", SqlDbType.NVarChar).Value = txt_long.Text;
            cmd.Parameters.Add("@inShopType", SqlDbType.VarChar).Value = txt_shopType.Text;
            cmd.Parameters.Add("@inShopRate", SqlDbType.Int).Value = int.Parse(txt_rank.Text);
            cmd.Parameters.Add("@inDept", SqlDbType.VarChar).Value = txt_dept.Text;
            cmd.Parameters.Add("@inUnit", SqlDbType.NVarChar).Value = txt_unit.Text;
            cmd.Parameters.Add("@inNote", SqlDbType.NVarChar).Value = txt_note.Text;
            cmd.Parameters.Add("@inVisitDate", SqlDbType.Date).Value = lb_visitDate.Text;
            cmd.Parameters.Add("@inDayTrip", SqlDbType.Int).Value = int.Parse(txt_dayTrip.Text);


            queryOk = cmd.ExecuteNonQuery();
            conn.Close();


            if (queryOk == 1) //ตรวจสอบผลการ query
            {
                 MessageBox.Show("อัพเดตข้อมูลเสร็จเรียบร้อย");
                 this.Close();
            }
            else
            {
                MessageBox.Show("การอัพเดตเกิดความผิดพลาด");
            }

การสร้าง auto complete ให้กับ textbox

ขั้นตอนที่ 1 : ประกาศตัวแปลง AutoCompleteStringCollection
    private static AutoCompleteStringCollection acShopName = new AutoCompleteStringCollection();

ขั้นตอนที่ 2 : เขียน method เพื่อดึงข้อมูลจากฐานข้อมูล 
   
 private void InitAcShopName()
        {
            conDB conDB01 = new conDB();
            string conStr = conDB01.getConStr();
            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();

            SqlCommand cmd = new SqlCommand("dbo.SP_autocom", conn);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@inType", SqlDbType.Int).Value = 6;

            SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            myAdapter.Fill(ds, "numData");
            conn.Close();
            int i = 0;
            for (i = 0; i < ds.Tables["numData"].Rows.Count; i++)
            {
                acShopName.Add(ds.Tables["numData"].Rows[i]["shopName"].ToString());
            }

        }

ขั้นตอนที่ 3 : เรียก method เพื่อใส่ค่าใน method load ของ ฟอร์ม
    //--------------------text ชื่อร้าน----------------------------------
            this.InitAcShopName();

            txt_name.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            txt_name.AutoCompleteSource = AutoCompleteSource.CustomSource;

            txt_name.AutoCompleteCustomSource = acShopName;

วันพฤหัสบดีที่ 19 พฤษภาคม พ.ศ. 2554

การใช้งานฐานข้อมูล MySQL ด้วย C#


 การใช้งานฐานข้อมูล MySQL ด้วย C# 

mysql_connector_net_main.jpgนนี้มาสรุปการใช้งานฐานข้อมูล MySQL ด้วย C# เอาไว้หลังจากที่ออฟใช้ในการทำงานโปรเจ็คแล้ว เรื่องที่สรุปก็มีในส่วนของการเชื่อมต่อฐานข้อมูล, การใช้ mysql connector/net ในการช่วยจัดการฐานข้อมูล MySQL, รวมถึงตัวอย่างการใช้งานพื้นฐานอย่างการ Insert

วันพฤหัสบดีที่ 17 กุมภาพันธ์ พ.ศ. 2554

วิธีเขียน C# ติดต่อกับ กล้อง + GPS ของ Window Mobile

การใช้ GPS ด้วย C# มีโค๊ดแถมมาพร้อม SDK ครับ เป็นการติดต่อกับ GPS Intermediate Driver เพื่ออ่านค่าจากโมดูล GPS
หาดูใน C:\Program Files\Windows Mobile 6 SDK\Samples\Smartphone\CS\GPS ได้ครับ
ถ้าอ่านค่ามาได้แล้ว ค่ามันจะเป็นภาษา NMEA อะไรของมันนี่แหละครับ มีคนเขียน Library แปลงให้แล้วที่
http://www.tma.dk/gps/
ส่วนการจะเซฟภาพถ่าย ใช้ CameraCaptureDialog (http://msdn.microsoft.com/en-us/library/microsoft.windowsmobile.forms.cameracapturedialog_members.aspx) ครับ เราควบคุมอะไรมันไม่ได้ แต่ว่ามันจะเซฟไฟล์รูปให้เราได้
จากนั้น ได้พิกัดจาก GPS แล้ว ได้รูปแล้ว จะเซฟพิกัด แนบกับรูปไปเลย ก้อต้องเขียน EXIF ครับhttp://www.opennetcf.com/ มี Library ชื่อว่า Smart Device Framework (http://www.opennetcf.com/Products/SmartDeviceFramework/tabid/65/Default.aspx) ลองๆ หาดู มีคนเขียน Sample ไว้แล้วครับ http://blog.markarteaga.com/ExtractingEXIFTagsUsingCompactFramework.aspx

วันอังคารที่ 17 สิงหาคม พ.ศ. 2553

การใช้งาน C# Socket Helper Classes (ตอนที่ 2: TcpListenner )








TcpListenenner Class 
     TcpListener Class จะอยู่ภายใต้ namespace System.Net.Sockets ซึ่งคลาสในนี้จะใช้ในการอำนวยความสะดวก ในการสร้าง TCP Server Application