PDA

View Full Version : استخراج ID به وسيله SP



mr_mtc
دوشنبه 24 تیر 1387, 09:48 صبح
اول بگم گشتم پيدا نكردم

من ميخوام ID ركورد جديد رو كه با SP در SQL ثبت مي كنم، بدست بيارم؟

قبلا كدها رو تو برنامه مينوشتم از دستور @@IDENTITY استفاده ميكردم، البته بگم كه من تو استفاده از SP تازه كارم (بيسوادم)

Xcalivorse
دوشنبه 24 تیر 1387, 11:26 صبح
برای این کار می تونی از Trigger ها و جدول Inserted استفاده کنی.

mehranFX
دوشنبه 24 تیر 1387, 11:36 صبح
IDENT_CURRENT

Returns the last identity value generated for a specified table in any session and any scope.
Syntax

IDENT_CURRENT('table_name')
Arguments

table_name
Is the name of the table whose identity value will be returned. table_name is varchar, with no default.
Return Types

sql_variant
Remarks

IDENT_CURRENT is similar to the Microsoft® SQL Server™ 2000 identity functions SCOPE_IDENTITY and @@IDENTITY. All three functions return last-generated identity values. However, the scope and session on which 'last' is defined in each of these functions differ.


IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.
@@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.
SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.

Examples

This example illustrates the different identity values returned by IDENT_CURRENT, @@IDENTITY, and SCOPE_IDENTITY.
USE pubs
DROP TABLE t6
DROP TABLE t7
GO
CREATE TABLE t6(id int IDENTITY)
CREATE TABLE t7(id int IDENTITY(100,1))
GO
CREATE TRIGGER t6ins ON t6 FOR INSERT
AS
BEGIN
INSERT t7 DEFAULT VALUES
END
GO
--end of trigger definition

bermouda
پنج شنبه 03 مرداد 1387, 12:05 عصر
دو مرحله در راه داری:

1- Sp on Sql : ( ساخت پروسیجر در SQL )


create procedure select_id_name
@name (Jense Meghdariye Field ) exp : @name nvarchar(50)
as
select id From "Database Table Name" where name = @name

2- C#.net : ( استفاده پروسیجر و نمایش داده استخراج شده در یک لیبل )


SqlConnection cnn = newSqlConnection(Connection String);
cnn.Open();
SqlCommand cmd = newSqlCommand("select_id_name", cnn);
cmd.Parameters.Add("@name", txtsearch.Text);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da;
da = newSqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds;
ds = newDataSet();
da.Fill(ds, "name");
lable1.Text = ds.Tables["name"].Rows[0]["id"].ToString();
cnn.Close();