简体   繁体   English

将时间发送到SQL Server Management Studio表,但不显示吗?

[英]Sending time to SQL Server Management Studio table but it does not display?

I am currently writing an application which involves a user being able to write the time to a database by clicking a button. 我当前正在编写一个应用程序,其中涉及用户能够通过单击按钮将时间写入数据库。 The problem is that the data will be send to the database table, but it does not show the time in SQL Server Management Studio. 问题在于数据将被发送到数据库表,但是在SQL Server Management Studio中却没有显示时间。

This is my query: 这是我的查询:

{
    string query = "insert into Sign_In_Out_Table(Sign_In)Values('"+ timetickerlbl.ToString()+ "')";
    SqlCommand cmd = new SqlCommand(query, con);
    cmd.Parameters.AddWithValue("@SignIn", DateTime.Parse (timetickerlbl.Text));
    //cmd.ExecuteNonQuery();
    MessageBox.Show("Signed in sucessfully" +timetickerlbl);
    con.Close();
}

The datatype in SQL Server is set to datetime . SQL Server中的数据类型设置为datetime

I'm open for suggestions to find a better way to capture the PC's time and logging it in a database. 我愿意征求建议,以找到一种更好的方式来捕获PC时间并将其记录在数据库中。

  1. Don't wrap the variable in ' when you are setting value with Parameters.Add() , or Parameters.AddWithValue() as they would wrap if needed. 在使用Parameters.Add()Parameters.AddWithValue()设置值时,请勿将变量包装在' ,因为如果需要,可以将它们包装起来。

  2. The variable in here would be the value of Sign_In and not the Sign_In itself. 这里的变量将是Sign_In值,而不是Sign_In本身的值。

  3. Always use Parameters.Add() instead of Parameters.AddWithValue() : 始终使用Parameters.Add()而不是Parameters.AddWithValue()

     string query = "insert into Sign_In_Out_Table(Sign_In) Values(@value)"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.Add("@value", SqlDbType.DateTime).Value = DateTime.Parse(timetickerlbl.Text); 

Edit (Considering your comment): 编辑(考虑您的评论):

If still it does not insert it, of course there is an error in your code, it could be a syntax error, invalid table or column name, connection problem ,... so put your code in a try-catch block (if it isn't already) and see what error you you get, it should give you a hint: 如果仍然没有插入,则代码中当然会有错误,可能是语法错误,无效的表或列名,连接问题...。因此,请将代码放在try-catch块中(如果它尚未),看看您遇到什么错误,它应该给您一个提示:

try
{
    //the lines of code for insert
}
catch(Exception ex)
{
    string msg = ex.Message;
   // the above line you put a break point and see if it reaches to the break point, and what the error message is.
}

Your table does not contain your timestamp because you have commented the execution of your query. 该表不包含您的时间戳,因为您已注释了查询的执行。 Presumably you added the comment because this line was throwing an error, remove the comment and share the error with us. 大概是因为该行引发了错误,所以添加了评论,请删除评论并与我们分享错误。

cmd.ExecuteNonQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM