 |
|
Code (VB.NET)
Function GetNewEmployee_ID(ByVal varConnectionString As String) As String
Dim iStr As String = String.Empty
Dim cmdBuilder As System.Text.StringBuilder = New System.Text.StringBuilder()
cmdBuilder.AppendLine("DECLARE @Counter AS BIGINT ;")
cmdBuilder.AppendLine("DECLARE @MaxCounter AS BIGINT ;")
cmdBuilder.AppendLine("DECLARE @Temp AS INT ;")
cmdBuilder.AppendLine("DECLARE @IDPrefix AS Varchar(10);")
cmdBuilder.AppendLine("DECLARE @TempStr AS Varchar(MAX);")
cmdBuilder.AppendLine()
cmdBuilder.AppendLine("SET @IDPrefix = 'EMP-' ;")
cmdBuilder.AppendLine("SET @Counter = 0 ;")
cmdBuilder.AppendLine("SET @MaxCounter = 999999999999 ;")
cmdBuilder.AppendLine()
cmdBuilder.AppendLine("SELECT @TempStr = MAX([Employee_id]) FROM [Employee] ;")
cmdBuilder.AppendLine()
cmdBuilder.AppendLine("IF (LEN(@TempStr)>0) ")
cmdBuilder.AppendLine("BEGIN")
cmdBuilder.AppendLine(" SET @TempStr = SUBSTRING(LEN(@IDPrefix) +1,LEN(@TempStr));")
cmdBuilder.AppendLine(" SET @Counter = CONVERT(BIGINT ,@TempStr) ;")
cmdBuilder.AppendLine("END ")
cmdBuilder.AppendLine("ELSE")
cmdBuilder.AppendLine(" SET @Counter = 0 ;")
cmdBuilder.AppendLine()
cmdBuilder.AppendLine("SET @Counter = @Counter +1 ;")
cmdBuilder.AppendLine("IF (@Counter > @MaxCounter)")
cmdBuilder.AppendLine(" SET @Counter = 1 ;")
cmdBuilder.AppendLine()
cmdBuilder.AppendLine("-- FORMAT DATA FOR OUTPUT")
cmdBuilder.AppendLine("SET @Temp = LEN(@MaxCounter) ;")
cmdBuilder.AppendLine("SET @TempStr = CONVERT(VARCHAR ,@Counter) ;")
cmdBuilder.AppendLine()
cmdBuilder.AppendLine("IF (LEN(@TempStr) < @Temp)")
cmdBuilder.AppendLine(" SET @TempStr = REPLICATE('0' ,@Temp - LEN(@TempStr)) + @TempStr ;")
cmdBuilder.AppendLine()
cmdBuilder.AppendLine("SET @TempStr = @IDPrefix + @TempStr ;")
cmdBuilder.AppendLine()
cmdBuilder.AppendLine("SELECT @TempStr ;")
Using myConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(varConnectionString)
Dim myCmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(cmdBuilder.ToString(), myConnection)
Try
myConnection.Open()
iStr = myCmd.ExecuteScalar().ToString()
Catch ex As Exception
iStr = "#Error"
End Try
End Using
Return iStr
End Function
Code (C#)
public string GetNewEmployee_ID(string varConnectionString)
{
string iStr = string.Empty;
System.Text.StringBuilder cmdBuilder = new System.Text.StringBuilder();
cmdBuilder.AppendLine("DECLARE @Counter AS BIGINT ;");
cmdBuilder.AppendLine("DECLARE @MaxCounter AS BIGINT ;");
cmdBuilder.AppendLine("DECLARE @Temp AS INT ;");
cmdBuilder.AppendLine("DECLARE @IDPrefix AS Varchar(10);");
cmdBuilder.AppendLine("DECLARE @TempStr AS Varchar(MAX);");
cmdBuilder.AppendLine();
cmdBuilder.AppendLine("SET @IDPrefix = 'EMP-' ;");
cmdBuilder.AppendLine("SET @Counter = 0 ;");
cmdBuilder.AppendLine("SET @MaxCounter = 999999999999 ;");
cmdBuilder.AppendLine();
cmdBuilder.AppendLine("SELECT @TempStr = MAX([Employee_id]) FROM [Employee] ;");
cmdBuilder.AppendLine();
cmdBuilder.AppendLine("IF (LEN(@TempStr)>0) ");
cmdBuilder.AppendLine("BEGIN");
cmdBuilder.AppendLine(" SET @TempStr = SUBSTRING(LEN(@IDPrefix) +1,LEN(@TempStr));");
cmdBuilder.AppendLine(" SET @Counter = CONVERT(BIGINT ,@TempStr) ;");
cmdBuilder.AppendLine("END ");
cmdBuilder.AppendLine("ELSE");
cmdBuilder.AppendLine(" SET @Counter = 0 ;");
cmdBuilder.AppendLine();
cmdBuilder.AppendLine("SET @Counter = @Counter +1 ;");
cmdBuilder.AppendLine("IF (@Counter > @MaxCounter)");
cmdBuilder.AppendLine(" SET @Counter = 1 ;");
cmdBuilder.AppendLine();
cmdBuilder.AppendLine("-- FORMAT DATA FOR OUTPUT");
cmdBuilder.AppendLine("SET @Temp = LEN(@MaxCounter) ;");
cmdBuilder.AppendLine("SET @TempStr = CONVERT(VARCHAR ,@Counter) ;");
cmdBuilder.AppendLine();
cmdBuilder.AppendLine("IF (LEN(@TempStr) < @Temp)");
cmdBuilder.AppendLine(" SET @TempStr = REPLICATE('0' ,@Temp - LEN(@TempStr)) + @TempStr ;");
cmdBuilder.AppendLine();
cmdBuilder.AppendLine("SET @TempStr = @IDPrefix + @TempStr ;");
cmdBuilder.AppendLine();
cmdBuilder.AppendLine("SELECT @TempStr ;");
using (System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(varConnectionString)) {
System.Data.SqlClient.SqlCommand myCmd = new System.Data.SqlClient.SqlCommand(cmdBuilder.ToString(), myConnection);
try {
myConnection.Open();
iStr = myCmd.ExecuteScalar().ToString();
} catch (Exception ex) {
iStr = "#Error";
}
}
return iStr;
}
|
ประวัติการแก้ไข 2010-08-19 16:46:54
 |
 |
 |
 |
Date :
2010-08-19 16:44:05 |
By :
blurEyes |
|
 |
 |
 |
 |
|
|
 |