【问题标题】:Insert only time in sql table from c#仅在 c# 的 sql 表中插入时间
【发布时间】:2013-08-16 11:46:09
【问题描述】:

我有一个 Windows 应用程序,我在其中使用日期时间选择器进行班次开始和班次结束。我必须将这些时间插入表中。我的代码是:

if (comboBox1.SelectedIndex == 0 || comboBox1.SelectedIndex == 1 || comboBox1.SelectedIndex == 2 || comboBox1.SelectedIndex == 3 || comboBox1.SelectedIndex == 4 || comboBox1.SelectedIndex == 5 || comboBox1.SelectedIndex == 6 || comboBox1.SelectedIndex == 7 || comboBox1.SelectedIndex == 8 || comboBox1.SelectedIndex == 9 || comboBox1.SelectedIndex == 10 || comboBox1.SelectedIndex == 11 || comboBox1.SelectedIndex == 12 || comboBox1.SelectedIndex == 13 || comboBox1.SelectedIndex == 14 || comboBox1.SelectedIndex == 15 || comboBox1.SelectedIndex == 16)
{
    string Query = " Insert into [ICPS].[dbo].[Cau reports]( [Name],[Date],[Shift Start],[Shift END ],[Overtime Start],[Overtime End],[Tasks Carried Out],[Normal ],[Overtime],[Normal 1],[Overtime1],[Comments],[Targets_(per hour)]) values ('" + this.textBox1.Text + "','" + this.Date.Text + "', CONVERT(VARCHAR(5),'" + this.SS.Text + "',108), '" + this.SE.Text + "','" + this.OS.Text + "','" + this.OE.Text + "','" + this.comboBox1.SelectedText + "','" + this.NT.Text + "','" + this.OT.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.textBox4.Text + "','" + this.textBox5.Text + "' ) ;";

    // string Query = " update [ICPS].[dbo].[Cau reports] set  [Normal]='" + this.textBox7.Text + "',[Overtime]='" + this.textBox8.Text + "',[Normal 1]='" + this.textBox2.Text + "',[Overtime1]='" + this.textBox3.Text + "',[comments]='" + this.textBox4.Text + "',[Targets_(per hour)]='" + this.textBox5.Text + "'where  [Tasks Carried Out] ='" + this.comboBox1.SelectedText + "'; ";

    SqlConnection conDatabase = new SqlConnection(constring);
    SqlCommand cmdDataBase = new SqlCommand(Query, conDatabase);
    SqlDataReader myReader;

    try
    {
        conDatabase.Open();
        myReader = cmdDataBase.ExecuteReader();
        MessageBox.Show("Saved");

        while (myReader.Read())
        {
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

但我的输出是:01/01/1900 16:36(我不需要轮班开始和轮班结束的日期,只需要时间)。任何帮助将不胜感激。请...

【问题讨论】:

  • 您使用什么数据类型将时间存储在基础 Cau_reports 表中?
  • 另外,有点跑题了,但我建议你考虑使用 SqlCommand 和参数
  • 是的,参数化查询。除了避免 SQL 注入之外,您还可以避免各种麻烦,例如注释字段中的单引号,或客户端计算机和 SQL 之间的日期/时间格式不匹配。

标签: c# sql sql-server datetime


【解决方案1】:

确保您为数据列使用适当的 SQL Server time 类型。

【讨论】:

  • 我在 sql 中有日期时间类型,在我的 Windows 应用程序表单中我有日期时间选择器,格式为时间,所以我有时间从他们那里得到.. CONVERT(VARCHAR(5),'" + this.SS .Text + "',108) = for shift start 给了我这个 o/p 01/01/1900 16:36
  • @kiranv:如果您在 SQL Server 表中有DATETIME,那么您将总是同时拥有日期和时间。你无法避免这一点。如果您只需要时间,请使用适当的数据类型 - TIME(3) 或类似的东西!
  • 嗨,马克,你是对的.. 但是 sql server managament studio 没有我首先要寻找的时间数据类型。
【解决方案2】:

试试这个

CONVERT(VARCHAR(8),'" + this.SS.Text + "',108)

108 返回hh:mm:ss

【讨论】:

    【解决方案3】:
    cast( '" + this.SS.Text + "' as time)
    

    convert(char(5), '" + this.SS.Text + "', 108)
    

    CONVERT(VARCHAR(8),'" + this.SS.Text + "',108)
    

    CONVERT(TIME,'" + this.SS.Text + "') 
    

    在查询中使用它之前只需转换您的 datetimepicker 值,如下所示

    DateTimePicker1.Value.ToShortTimeStringDateTimePicker1.Value.tostring("HH:mm:ss")

    【讨论】:

    • 在查询中使用它之前,您是否尝试过更改 datetimepicker 值。请查看我的更新。
    • 谁能知道我的查询有什么问题。
    【解决方案4】:

    如果您使用的是 SQL Server 2008 或更高版本,则可以使用 TIME 数据类型来存储时间。如果您使用的是早期版本 - 您的最佳选择是 DATETIME 数据时间,其中包含您可以忽略的日期组件。无论消耗什么,您的数据总是可以将其显示为时间而忽略日期。

    另一种选择是将时间存储为字符串,但如果您需要进行任何比较或日期数学运算,这可能比它的价值更麻烦。

    【讨论】:

    • 我看不到时间数据类型,除了(时间戳),这与我的情况无关方法,但仍然没有运气。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    相关资源
    最近更新 更多