ขอโค๊ดเพิ่มไอดีออโต้ C# หน่อยคับ อยากได้โค๊ดเพิ่มไอดีของ C# อ่าครับ อย่างเช่นว่าเมื่อเรากดปุ่ม
ต่อจากด้านบนนะครับ พอดีผมมีโค๊ดของ basic อยู่อ่าคับ แต่อยากได้โค๊ดแบบ C# ครับ ใครมีช่วยเอามาลงให้หน่อยนะครับ
Code (VB.NET)
Private Sub autoEmployee_ID()
Dim sqlcmd As SqlCommand = New SqlCommand
Dim dr As SqlDataReader
Dim tempSql As String = "select Max(Employee_id) as Employee_id from Employee"
Dim tempId As String
open_conn()
Try
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = tempSql
sqlcmd.Connection = Mainmenuform.SqlConnection1
dr = sqlcmd.ExecuteReader
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
dr.Read()
If IsDBNull(dr.Item("Employee_id")) = True Then
txtempid.Text = "EMP" & "0001"
Else
tempId = dr.Item("Employee_id")
txtempid.Text = "EMP" & (tempId.Substring(3) + 1).ToString("0000")
End If
closeConn()
End Sub
Date :
2010-08-14 13:24:37
By :
fakestar
ลองแปลงแล้วใช้ไม่ได้อยู๋ดีครับคุณ Stupid gurl รบกวนถ้ามีโค้ดหรือคำอธิบายใด ๆ ช่วยมาลงทีนะครับ ขอบคุณมาก
Date :
2010-08-19 15:46:25
By :
fakestar
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
Load balance : Server 02