Hi tsakumar81,
Refer below example.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
Function
CREATE FUNCTION [dbo].[ufnGetLastUpdatedDate]
(
@TableName varchar(100)
)
RETURNS DATETIME
AS
BEGIN
DECLARE @LastUpdated DATETIME
SELECT @LastUpdated = last_user_update
FROM sys.dm_db_index_usage_stats
WHERE OBJECT_ID=OBJECT_ID(@TableName)
RETURN @LastUpdated
END
GO
Namespaces
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "[ufnGetLastUpdatedDate]";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@TableName", "Customers");
SqlParameter sqlParameter = cmd.Parameters.Add("@TableName", SqlDbType.VarChar);
sqlParameter.Direction = ParameterDirection.ReturnValue;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
ViewBag.Message = sqlParameter.Value;
}
}
return View();
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<span>Last updated date: </span>@ViewBag.Message
</body>
</html>
Output
8/19/2023 1:57:09 PM