sondmk header
C# Programming

C# : Database Connection > การเชื่อต่อ Database ผ่านไฟล์ ini (Connect Database With .ini)

Post by Goborijung at 2020-11-18 11:03:42 | ID: 884

//Reading/writing an INI file
//The tiny class (class เล็กๆ) : เพิ่มคลาสใหม่ชื่อ inifile.cs ในโปรเจ็กต์ของคุณ

using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

// Change this to match your program's normal namespace
namespace MyProg
{
    class IniFile   // revision 11
    {
        string Path;
        string EXE = Assembly.GetExecutingAssembly().GetName().Name;

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);

        public IniFile(string IniPath = null)
        {
            Path = new FileInfo(IniPath ?? EXE + ".ini").FullName;
        }

        public string Read(string Key, string Section = null)
        {
            var RetVal = new StringBuilder(255);
            GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
            return RetVal.ToString();
        }

        public void Write(string Key, string Value, string Section = null)
        {
            WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
        }

        public void DeleteKey(string Key, string Section = null)
        {
            Write(Key, null, Section ?? EXE);
        }

        public void DeleteSection(string Section = null)
        {
            Write(null, null, Section ?? EXE);
        }

        public bool KeyExists(string Key, string Section = null)
        {
            return Read(Key, Section).Length > 0;
        }
    }
}

// How to use it : การใช้งาน Class : เปิดไฟล์ ini ด้วยวิธีใดวิธีหนึ่งดังต่อไปนี้ (มี 3 วิธี)
// Creates or loads an INI file in the same directory as your executable
// named EXE.ini (where EXE is the name of your executable)
var MyIni = new IniFile();

// Or specify a specific name in the current dir
var MyIni = new IniFile("Settings.ini");

// Or specify a specific name in a specific dir
var MyIni = new IniFile(@"C:Settings.ini");

// คุณสามารถเขียนค่าบางอย่างได้
MyIni.Write("DefaultVolume", "100");
MyIni.Write("HomePage", "http://www.google.com");
จะได้
[MyProg]
DefaultVolume=100
HomePage=http://www.google.com

//อ่านค่าจากไฟล์ ini
var DefaultVolume = MyIni.Read("DefaultVolume");
var HomePage = MyIni.Read("HomePage");

// เราสามารถตั้งค่าได้
MyIni.Write("DefaultVolume", "100", "Audio");
MyIni.Write("HomePage", "http://www.google.com", "Web");
จะได้
[Audio]
DefaultVolume=100

[Web]
HomePage=http://www.google.com

// สามารถตรวจสอบการมีอยู่ของคีย์ได้
if(!MyIni.KeyExists("DefaultVolume", "Audio"))
{
    MyIni.Write("DefaultVolume", "100", "Audio");
}

//การลบคีย์
MyIni.DeleteKey("DefaultVolume", "Audio");

//เรายังสามารถลบทั้งส่วน (รวมถึงคีย์ทั้งหมด) ได้เช่นกัน
MyIni.DeleteSection("Web");

------------------------------------------------------

// Steps การใช้คลาส ini
using INI;

INIFile ini = new INIFile("C:	est.ini");

C# : DataGridView Cell Index is Checked == True

Post by Goborijung at 2020-09-14 13:31:05 | ID: 798

private void dgS_CellClick(object sender, DataGridViewCellEventArgs e)
{
  int rowCount = dgS.Rows.Count - 1; //ให้ Row เปลี่ยนสีเมื่อมีการ Click และ click เป็น true
  for (i = 0; i <= rowCount; i++)
  {
    if (System.Convert.ToBoolean(dgS.Rows[i].Cells[0].Value) == true)
    {
      //dgS.Rows[i].DefaultCellStyle.BackColor = Color.HotPink;
      dgS.Rows[i].Cells[0].Value = true;
    }
    else
    {
      //dgS.Rows[i].DefaultCellStyle.BackColor = Color.White;
      dgS.Rows[i].Cells[0].Value = false;
    }
  }
}

C# : DataGridView Row Enter

Post by Goborijung at 2020-09-14 13:48:30 | ID: 799

private void dg_RowEnter(object sender, DataGridViewCellEventArgs e)
{
  //dg
  int iVal = 0;
  int rowCount = dg.Rows.Count-1;
  for (int r = 0; r < rowCount; r++)
  {
    iVal += Convert.ToInt32(dg.Rows[r].Cells[4].Value);
  }
  txtQty.Text = iVal.ToString();
}

