【问题标题】:get only date from sql仅从 sql 获取日期
【发布时间】:2014-04-21 17:31:07
【问题描述】:

我只需要从 SQL 中获取日期部分。 SQL 数据类型是日期,但当我尝试调用它时,它返回此值01-Mar-14 12:00:00 AM。我唯一感兴趣的部分是01-Mar-14 我在stackoverflow、微软和谷歌中进行了长时间的搜索,但徒劳无功。 First search didn't work with me
also this one didn't work out
这是我获取这些值的 C# 代码。

private void SearchName_Click(object sender, EventArgs e)     
{                
      db.con.Open();
      SqlDataReader reader;
      SqlCommand command = new SqlCommand("select * from Employee inner join DatePics on ID=Emp_ID where EmpName='" + SearNametxt.Text + "' ", db.con);                
      reader = command.ExecuteReader();
      while (reader.Read())
      {
           NameSeartxt.Text = (reader["EmpName"].ToString());                   
           PassSeartxt.Text = (reader["Passport"].ToString());               
           IDSeartxt.Text = (reader["IDIssue"].ToString());
           expiryseartxt.Text = (reader["IDExpiry"].ToString());                  
      }
      db.con.Close();
}

但也只返回整个格式。试过用这个,但是失败了。
select convert(varchar(10), '01-Mar-14 12:00:00', 120)

编辑:最新代码:

db.con.Open();
SqlDataReader reader; 
SqlCommand command = new SqlCommand("select * from Employee inner join 
     DatePics on ID=Emp_ID where EmpName='" + SearNametxt.Text + "' ", db.con);
reader = command.ExecuteReader(); 
while (reader.Read()) 
{
 DateTime Received; 
 DateTime.TryParse(reader["ReceivedDate"], CultureInfo.InvariantCulture,  
                                    DateTimeStyles.None, out Received); 
 recsearchtxt.Text = Received.ToString("d");

【问题讨论】:

    标签: c# sql sql-server date


    【解决方案1】:

    要获取日期,请使用带有格式规范的convert()

    select *, convert(varchar(10), datecol, 121) as datestr
    from Employee inner join
         DatePics
         on ID = Emp_ID
    where EmpName='" + SearNametxt.Text + "' ";
    

    这会将其转换为 YYYY-MM-DD。

    【讨论】:

    • 还是一样。这是我使用的语句 `SqlCommand command = new SqlCommand("select *, convert (varchar(10), ReceivedDate,121) as datestr from Employee inner join DatePics on ID=Emp_ID where EmpName='" + SearNametxt.Text + "'", db.con); `
    • 那么问题出在应用层,Karl 的回答可能有效。
    【解决方案2】:

    您应该能够执行以下操作:

    DateTime issueDate;
    DateTime.TryParse(reader["IDIssue"].ToString(), out issueDate);
    IDSeartxt.Text = issueDate.ToString("d");
    

    虽然您需要使用正确的格式参数。

    这是fiddle

    【讨论】:

    • error 'System.DateTime.TryParse(string, Out System.DateTieme)' 的最佳重载方法匹配有一些无效参数。 `
    • @Sudhakar Tillapudi:唯一的一个是CultureInfoConverter
    • @BillG: 你需要导入 System.Globalization 命名空间,导入这个 -> using System.Globalization;
    • @Sudhakar Tillapudi:DateTime Received; DateTime.TryParse(reader["ReceivedDate"], out Received,CultureInfo.InvariantCulture); recsearchtxt.Text = Received.ToString("d"); 错误`方法“Tryparse”没有重载需要 3 个参数`
    • @BillG:试试这个:DateTime.TryParse(reader["IDIssue"], CultureInfo.InvariantCulture, DateTimeStyles.None, out issueDate)
    【解决方案3】:

    您需要将第一个参数作为字符串传递,因此使用reader["ReceivedDate"].ToString()reader["ReceivedDate"] 转换为字符串

    试试这个:

    DateTime Received;
    DateTime.TryParse(reader["ReceivedDate"].ToString(),
                  CultureInfo.InvariantCulture, DateTimeStyles.None, out Received);
    

    【讨论】:

    • @BillG:你能用最新的代码编辑你的问题吗?
    • db.con.Open(); SqlDataReader reader; SqlCommand command = new SqlCommand("select * from Employee inner join DatePics on ID=Emp_ID where EmpName='" + SearNametxt.Text + "' ", db.con); reader = command.ExecuteReader(); while (reader.Read()) { DateTime Received; DateTime.TryParse(reader["ReceivedDate"], CultureInfo.InvariantCulture, DateTimeStyles.None, out Received); recsearchtxt.Text = Received.ToString("d");
    • @BillG:第一个参数应该是字符串,检查我修改后的答案
    • @SudhakarTillapudi:感谢您关注我最初(不完整)的回答。 TryParse 的这种重载比我最初脱口而出的两个步骤要好。
    • @KarlKieninger:如果您可以在阅读器数据上使用ToString(),我认为您的方法也很好,但是正如您所说,InvariantCulture 过载至少在处理日期时比正常的 2 参数方法要好 :)
    【解决方案4】:

    试试:

    expiryseartxt.Text = (reader["IDExpiry"].ToShortDateString());  
    

    或者这个:

    expiryseartxt.Text = reader.GetDateTime().ToShortDateString();
    

    【讨论】:

      猜你喜欢
      • 2013-08-05
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 2016-03-24
      相关资源
      最近更新 更多