Suganda Suganda

Suganda Suganda

  • NA
  • 24
  • 10.8k

How to create unique id with mysql and c#?

Mar 6 2018 10:37 PM
I try every video in youtube about unique id with C#. But, every video using SQL Server.
 
I create database id using varchar for input A0001, and try to covert code for MySQL database. But, not working. 
 
This is detail code:
 
Class MySQLService.cs
  1. using MySql.Data.MySqlClient;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8.   
  9. namespace SistemPakar  
  10. {  
  11.         class MySQLService : MySQLConn  
  12.         {  
  13.             private String strCon = "";  
  14.             private MySqlConnection sConn;  
  15.             private MySqlCommand sComm;  
  16.             private MySqlDataAdapter dtAdp;  
  17.             public String Query;  
  18.   
  19.             public MySQLService()  
  20.             {  
  21.                 strCon = Constr();  
  22.                 sConn = new MySqlConnection(strCon);  
  23.                 sComm = new MySqlCommand();  
  24.                 dtAdp = new MySqlDataAdapter();  
  25.             }  
  26.   
  27.             public String Constr()  
  28.             {  
  29.                 String data;  
  30.                 data = "SERVER=localhost; DATABASE=csharp_sistempakar; UID=root; PWD=;";  
  31.                 return data;  
  32.             }  
  33.   
  34.             public void BukaKoneksi()  
  35.             {  
  36.                 if (sConn.State == ConnectionState.Closed)  
  37.                 {  
  38.                     try  
  39.                     {  
  40.                         sConn.Open();  
  41.                     }  
  42.                     catch (Exception)  
  43.                     { }  
  44.                 }  
  45.             }  
  46.   
  47.             public void TutupKoneksi()  
  48.             {  
  49.                 sConn.Close();  
  50.             }  
  51.   
  52.             public override int ExecNonQuery(String Query)  
  53.             {  
  54.                 int retVal = -1;  
  55.   
  56.                 try  
  57.                 {  
  58.                     BukaKoneksi();  
  59.                     sComm.Connection = sConn;  
  60.                     sComm.CommandText = Query;  
  61.                     retVal = sComm.ExecuteNonQuery();  
  62.                 }  
  63.                 catch (Exception) { }  
  64.                 finally  
  65.                 {  
  66.                     TutupKoneksi();  
  67.                 }  
  68.                 return retVal;  
  69.             }  
  70.   
  71.             public override DataTable ExecQuery(String Query)  
  72.             {  
  73.                 DataTable retVal = new DataTable();  
  74.                 try  
  75.                 {  
  76.                     BukaKoneksi();  
  77.                     sComm.Connection = sConn;  
  78.                     sComm.CommandText = Query;  
  79.                     dtAdp.SelectCommand = sComm;  
  80.                     dtAdp.Fill(retVal);  
  81.                 }  
  82.                 catch (Exception) { }  
  83.                 finally  
  84.                 {  
  85.                     TutupKoneksi();  
  86.                 }  
  87.                 return retVal;  
  88.             }  
  89.         }  
  90. }   
Class MySQLConn.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace SistemPakar  
  8. {  
  9.     class Srvc  
  10.     {  
  11.         //Membuat class untuk menampilkan id  
  12.         private String id;  
  13.   
  14.         public Srvc()  
  15.         {  
  16.             id = "";  
  17.         }  
  18.   
  19.         public String ID  
  20.         {  
  21.             set { id = value; }  
  22.             get { return id; }  
  23.         }  
  24.     }  
  25. }   
Class Srv.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace SistemPakar  
  8. {  
  9.     class Srvc  
  10.     {  
  11.         //Membuat class untuk menampilkan id  
  12.         private String id;  
  13.   
  14.         public Srvc()  
  15.         {  
  16.             id = "";  
  17.         }  
  18.   
  19.         public String ID  
  20.         {  
  21.             set { id = value; }  
  22.             get { return id; }  
  23.         }  
  24.     }  
  25. }  
Class Service.cs 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace SistemPakar  
  9. {  
  10.     class Service : Srvc  
  11.     {  
  12.         MySQLService mysql;  
  13.         DataTable dt;  
  14.   
  15.         public Service()  
  16.         {  
  17.             mysql = new MySQLService();  
  18.             dt = new DataTable();  
  19.         }  
  20.   
  21.         public String IDKostum()  
  22.         {  
  23.             String kode = "";  
  24.             int idx = 0;  
  25.             mysql.Query = "Select isnull(MAX(Right(code_hypothesis,4)),0) as jml From hypothesis";  
  26.             dt = mysql.ExecQuery(mysql.Query);  
  27.   
  28.             if (dt.Rows.Count > 0)  
  29.             {  
  30.                 foreach (DataRow dtRow in dt.Rows)  
  31.                 {  
  32.                     idx = Convert.ToInt32(dtRow.Field<String>("jml"));  
  33.                 }  
  34.             }  
  35.   
  36.             if (idx >= 0 && idx <= 8)  
  37.             {  
  38.                 kode = "H" + "000" + Convert.ToString(idx + 1);  
  39.             }  
  40.             else if (idx >= 9 && idx <= 98)  
  41.             {  
  42.                 kode = "H" + "00" + Convert.ToString(idx + 1);  
  43.             }  
  44.             else if (idx >= 99 && idx <= 998)  
  45.             {  
  46.                 kode = "H" + "0" + Convert.ToString(idx + 1);  
  47.             }  
  48.             else if (idx >= 999 && idx <= 9998)  
  49.             {  
  50.                 kode = "H" + Convert.ToString(idx + 1);  
  51.             }  
  52.             return kode;  
  53.         }  
  54.     }  
  55. }  
 

Answers (1)