gpt4 book ai didi

c# - FormatException 错误字符串到日期时间

转载 作者:太空宇宙 更新时间:2023-11-03 12:25:04 29 4
gpt4 key购买 nike

我已经在座位上坐了一个多小时了,不知道出了什么问题。有人可以帮忙吗?

Error

错误据说是“将字符串转换为 DateTime 时,在将每个变量放入 DateTime 对象之前解析字符串以获取日期。

字段名称“LastLoginTime”是我数据库中的 DATETIME 数据类型。

这些是代码..

protected void Page_Load(object sender, EventArgs e)
{
AuditNLoggingDAO al = new AuditNLoggingDAO();

int result = 0;

int resultLogout = 0;

DateTime dateTimeOfLatestLogin = DateTime.MinValue;

//Get IP Address of Client's Machine
String externalIP = null;
try
{
externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")).Matches(externalIP)[0].ToString();
}
catch (Exception ex)
{
logManager log = new logManager();
log.addLog("Retrieval of IP Address", "IP Address", ex);
}

if (!Page.IsPostBack)
{
if (!String.IsNullOrEmpty(Session["LoginUserName"].ToString()))
{
String loginUsername = Session["LoginUserName"].ToString();

//Get latest Login time
DataSet ds = new DataSet();

ds = al.getAuditData(Session["LoginUserName"].ToString());

foreach (DataRow r in ds.Tables[0].Rows)
{
dateTimeOfLatestLogin = Convert.ToDateTime(r["LastLoginTime"]);
}

result = al.trackLogout(loginUsername, DateTime.Now, externalIP, Convert.ToDouble(latitudeTB.Value), Convert.ToDouble(longitudeTB.Value));
resultLogout = al.updateLLogoutT(loginUsername, DateTime.Now, externalIP);
}

loginDetails.InnerText = "You logged into your account at " + dateTimeOfLatestLogin.ToString("hh:mm:ss tt dd/MM/yyyy") + " SGT.";
logoutDetails.InnerText = "You logged out from your session at " + (DateTime.Now).ToString("hh:mm:ss tt dd/MM/yyyy") + " SGT.";
}
}

我似乎找不到错误..我猜这是我的 dateTimedateTimeOfLatestLogin 变量..

al.trackLogout 方法,

//Track Logout Activity
public int trackLogout(String username, DateTime dateTimeActivity, String ipaddress, Double latitude, Double longitude)
{
int result = 0;

StringBuilder sqlCmd = new StringBuilder();
sqlCmd.AppendLine("INSERT INTO AuditActivity (Username, DateTimeActivity, IPAddressActivity, LatitudeActivity, LongitudeActivity, ActivityType) ");
sqlCmd.AppendLine("VALUES (@addUsername, @addDT, @addIPAddress, @addLat, @addLng, @addActivity)");

try
{
SqlConnection myConn = new SqlConnection(DBConnectionStr);

myConn.Open();

SqlCommand cmd = new SqlCommand(sqlCmd.ToString(), myConn);

cmd.Parameters.AddWithValue("@addUsername", username);
cmd.Parameters.AddWithValue("@addDT", dateTimeActivity);
cmd.Parameters.AddWithValue("@addIPAddress", ipaddress);
cmd.Parameters.AddWithValue("@addLat", latitude);
cmd.Parameters.AddWithValue("@addLng", longitude);
cmd.Parameters.AddWithValue("@addActivity", "Logout");

result = cmd.ExecuteNonQuery();

myConn.Close();

return result;
}
catch (SqlException ex)
{
logManager log = new logManager();
log.addLog("AuditNLoggingDAO.trackLogout", sqlCmd.ToString(), ex);
return 0;
}
}

最佳答案

不是 C# 但没有打开的 C# 项目可以写入,所以它在 VB.Net 中;较小的语法调整和更改,但其他方面相同:

Public Function trackLogout(username As String, dateTimeActivity As DateTime, ipaddress As String, latitude As Double, longitude As Double) As Integer
Dim result As Integer = 0

Try
Using conn As New SqlConnection(DBConnectionStr)
Dim sb As New StringBuilder
sb.AppendLine("INSERT INTO AuditActivity (Username, DateTimeActivity, IPAddressActivity, LatitudeActivity, LongitudeActivity, ActivityType) ")
sb.AppendLine("VALUES (@addUsername, @addDT, @addIPAddress, @addLat, @addLng, @addActivity)")

Using cmd As New SqlCommand() With {.CommandText = sb.ToString(), .Connection = conn, .CommandType = CommandType.Text}
cmd.Parameters.AddWithValue("@addUsername", username)
cmd.Parameters.AddWithValue("@addDT", dateTimeActivity)
cmd.Parameters.AddWithValue("@addIPAddress", ipaddress)
cmd.Parameters.AddWithValue("@addLat", latitude)
cmd.Parameters.AddWithValue("@addLng", longitude)
cmd.Parameters.AddWithValue("@addActivity", "Logout")

result = cmd.ExecuteNonQuery
End Using
End Using
Catch ex As Exception
result = -1
' Whatever your current implementation is.
End Try

Return result
End Function

不幸的是,执行内联sql需要StringBuilder。你是对的。我使用存储过程来执行结构化 SQL 语句,因此我不必在应用程序中管理 SQL。

关于c# - FormatException 错误字符串到日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45417840/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com