I have a server with a different datetime format from my client. In my client its using PM / AM and my server is using format 24H. When I try using a DateTime
variable in C# and datatype DateTime
in my SQL Server. In my SQL Server 2020-06-16 14:54:33.937 When I try run in server PC its OK. but when I try run in client my date in SQL Server it returns int32
(different format).
I use dapper for SQL Server helper. This is my function to select my table
private static void get_chat()
{
ConnectDB();
string sql = "SELECT * FROM chat where to_user = " + MyLogin.id + " OR from_user = " + MyLogin.id;
var queryResult = cnn.Query<SQLServer_Chat>(sql);
SQLServer_Olah.Olah_Chat(queryResult);
ArrayList list_id = SQLServer_Olah.List_chat_id(queryResult);
CloseDB();
get_chat_det(list_id);
}
and this is my SQLServer_Chat
class SQLServer_Chat
{
public int id_chat { set; get; }
public int to_user { set; get; }
public int from_user { set; get; }
public string last_chat { set; get; }
public int to_unread { set; get; }
public int from_unread { set; get; }
public DateTime created_at { set; get; }
public DateTime last_chat_time { set; get; }
public string to_name { set; get; }
public string from_name { set; get; }
}
hope anyone can help me about this bug , thank you!!
Since you say the column is datetime
in the database, frankly: format doesn't exist. I expect the problem is that you are saving it incorrectly, because you aren't using parameters. The query shown should really, really be:
var queryResult = cnn.Query<SQLServer_Chat>(
"SELECT * FROM chat where to_user = @id OR from_user = @id", new { MyLogin.id });
The anonymous type (new { ... }
) tells Dapper to add a parameter called id
with the type and value that it gets from evaluating MyLogin.id
.
If we assume that your save code also didn't use parameters: then - the data in the database is now simply incorrect. The solution: use parameters; this fixes: