Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to get current user of the system

What I have tried:

query = "insert into AllottedLicense  (Department , AllottedLicense,Server,DateAllotted,CreatedBy) values(@Department,@AllottedLicense,@Server,@DateAllotted,USER_NAME())";
Posted
Updated 16-Mar-21 22:24pm
Comments
Maciej Los 17-Mar-21 4:16am    
What system? Database (server) system or loacal computer system?
Member 15088142 17-Mar-21 4:25am    
local computer system

1 solution

Your query should do the job!

USER_NAME() and CURRENT_USER should return the same, the name of user, which currently executes the statement.

More at: CURRENT_USER (Transact-SQL) - SQL Server | Microsoft Docs[^]

In case you wanted to get other answer, you need to explain what's worng with your code or what you want to achieve...

[EDIT]

Maciej Los wrote:

What system? Database (server) system or loacal computer system?
Member 15088142 wrote:

local computer system


To get local computer user name, use:
C#
string currUser = Environment.UserName;

and pass it as a parameter into command ;)
 
Share this answer
 
v2
Comments
Member 15088142 17-Mar-21 4:28am    
if (row["Department"].ToString() != null && row["AllottedLicense"].ToString() != null)
{
OleDbDataAdapter _oda = new OleDbDataAdapter();
query = "insert into AllottedLicense (Department , AllottedLicense,Server,DateAllotted,CreatedBy) values(@Department,@AllottedLicense,@Server,@DateAllotted,CURRENT_USER())";
//query = "insert into AllottedLicense ALT (Department , AllottedLicense,Server,DateAllotted) values(@Department,@AllottedLicense,@Server,@DateAllotted)'" +
// " INNER JOIN DetailUsage DU ON ALT.CreatedBy = DU.CreatedBy";
_cmd = new OleDbCommand(query, _cnn);
_cmd.Parameters.AddWithValue("@Department", row["Department"].ToString());
_cmd.Parameters.AddWithValue("@AllottedLicense", row["AllottedLicense"].ToString());
_cmd.Parameters.AddWithValue("@Server", row["Server"].ToString());
_cmd.Parameters.AddWithValue("@DateAllotted", dtn);
//_cmd.Parameters.AddWithValue("@CreatedBy", row["CreatedBy"].ToString());
_oda.SelectCommand = _cmd;
_oda.Fill(resultDt);
}
}


this is my code i want to updated created bu column if user updates anything in created by column username name as to store
Maciej Los 17-Mar-21 4:38am    
See updated answer.
Maciej Los 17-Mar-21 4:53am    
Please, read my answer carefully...
Do NOT post the same content in comments. It's rude!
Member 15088142 17-Mar-21 4:54am    
if system is a Database (server) system, how i can get
Maciej Los 17-Mar-21 4:57am    
The second statement of my answer is:
USER_NAME() and CURRENT_USER should return the same, the name of user, which currently executes the statement.
What's unclear here?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900