 |
|
ตอนแรกกำลังให้เลขอัตโนมัติเป็น 55-001 ตอนแรกก็บันทึกได้อ่ะครับ แต่พอมาครั้งที่2 ก็ยังเป็น 55-001 เหมือนเดิมเลย ตอนนี้อยากได้แบบรันไปเรื่อยๆๆอ่ะครับ 55-002,55-003 อ่ะครับ |
|
 |
|
|
 |
 |
|
Code (VB.NET)
Dim s As String = ""
s = "Select Top 1 id_sup From suppliers Order By id_sup DESC"
Dim da As New SqlDataAdapter(s, cn)
Dim ds As New DataSet
da.Fill(ds, "id_sup")
Dim ny As String = Now.Year + 543
Dim y As String = ny.Substring(2, 2)
Dim m As String = Now.Month.ToString("-")
Dim newid As String = ""
With ds.Tables("id_sup")
If .Rows.Count = 0 Then
newid = y & m & "001"
TextBox2.Text = newid
Exit Sub
End If
Dim oldid, Lid, mid, Rid As String
oldid = .Rows(0).Item(0).ToString
Lid = oldid.Substring(0, 2)
mid = oldid.Substring(2, 2)
Rid = oldid.Substring(3, 3)
If y = Lid Then
If m = mid Then
newid = y & m & (CInt(Rid) + 1).ToString("000")
Else
newid = y & m & "001"
End If
Else
newid = y & m & "001"
End If
TextBox2.Text = newid
End With
TextBox3.ReadOnly = False
TextBox3.Focus()
Tag : .NET, Ms SQL Server 2008, Win (Windows App), VB.NET, VS 2008 (.NET 3.x), Windows
|
|
 |
 |
 |
 |
Date :
2012-11-26 14:32:24 |
By :
บี |
View :
1032 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยหน่อยนะครับ
|
 |
 |
 |
 |
Date :
2012-11-26 14:56:40 |
By :
บี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Sub autoid()
Dim tmpautoid As Integer = 0
Dim sqlauto As String
sqlauto = "SELECT Field From Table "
DC.CommandType = CommandType.Text
DC.CommandText = sqlauto
DC.Connection = CONN
DA = New OleDbDataAdapter(DC)
DS1.Clear()
DA.Fill(DS1, "number")
tmpautoid = (DS1.Tables("number").Rows.Count) + 1
txt_Depart_id.Text = Format("55-" + tmpautoid.ToString("000"))
End Sub
ผมสร้าง Sub แล้วเรียกใช้เอาครับ แบบนี้ ลองดูครับ
|
 |
 |
 |
 |
Date :
2012-11-26 16:22:03 |
By :
Littlefatboyz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้เป็น โค้ด C# นะครับ ลองเอาไปประยุกต์ใช้งาน
Code (C#)
private string GetRunningId()
{
string _RunId = "";
string _GetDate = "";
if (con.State == ConnectionState.Open) con.Close();
con.ConnectionString = @"Data Source=172.0.0.1\sql2008;Initial Catalog=ThaiCreateDB;User ID=sa;Password=sa;";
con.Open();
sb.Remove(0, sb.Length);
sb.Append(" select GETDATE() as GetDate ");
DataSet dsGetDate = new DataSet();
da = new SqlDataAdapter(sb.ToString(), con);
da.Fill(dsGetDate, "GetDate");
_GetDate = Convert.ToDateTime(dsGetDate.Tables["GetDate"].Rows[0]["GetDate"].ToString()).Year.ToString();
sb.Remove(0, sb.Length);
sb.Append(" select max(customerid) as customerid from customer ");
DataSet dsRunning = new DataSet();
da = new SqlDataAdapter(sb.ToString(), con);
da.Fill(dsRunning, "Running");
if (dsRunning.Tables["Running"].Rows.Count != 0)
{
string _RunIdOld = dsRunning.Tables["Running"].Rows[0]["customerid"].ToString();
string[] _arrayRun = _RunIdOld.Split('-');
int _RunningNew = int.Parse(_arrayRun[1].ToString()) + 1;
_RunId = _GetDate.Substring(2, 2) + "-" + _RunningNew.ToString("0000");
}
else
{
_RunId = _GetDate.Substring(2, 2) + "-" + "0001";
}
return _RunId;
}
|
 |
 |
 |
 |
Date :
2012-11-27 13:34:38 |
By :
tee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|