is there no way i could retrieve value from a trigger variable to my
dataset
i mean ive been on it like a hound for two days and not a single
article or post that really helped me.
heres what i have understood till now.
ALTER TRIGGER lastserial
ON dbo.master
FOR INSERT
AS
begin
select serialcode = @.@.IDENTITY
end
now how do i retrieve this serialcode value to my application in vb.netHere is one way:
=====
CREATE TABLE Foo
(
colA VARCHAR(10)
)
GO
CREATE TRIGGER FooTrigger ON Foo
FOR INSERT AS
BEGIN
SELECT 'Srinivas'
END
GO
=====
The above creates a SQL Server table and a trigger on the same. Now, here is
the C#.NET code that puts a record into the table and reads the output of
the trigger into the application code and prints it out. The example uses
.NET 1.1 version
=====
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
using (SqlConnection oConn = new
SqlConnection("Server=tl- devdb\\matrix;Database=pubs;Uid=sa;Pwd=p
@.ssw0rd"))
{
SqlCommand oCmd = new SqlCommand();
SqlDataReader rdr;
oCmd.Connection = oConn;
oCmd.CommandType = CommandType.Text;
oCmd.CommandText = "INSERT INTO Foo VALUES ('Sampath')";
oConn.Open();
try
{
rdr = oCmd.ExecuteReader();
while (rdr.Read())
Console.WriteLine("{0}", rdr.GetString(0));
rdr.Close();
}
catch (SqlException e)
{
Console.WriteLine (e.Message);
}
finally
{
oConn.Close();
}
Console.Read();
}
}
}
}
=====
--
HTH,
SriSamp
Email: srisamp@.gmail.com
Blog: http://blogs.sqlxml.org/srinivassampath
URL: http://www32.brinkster.com/srisamp
<prabodhtiwari@.gmail.com> wrote in message
news:1140425436.598510.175750@.f14g2000cwb.googlegroups.com...
> is there no way i could retrieve value from a trigger variable to my
> dataset
> i mean ive been on it like a hound for two days and not a single
> article or post that really helped me.
> heres what i have understood till now.
> ALTER TRIGGER lastserial
> ON dbo.master
> FOR INSERT
> AS
> begin
> select serialcode = @.@.IDENTITY
> end
> now how do i retrieve this serialcode value to my application in vb.net
>|||but how do you retrieve the value 'sampath' only using the datareader|||I thought your question was to retrieve what the trigger was SELECTing into,
which is what this example shows.
--
HTH,
SriSamp
Email: srisamp@.gmail.com
Blog: http://blogs.sqlxml.org/srinivassampath
URL: http://www32.brinkster.com/srisamp
"SriSamp" <ssampath@.sct.co.in> wrote in message
news:%23m7Nc4fNGHA.1460@.TK2MSFTNGP10.phx.gbl...
> Here is one way:
> =====
> CREATE TABLE Foo
> (
> colA VARCHAR(10)
> )
> GO
> CREATE TRIGGER FooTrigger ON Foo
> FOR INSERT AS
> BEGIN
> SELECT 'Srinivas'
> END
> GO
> =====
> The above creates a SQL Server table and a trigger on the same. Now, here
> is the C#.NET code that puts a record into the table and reads the output
> of the trigger into the application code and prints it out. The example
> uses .NET 1.1 version
> =====
> using System;
> using System.Data;
> using System.Data.SqlClient;
> namespace ConsoleApplication1
> {
> class Class1
> {
> [STAThread]
> static void Main(string[] args)
> {
> using (SqlConnection oConn = new
> SqlConnection("Server=tl- devdb\\matrix;Database=pubs;Uid=sa;Pwd=p
@.ssw0rd")
)
> {
> SqlCommand oCmd = new SqlCommand();
> SqlDataReader rdr;
> oCmd.Connection = oConn;
> oCmd.CommandType = CommandType.Text;
> oCmd.CommandText = "INSERT INTO Foo VALUES ('Sampath')";
> oConn.Open();
> try
> {
> rdr = oCmd.ExecuteReader();
> while (rdr.Read())
> Console.WriteLine("{0}", rdr.GetString(0));
> rdr.Close();
> }
> catch (SqlException e)
> {
> Console.WriteLine (e.Message);
> }
> finally
> {
> oConn.Close();
> }
> Console.Read();
> }
> }
> }
> }
> =====
> --
> HTH,
> SriSamp
> Email: srisamp@.gmail.com
> Blog: http://blogs.sqlxml.org/srinivassampath
> URL: http://www32.brinkster.com/srisamp
> <prabodhtiwari@.gmail.com> wrote in message
> news:1140425436.598510.175750@.f14g2000cwb.googlegroups.com...
>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment