منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : استفسار :- رقم الاي دي لاخر ادخال في قاعدة البيانات
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
السلام عليكم ورحمة الله

محتاد طريقه اطلع فيها رقم الاي دي  بعد عمليه ال INSERT

هذا الكود

كود :
       conx = New SQLiteConnection(String.Format("Data Source= C:\LOG\{0}\DB{1}.s3db", strcallbk,test))
       cmdx = New SQLiteCommand("INSERT INTO DBQTC (station, UTC) Values (@station,@UTC") With {
           .Connection = conx
       }

 conx.Open()
           cmdx.Parameters.AddWithValue("@station", strcallbk)
           cmdx.Parameters.AddWithValue("@UTC", timk)

       cmdx.ExecuteNonQuery()
         conx.Close()


محتاج بعد الانزال يطلع لي رقم الاي دي ID  لهذا الادخال

وشكرا بارك الله فيكم
السلام عليكم اخي الكريم
ان كنت تريد رقم id للادخال الذي ادهلته للتو بعد كود الحفظ
فقط استخدم  كود max id مع انقاص رقم واحد
قم بانشاء  label وضع له هذا الكود
ولتكن هكذا


كود :
Label1.Text = (MaxID("اسم الجدول").Rows(0)(0)) - 1




اما ان كنت تقصد max id فلا تنقص منه سيأتي بأخر قيمة +1
كود :
Txt_id.Text = (MaxID_("اسم الجدول").Rows(0)(0))
بعد تعديل الكود أصبح كالتالي :

كود :
conx = New SQLiteConnection(String.Format("Data Source= C:\LOG\{0}\DB{1}.s3db", strcallbk,test))
cmdx = New SQLiteCommand("INSERT INTO DBQTC (station, UTC) Values (@station,@UTC") With {
.Connection = conx}

conx.Open()
cmdx.Parameters.AddWithValue("@station", strcallbk)
cmdx.Parameters.AddWithValue("@UTC", timk)
cmdx.ExecuteNonQuery()

'------------------------------------
cmdx.CommandText = "SELECT SCOPE_IDENTITY()"
Dim NewID As Integer
NewID = cmdx.ExecuteScalar()
'------------------------------------

conx.Close()

الآن المتغير NewID سيحمل قيمة رقم الـ ID الجديد

الإستعلام : SELECT SCOPE_IDENTITY
يقوم بإرجاع آخر رقم ID تم إنشائة 

كما يمكنك إستخدام : SELECT @@IDENTITY




هذا هو الفرق بين SCOPE_IDENTITY وبين IDENTITY@@

SELECT @@IDENTITY
It returns the last IDENTITY value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value.
@@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope. If you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.


SELECT SCOPE_IDENTITY()
It returns the last IDENTITY value produced on a connection and by a statement in the same scope, regardless of the table that produced the value.

SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function.



(15-09-18, 12:19 AM)ابراهيم ايبو كتب : [ -> ]السلام عليكم اخي الكريم
ان كنت تريد رقم id للادخال الذي ادهلته للتو بعد كود الحفظ
فقط استخدم  كود max id مع انقاص رقم واحد
قم بانشاء  label وضع له هذا الكود
ولتكن هكذا


كود :
Label1.Text = (MaxID("اسم الجدول").Rows(0)(0)) - 1




اما ان كنت تقصد max id فلا تنقص منه سيأتي بأخر قيمة +1
كود :
Txt_id.Text = (MaxID_("اسم الجدول").Rows(0)(0))

تسلم عزيزي على الرد

بس ما اللي اقصده يختلف عن اللي كتبته عزيزي 
بارك الله فيك

(15-09-18, 12:38 AM)عبدالله الدوسري كتب : [ -> ]بعد تعديل الكود أصبح كالتالي :

كود :
conx = New SQLiteConnection(String.Format("Data Source= C:\LOG\{0}\DB{1}.s3db", strcallbk,test))
cmdx = New SQLiteCommand("INSERT INTO DBQTC (station, UTC) Values (@station,@UTC") With {
.Connection = conx}

conx.Open()
cmdx.Parameters.AddWithValue("@station", strcallbk)
cmdx.Parameters.AddWithValue("@UTC", timk)
cmdx.ExecuteNonQuery()

'------------------------------------
cmdx.CommandText = "SELECT SCOPE_IDENTITY()"
Dim NewID As Integer
NewID = cmdx.ExecuteScalar()
'------------------------------------

conx.Close()

الآن المتغير NewID سيحمل قيمة رقم الـ ID الجديد

الإستعلام : SELECT SCOPE_IDENTITY
يقوم بإرجاع آخر رقم ID تم إنشائة 

كما يمكنك إستخدام : SELECT @@IDENTITY




هذا هو الفرق بين SCOPE_IDENTITY وبين IDENTITY@@

SELECT @@IDENTITY
It returns the last IDENTITY value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value.
@@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope. If you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.


SELECT SCOPE_IDENTITY()
It returns the last IDENTITY value produced on a connection and by a statement in the same scope, regardless of the table that produced the value.

SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function.




تسلم على الرد بارك الله فيك 
طلع لي مسج غلط

[attachment=19498]
SELECT ID FROM TABLE_NAME ORDER BY ID DESC LIMIT 1
(15-09-18, 12:46 AM)dubai.eig كتب : [ -> ]تسلم على الرد بارك الله فيك 
طلع لي مسج غلط



أعتذر لأنني أعتقدت أنها SQL

على العموم إستخدم هذة بدلاً عنها : 

SELECT last_insert_rowid()
(15-09-18, 01:24 AM)عبدالله الدوسري كتب : [ -> ]
(15-09-18, 12:46 AM)dubai.eig كتب : [ -> ]تسلم على الرد بارك الله فيك 
طلع لي مسج غلط



أعتذر لأنني أعتقدت أنها SQL

على العموم إستخدم هذة بدلاً عنها : 

SELECT last_insert_rowid()

نعم ضبط معاي شكرا لك بارك الله فيك

الحمدالله تسلم على وقتك

(15-09-18, 12:58 AM)أبوبكر سويدان كتب : [ -> ]SELECT ID FROM TABLE_NAME ORDER BY ID DESC LIMIT 1

شكرا استاذي ابوبكر الحمدالله حصلت الجل في رد استاذي عبدالله شكرا لك
وعليكم السلام ورحمة الله وبركاته

مشاء الله الاخوة لم تقصر بالحل
وليس هذا فقط بال تواجد كلاً من 

[*] عبدالله الدوسري
[*] أبوبكر سويدان

عودة حميدة يارب


حتى الاخ ابراهيم ايبو اتى بالرد المناسب ايضاً

لكن يبدو انه لم تصل فكرته لك فكل الشكر والتقدير للمساعدة فى الرد

واتمنى ان تكون دائماً وان لا يتوقف بما يعلمه للغير حتى تعم العلم والائدة للجميع


فكل الشكر والتقدير للجميع


تحياتى لكم

وتمنياتى لكم التوفيق الدائم