C# : Date Time Format

Post by Goborijung at 2020-11-16 10:05:36 | ID: 879

DateTime.Now.ToString("M/d/yyyy");
Result : "9/1/2015"

DateTime.Now.ToString("M-d-yyyy");
Result : "9-1-2015"

DateTime.Now.ToString("yyyy-MM-dd");
Result : "2015-09-01"

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
Result : "2015-09-01 09:20:10"

C# : dr.Read()

Post by Goborijung at 2020-10-30 08:38:22 | ID: 846

private void btnSearch_Click(object sender, EventArgs e) {
  conn = db.GSSv2_Prod();
  string sql = "Select FG_Barcode as SerialNumber From carton_Temp where id = (select max(id) From carton_Temp)";
  SqlCommand cmd = new SqlCommand(sql, conn);
  conn.Open();
  SqlDataReader dr = cmd.ExecuteReader();
  if (dr.Read() == true) {
    string defaultStr = dr["SerialNumber"].ToString();
  }
  dr.Close();
  cmd.Dispose();
  conn.Close();
}

C# : Execute exe File

Post by Goborijung at 2020-09-15 11:53:45 | ID: 772

private void aCGetPOToolStripMenuItem_Click(object sender, EventArgs e)
{
  string GETACBar = "~~" + "~~172.16.0.190~~pf_project~~ITS~~GETACBar.exe";
  Console.WriteLine(GETACBar);
  Process.Start(GETACBar);
}

C# : Find String or Charector , String.IndexOf()

Post by Goborijung at 2021-04-08 14:39:14 | ID: 1120

string str = "GeeksForGeeks";
 
// Finding the index of character
// which is present in string and
// this will show the value 5
int index1 = str.IndexOf('F'); // พบตำแหน่งที่ 5 


การหา indexof ตัวสุดท้าย
string s = "thisis";
int x = s.LastIndexOf('i');
Console.WriteLine ("Hello Mono World : "+x.ToString()); // พบ i เป็น index ที่ 4

-----------

Reference : https://www.geeksforgeeks.org/c-sharp-string-indexof-method-set-1/
C # | String.IndexOf () วิธีการ | ชุด - 1
อัปเดตล่าสุด: 31 ม.ค. 2019
ใน C # เมธอดIndexOf ()เป็นเมธอดสตริง วิธีนี้ใช้เพื่อค้นหาดัชนีตามศูนย์ของการเกิดครั้งแรกของอักขระหรือสตริงที่ระบุภายในอินสแตนซ์ปัจจุบันของสตริง วิธีการคืนค่า -1 หากไม่พบอักขระหรือสตริง วิธีนี้สามารถโอเวอร์โหลดได้โดยส่งผ่านพารามิเตอร์ที่แตกต่างกันไป

String.IndexOf (char x)
String.IndexOf (char x, int start1)
String.IndexOf (char x, int start1, int start2)
String.IndexOf (สตริง s1)
String.IndexOf (สตริง s1, int start1)
String.IndexOf (สตริง s1, int start1, int start2)
String.IndexOf (สตริง s1, int start1, int start2, StringComparison cType)
String.IndexOf (สตริง s1, int start1, StringComparison cType)
String.IndexOf (สตริง s1, StringComparison cType)
String.IndexOf (ถ่าน x) วิธีการ
วิธีนี้จะส่งคืนดัชนีที่อิงเป็นศูนย์ของการเกิดครั้งแรกของอักขระที่ระบุภายในสตริง ในกรณีที่ไม่พบอักขระดังกล่าวก็จะส่งกลับ -1

ไวยากรณ์:

int สาธารณะ IndexOf (ถ่าน x)
พารามิเตอร์:วิธีนี้ใช้พารามิเตอร์char xของชนิดSystem.Charซึ่งระบุอักขระที่ต้องการค้นหา
ประเภทย้อนกลับ:ประเภทการกลับมาของวิธีนี้คือSystem.Int32
ตัวอย่าง:ในโค้ดด้านล่างนี้ผู้ใช้ต้องการทราบดัชนีของอักขระ 'F' ภายในสตริงที่ระบุ "GeeksForGeeks" และด้วยเหตุนี้วิธีนี้จะส่งคืนผลลัพธ์ตามลำดับโดยส่วนใหญ่เป็นการเกิดครั้งแรกของอักขระ 'F' นอกจากนี้ในกรณีที่สองไม่มีอักขระ 'C' ดังนั้นจึงส่งกลับค่า -1


