Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

asp.net - How to get the client IP address from SQL Server 2008 itself?

I have a trigger for insert/update/delete. That is working fine. Also, I need the client's IP address from where the changes are made. That I need in T-SQL, that means, not in any web form but in the SQL/T-SQL while my trigger will get fired.

Also I go-ogled, and got that there is stored procedure in master database named xp_cmdshell which when executed with ipconfig we can get the IP Address. I think this will work only when you have administrative access to database. Mine hosting is a shared hosting , so I don't have such privilege. Is there any other way out?

Please note: I don't have administrative privileges on my SQL Server 2008 database. I need a solution as an authenticated user.

Another update:

I have got the solution, the query that will work for my scenario is

SELECT hostname, net_library, net_address
FROM sys.sysprocesses 
WHERE spid = @@SPID

It executes as needed but there is only one issue, that net_address is not in IP format. below is mine result:

hostname    net_library     net_address
IPC03       TCP/IP          AE8F925461DE  

I am eager to know:

  1. What is net_address here? Is is MAC Address or Some IP address etc?

  2. Is there any way to convert net_address to ip?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found something which might work for you

CREATE FUNCTION [dbo].[GetCurrentIP] ()
RETURNS varchar(255)
AS
BEGIN
    DECLARE @IP_Address varchar(255);

    SELECT @IP_Address = client_net_address
    FROM sys.dm_exec_connections
    WHERE Session_id = @@SPID;

    Return @IP_Address;
END

From How to get Client IP Address in SQL Server

Also have a look at this article about Get client IP address


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...