C# 如何在数据库C中只插入日期#
//我有这个代码,我只想在数据库中插入日期,而不插入//时间,但这将始终像这样存储值:2019-10-19 00:00:00.000C# 如何在数据库C中只插入日期#,c#,sql,C#,Sql,//我有这个代码,我只想在数据库中插入日期,而不插入//时间,但这将始终像这样存储值:2019-10-19 00:00:00.000 SqlCommand cmd2 = conn.CreateCommand(); cmd2.CommandType = CommandType.Text; DateTime date = DateTime.ParseExact(TextBox_DepartureDate.Text, "d
SqlCommand cmd2 = conn.CreateCommand();
cmd2.CommandType = CommandType.Text;
DateTime date = DateTime.ParseExact(TextBox_DepartureDate.Text, "dd/MM/yyyy", null);
DateTime date2 = DateTime.ParseExact(TextBox_ArrivalDate.Text, "dd/MM/yyyy", null);
string time1 = TextBox_DepartureTime.Text + " " + DropDownListAMPM.SelectedValue;
string time2 = TextBox_ArrivalTime.Text + " " + DropDownList_AMPM2.SelectedValue;
DateTime dateOnly1 = date.Date;
DateTime dateOnly2 = date2.Date;
if (date <= DateTime.Now.Date)
{
Label_EditTableMessage.ForeColor = System.Drawing.Color.Red;
Label_EditTableMessage.Text = "Departure Date Cannot be in the past";
}
else
{
if (date2 <= date)
{
Label_EditTableMessage.ForeColor = System.Drawing.Color.Red;
Label_EditTableMessage.Text = "Return Date Cannot be Less than the Departure Date";
}
else
{
cmd2.CommandText = "insert into flight(airline_id,flight_number,numer_seats,departure_city,destination_city,departure_time,arrival_time,departure_date,arrival_date) values( '" + DropDownList_AirlineId.SelectedValue + "','" + TextBox_FlightNumber.Text + "','" + TextBox_NumerSeats.Text + "', '" + TextBox_DepartureCity.Text + "', '" + TextBox_DestinationCity.Text + "', '" + time1 + "', '" + time2 + "', '" + dateOnly1 + "', '" + dateOnly2 + "') ";
cmd2.ExecuteNonQuery();
Label_EditTableMessage.ForeColor = System.Drawing.Color.Green;
Label_EditTableMessage.Text = "Flight Added";
EmptyTextBoxes();
GridView_Flight.DataBind();
}
}
}
SqlCommand cmd2=conn.CreateCommand();
cmd2.CommandType=CommandType.Text;
DateTime date=DateTime.ParseExact(文本框_DepartureDate.Text,“dd/MM/yyyy”,null);
DateTime date2=DateTime.ParseExact(TextBox_ArrivalDate.Text,“dd/MM/yyyy”,null);
string time1=TextBox\u DepartureTime.Text+“”+DropDownListAMPM.SelectedValue;
string time2=TextBox\u ArrivalTime.Text+“”+DropDownList\u AMPM2.SelectedValue;
DateTime dateOnly1=日期。日期;
DateTime dateOnly2=日期2.日期;
如果(日期2个部分):
您的架构可能将列数据类型设置为DATETIME
,而不仅仅是DATE
,这将导致它存储完整的日期和时间。这是我要检查的第一件事。
在C#中,您可以使用日期时间
对象上的日期
属性仅获取其日期部分。如果根据第1点为列使用正确的数据类型,则这与此无关
或者,如果需要字符串格式:
date.Date.ToShortDateString()
本协议的两个部分:
您的架构可能将列数据类型设置为DATETIME
,而不仅仅是DATE
,这将导致它存储完整的日期和时间。这是我要检查的第一件事。
在C#中,您可以使用日期时间
对象上的日期
属性仅获取其日期部分。如果根据第1点为列使用正确的数据类型,则这与此无关
或者,如果需要字符串格式:
date.Date.ToShortDateString()
问题不在于您的代码。您应该更改数据库中字段的数据类型。您应该使用datetime
而不是datetime2
您可以参考本文:问题不在于您的代码。您应该更改数据库中字段的数据类型。您应该使用datetime
而不是datetime2
您可以参考这篇文章:问题在于您使用
DateTime dateOnly1 = date.Date;
DateTime dateOnly2 = date2.Date;
你用的是普雷维乌斯
DateTime date = DateTime.ParseExact(TextBox_DepartureDate.Text, "dd/MM/yyyy", null);
DateTime date2 = DateTime.ParseExact(TextBox_ArrivalDate.Text, "dd/MM/yyyy", null);
您只使用date,datetime假定时间为00:00:00问题在于您使用
DateTime dateOnly1 = date.Date;
DateTime dateOnly2 = date2.Date;
你用的是普雷维乌斯
DateTime date = DateTime.ParseExact(TextBox_DepartureDate.Text, "dd/MM/yyyy", null);
DateTime date2 = DateTime.ParseExact(TextBox_ArrivalDate.Text, "dd/MM/yyyy", null);
您只使用date,datetime假设时间为00:00:00,数据库中的数据类型是什么?它是datetime。我认为您在这里不使用javascript标记,它是关于.net和c的,文本框#u DepartureDate包含时间??您不应该关心这一点。只有GUI会关心值何时显示给最终用户,以及n、 使用不包含时间部分的格式。数据库中是哪种数据类型?它是datetime。我认为您在这里不使用javascript标记,它是关于.net和c的。文本框#u DepartureDate包含时间??您不应该关心这一点。只有GUI会关心值何时显示给最终用户,然后,使用不包含时间的格式e时间部分。我更改了它,但它仍然存储相同的内容。您是如何确认它仍然相同的?在MSSQL Studio中还是在您的代码中?在数据库中,它将只存储日期。但当我尝试在Gridview中选择此字段时,它将显示时间。@sara如果您分配给DateTime
对象,它将始终显示时间让我们给它添加一个时间-由您来编写代码,使其格式化并仅显示为日期。我更改了它,但它仍然存储相同的内容。您是如何确认它仍然相同的?在MSSQL Studio中还是在您的代码中?在数据库中,它将仅存储日期。但当我尝试在Gridview中选择此字段时,它将显示timsara如果你给一个DateTime
对象赋值,它总是会给它加上一个时间-由你自己来编写代码,使其格式化并显示为仅日期。那么我该如何修复它呢?那么我该如何修复它呢?3.SQL注入3.SQL注入