// C# program to illustrate the 
// String.IndexOf(char x) method
using System;
namespace ConsoleApplication1 {
  
class Geeks {
  
    // Main Method
    static void Main(string[] args)
    {
  
        string str = "GeeksForGeeks";
  
        // Finding the index of character 
        // which is present in string and
        // this will show the value 5
        int index1 = str.IndexOf('F');
          
        Console.WriteLine("The Index Value of character 'F' is " + index1);
  
        // Now finding the index of that character which
        //  is not even present with the string
        int index2 = str.IndexOf('C');
  
        // As expected, this will output value -1
        Console.WriteLine("The Index Value of character 'C' is " + index2);
    }
}
}
เอาท์พุต:



ค่าดัชนีของอักขระ 'F' คือ 5
ค่าดัชนีของอักขระ 'C' คือ -1
String.IndexOf (ถ่าน x, int start1) วิธีการ
วิธีนี้จะส่งคืนดัชนีที่อิงเป็นศูนย์ของการเกิดครั้งแรกของอักขระที่ระบุภายในสตริง อย่างไรก็ตามการค้นหาอักขระนั้นจะเริ่มจากตำแหน่งที่ระบุและหากไม่พบจะส่งกลับค่า -1

ไวยากรณ์:

int สาธารณะ IndexOf (ถ่าน x, int start1)
พารามิเตอร์:วิธีนี้ใช้พารามิเตอร์สองตัวคือchar xของประเภทSystem.Charซึ่งระบุอักขระที่ต้องการค้นหาและstart1ของประเภทSystem.Int32ซึ่งระบุตำแหน่งเริ่มต้นในรูปแบบของค่าจำนวนเต็มจากจุดที่จะเริ่มการค้นหา
ประเภทย้อนกลับ:ประเภทการกลับมาของวิธีนี้คือSystem.Int32
ข้อยกเว้น:วิธีนี้สามารถให้ArgumentOutOfRangeExceptionถ้าstart1น้อยกว่า 0 (ศูนย์) หรือมากกว่าความยาวของสตริง
ตัวอย่าง:ในโค้ดด้านล่างนี้ผู้ใช้ต้องการทราบดัชนีของอักขระ "H" ภายในสตริงที่ระบุ "HelloGeeks" และด้วยเหตุนี้เมธอดนี้จะส่งกลับดัชนีของอักขระ 'H' ตามลำดับ อย่างไรก็ตามหากstart1มากกว่า 1 จะเห็นได้ชัดว่าจะคืนค่า -1


// C# program to illustrate the 
// String.IndexOf(char x, int start1) method
using System;
namespace ConsoleApplication2{
  
class Geeks {
  
    // Main Method
    static void Main(string[] args)
    {
  
        string str = "HelloGeeks";
  
        // Finding the index of character
        // which is present in string
        // this will show the value 0
        int index1 = str.IndexOf('H', 0);
    
  
        Console.WriteLine("The Index Value of character 'H' "+
                          "with start index 0 is " + index1);
  
        // Now finding the index of character 
        // 'H' with starting position greater
        // than index position of 'H'
        int index2 = str.IndexOf('H', 5);
  
        // As expected, this will output value -1
        Console.WriteLine("The Index Value of character 'H' is " + index2);
    }
}
}
เอาท์พุต:

ค่าดัชนีของอักขระ 'H' พร้อมดัชนีเริ่มต้น 0 คือ 0
ค่าดัชนีของอักขระ 'H' คือ -1
String.IndexOf (ถ่าน x, int start1, int start2) วิธีการ
วิธีนี้จะส่งคืนดัชนีที่ใช้ศูนย์ของการเกิดขึ้นครั้งแรกของอักขระที่ระบุภายในสตริง อย่างไรก็ตามการค้นหาอักขระนั้นจะเริ่มจากตำแหน่งที่ระบุstart1จนถึงตำแหน่งที่ระบุเช่นstart2และหากไม่พบจะส่งกลับ -1

ไวยากรณ์:

int สาธารณะ IndexOf (ถ่าน x, int start1, int start2)
พารามิเตอร์:วิธีนี้ใช้พารามิเตอร์สามตัว ได้แก่ถ่าน xประเภทSystem.Charซึ่งระบุอักขระที่ต้องการค้นหาstart1ของประเภทSystem.Int32ซึ่งระบุตำแหน่งเริ่มต้นในรูปแบบของค่าจำนวนเต็มจากจุดที่การค้นหาเริ่มต้นและstart2ของ พิมพ์System.Int32ซึ่งระบุตำแหน่งสิ้นสุดที่จะหยุดการค้นหา
ประเภทย้อนกลับ:ประเภทการกลับมาของวิธีนี้คือSystem.Int32
ข้อยกเว้น:วิธีการนี้สามารถให้ArgumentOutOfRangeExceptionถ้าstart1 หรือ start2 เป็นลบหรือstart1 มีค่ามากกว่าความยาวของสตริงปัจจุบันหรือstart2 เป็นมากกว่าความยาวของกระแส start1
ตัวอย่าง:ในโค้ดด้านล่างนี้ผู้ใช้ต้องการทราบดัชนีของอักขระ 'R' ภายในสตริงที่ระบุ "My Life My Rules" และด้วยเหตุนี้วิธีนี้จะส่งกลับค่าดัชนีของอักขระ 'R' อีกครั้งสำหรับกรณีที่ start1> 1 และ start2 <8 มันจะคืนค่า -1 อีกครั้งเนื่องจากไม่พบอักขระใด ๆ


// C# program to illustrate the
// String.IndexOf(char x, int start1,
//  int start2) method
using System;
namespace ConsoleApplication3 {
  
class Geeks {
  
    // Main Method
    static void Main(string[] args)
    {
  
        string str = "My Life My Rules";
  
        int index1 = str.IndexOf('R', 2, 14);
  
        // Here starting index is < Index value of 'R'
        // Also ending index is > Index of 'R'
        // Hence It is obvious to return 11
        Console.WriteLine("Index Value of 'R' with start"+ 
                          " Index =2 and end Index = 15 is " + index1);
  
        // Now here starting index is chosen right
        // However ending position is < index of 'R'
        // Surely it will return -1
        int index2 = str.IndexOf('R', 1, 8);
  
        Console.WriteLine("Index Value of 'R' with start"+
                          " Index = 1 and end Index = 8 is " + index2);
    }
}
}
เอาท์พุต:

ค่าดัชนีของ 'R' พร้อมดัชนีเริ่มต้น = 2 และดัชนีสิ้นสุด = 15 คือ 11
ค่าดัชนีของ 'R' พร้อมดัชนีเริ่มต้น = 1 และสิ้นสุดดัชนี = 8 คือ -1
String.IndexOf (สตริง s1) วิธีการ
วิธีนี้จะส่งคืนดัชนีที่อิงเป็นศูนย์ของการเกิดครั้งแรกของสตริงย่อยที่ระบุภายในสตริง ในกรณีที่ไม่พบสตริงดังกล่าวจะส่งกลับ -1 เหมือนกับในกรณีของอักขระ

ไวยากรณ์:

ดัชนี int สาธารณะ (สตริง s1)
พารามิเตอร์:วิธีนี้ใช้พารามิเตอร์s1ประเภทSystem.Stringซึ่งระบุสตริงย่อยที่จะค้นหา
ประเภทย้อนกลับ:ประเภทการกลับมาของวิธีนี้คือSystem.Int32 ตำแหน่งดัชนีที่อิงศูนย์ของ s1 หากพบสตริงนั้นหรือ -1 ถ้าไม่ใช่ ถ้า s1 เป็น String.Empty ค่าที่ส่งคืนจะเป็น 0
ข้อยกเว้น:วิธีนี้สามารถให้ArgumentNullExceptionถ้าs1เป็นโมฆะ
ตัวอย่าง:ในโค้ดด้านล่างเป็นที่ทราบกันดีว่าสตริง 'How' อยู่ในสตริงหลักดังนั้นมันจะส่งคืนค่าดัชนีของอักขระตัวแรก อย่างไรก็ตามในกรณีถัดไปจะไม่มีสตริงย่อยที่มีชื่อ "Chair" ดังนั้นมันจะส่งกลับค่า -1


// C# program to illustrate the
// String.IndexOf(string  s1) method
using System;
namespace ConsoleApplication4 {
  
class Geeks {
  
    // Main Method
    static void Main(string[] args)
    {
  
        string str = "Hello Friends....How are you...";
  
        int i = str.IndexOf("How");
  
        // As this string is present in the 
        // main string then it will obviously
        //  output the value as 17
        Console.WriteLine("First value Index of 'How' is " + i);
  
        // now the following string is not present
        // So as per the rules, it will return -1
        int i1 = str.IndexOf("Chair");
  
        // As this string is present in 
        // the main string then it will 
        // obviously output the value as -1
        Console.WriteLine("First value Index of 'Chair' is " + i1);
    }
}
}
เอาท์พุต:

ดัชนีค่าแรกของ 'How' คือ 17
ค่าแรกดัชนีของ 'เก้าอี้' คือ -1
ผู้อ่านให้ความสนใจ! อย่าหยุดเรียนรู้ตอนนี้ รับแนวคิด DSA ที่สำคัญทั้งหมดด้วยหลักสูตร DSA Self Pacedในราคาที่เป็นมิตรกับนักเรียนและเตรียมพร้อมสำหรับอุตสาหกรรม

C# : GenBar

Post by Goborijung at 2020-10-22 22:05:04 | ID: 847

private void btnSearch_Click(object sender, EventArgs e) {
  conn = db.GSSv2_Prod();
  string sql = "Select FG_Barcode as SerialNumber From carton_Temp where id = (select max(id) From carton_Temp)";
  SqlCommand cmd = new SqlCommand(sql, conn);
  conn.Open();
  SqlDataReader dr = cmd.ExecuteReader();
  if (dr.Read() == true) {
    string defaultStr = dr["SerialNumber"].ToString(); //FG201022004265
    string subL = defaultStr.Substring(0, 8); //FG201022
    string subR = "000000" + (Convert.ToInt32(defaultStr.Substring(8, 6)) + 1); //0000004266
    int strLength = subR.Length; //0000004266 : 10 Digit
    string newid = subL + subR.Substring(strLength - 6, 6);
    txtBarStart.Text = newid;
    txtBarEnd.Text = subL + "999999";
  }
  dr.Close();
  cmd.Dispose();
  conn.Close();
}

C# : Get Data to Combobox by Class

Post by Goborijung at 2020-09-14 10:35:03 | ID: 792

>> Class DBConn
class dbConn
{
  public string _server;
  public string _dbname;
  public string _user;
  public string _password;

  public SqlConnection GSSv2_Prod()
  {
    _server = "172.16.0.xxx";
    _dbname = "dbName";
    _user = "sa";
    _password = "Password";
    return new SqlConnection("Data Source="+_server+";Initial Catalog="+_dbname+";Persist Security Info=True;User ID="+_user+";Password="+_password+"");
  }
}

>> Class objForm
class objForm
{
  public void GetDataToComboBox(string sql, ComboBox cboName)
  {
    conn = new dbConn().GSSv2_Prod();
    cmd = new SqlCommand(sql,conn);
    cmd.CommandText = sql;
    conn.Open();
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
      cboName.Items.Add(dr[0].ToString());
    }
    conn.Close();
  }
}

>> การใช้งาน
new objForm().GetDataToComboBox("SELECT Name From Customer WHERE GCRecord is null",cboName);

C# : Get Data to DataGridView by Class

Post by Goborijung at 2020-09-14 10:40:49 | ID: 794

>> Class DBConn
class dbConn
{
  public string _server;
  public string _dbname;
  public string _user;
  public string _password;

  public SqlConnection GSSv2_Prod()
  {
    _server = "172.16.0.xxx";
    _dbname = "dbName";
    _user = "sa";
    _password = "Password";
    return new SqlConnection("Data Source="+_server+";Initial Catalog="+_dbname+";Persist Security Info=True;User ID="+_user+";Password="+_password+"");
  }
}

>> Class objForm
class objForm
{
  public void GetDataToGridView(string sql, DataGridView dgvName)
  {
    conn = new dbConn().GSSv2_Prod();
    da = new SqlDataAdapter(sql, conn);
    ds = new DataSet();
    conn.Open();
    da.Fill(ds, "tblName");
    conn.Close();
    dgvName.DataSource = ds;
    dgvName.DataMember = "tblName";
  }
}

>> การใช้งาน
new objForm().GetDataToGridView("SELECT Name From Customer WHERE GCRecord is null",dgvName);

<<<12345678910...>>>

Framework

Library


เครื่องมือพัฒนาเว็บ



การออกแบบและพัฒนาเว็บไซต์


Download SourceCode



copyAllright © 2016 soundmk.com