TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Suganda Suganda
NA
24
10.9k
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
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;
}
}
}
Reply
Answers (
1
)
Display currently running status of an Action Method
How to open pdf file in new tab from c# server code