Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
1
Answer
How to create unique id with mysql and c#?
Suganda Suganda
7y
1.4k
1
Reply
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
using
MySql.Data.MySqlClient;
using
System;
using
System.Collections.Generic;
using
System.Data;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
SistemPakar
{
class
MySQLService : MySQLConn
{
private
String strCon =
""
;
private
MySqlConnection sConn;
private
MySqlCommand sComm;
private
MySqlDataAdapter dtAdp;
public
String Query;
public
MySQLService()
{
strCon = Constr();
sConn =
new
MySqlConnection(strCon);
sComm =
new
MySqlCommand();
dtAdp =
new
MySqlDataAdapter();
}
public
String Constr()
{
String data;
data =
"SERVER=localhost; DATABASE=csharp_sistempakar; UID=root; PWD=;"
;
return
data;
}
public
void
BukaKoneksi()
{
if
(sConn.State == ConnectionState.Closed)
{
try
{
sConn.Open();
}
catch
(Exception)
{ }
}
}
public
void
TutupKoneksi()
{
sConn.Close();
}
public
override
int
ExecNonQuery(String Query)
{
int
retVal = -1;
try
{
BukaKoneksi();
sComm.Connection = sConn;
sComm.CommandText = Query;
retVal = sComm.ExecuteNonQuery();
}
catch
(Exception) { }
finally
{
TutupKoneksi();
}
return
retVal;
}
public
override
DataTable ExecQuery(String Query)
{
DataTable retVal =
new
DataTable();
try
{
BukaKoneksi();
sComm.Connection = sConn;
sComm.CommandText = Query;
dtAdp.SelectCommand = sComm;
dtAdp.Fill(retVal);
}
catch
(Exception) { }
finally
{
TutupKoneksi();
}
return
retVal;
}
}
}
Class MySQLConn.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
SistemPakar
{
class
Srvc
{
//Membuat class untuk menampilkan id
private
String id;
public
Srvc()
{
id =
""
;
}
public
String ID
{
set
{ id = value; }
get
{
return
id; }
}
}
}
Class Srv.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
SistemPakar
{
class
Srvc
{
//Membuat class untuk menampilkan id
private
String id;
public
Srvc()
{
id =
""
;
}
public
String ID
{
set
{ id = value; }
get
{
return
id; }
}
}
}
Class Service.cs
using
System;
using
System.Collections.Generic;
using
System.Data;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
SistemPakar
{
class
Service : Srvc
{
MySQLService mysql;
DataTable dt;
public
Service()
{
mysql =
new
MySQLService();
dt =
new
DataTable();
}
public
String IDKostum()
{
String kode =
""
;
int
idx = 0;
mysql.Query =
"Select isnull(MAX(Right(code_hypothesis,4)),0) as jml From hypothesis"
;
dt = mysql.ExecQuery(mysql.Query);
if
(dt.Rows.Count > 0)
{
foreach
(DataRow dtRow
in
dt.Rows)
{
idx = Convert.ToInt32(dtRow.Field<String>(
"jml"
));
}
}
if
(idx >= 0 && idx <= 8)
{
kode =
"H"
+
"000"
+ Convert.ToString(idx + 1);
}
else
if
(idx >= 9 && idx <= 98)
{
kode =
"H"
+
"00"
+ Convert.ToString(idx + 1);
}
else
if
(idx >= 99 && idx <= 998)
{
kode =
"H"
+
"0"
+ Convert.ToString(idx + 1);
}
else
if
(idx >= 999 && idx <= 9998)
{
kode =
"H"
+ Convert.ToString(idx + 1);
}
return
kode;
}
}
}
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
Display currently running status of an Action Method
How to open pdf file in new tab from c# server code