C# 字符串未被识别为有效的DateTime[从DateTime中删除时间]
我正在从sql server中检索一个日期,即2016-01-06(年-月-日),并将其转换为字符串。sql格式是Date。我将获得2016年6月1日(月/日/年)12:00:00 AM,而不是仅获得2016年1月1日06分。现在我要做的是删除时间并将日期转换为dd/m/yyy格式。我已经在其他问题中遵循了所有示例,但我会得到“字符串未被识别为有效的日期时间”是否有任何步骤我错过了或我做得不对C# 字符串未被识别为有效的DateTime[从DateTime中删除时间],c#,asp.net,sql-server,date,datetime,C#,Asp.net,Sql Server,Date,Datetime,我正在从sql server中检索一个日期,即2016-01-06(年-月-日),并将其转换为字符串。sql格式是Date。我将获得2016年6月1日(月/日/年)12:00:00 AM,而不是仅获得2016年1月1日06分。现在我要做的是删除时间并将日期转换为dd/m/yyy格式。我已经在其他问题中遵循了所有示例,但我会得到“字符串未被识别为有效的日期时间”是否有任何步骤我错过了或我做得不对 protected void btnUser_Click(object sender, EventA
protected void btnUser_Click(object sender, EventArgs e)
{
{
string Name = cmbName.Text;
string start = "";
SqlConnection myConn = new SqlConnection("Data Source=localhost;" + "Initial Catalog=IBBTS_DB; Integrated Security =SSPI");
SqlCommand retrieveStart_DateCmd = new SqlCommand("SELECT startDate FROM testSet where TS_ID = 121 ;", myConn);
SqlDataReader reader6 = retrieveStart_DateCmd.ExecuteReader();
while (reader6.Read())
{
start = (reader6.GetValue(0).ToString());
}
reader6.Close();
DateTime dateTime = DateTime.ParseExact(start, "dd/MM/yyyy", CultureInfo.InvariantCulture);
}
}
避免使用ToString()
-这是在简化SQL Server发送给您的类型。您应该能够使用GetDateTime(0)
而不是GateValue(0)
检索该值。这将允许您更轻松地操纵值
C#没有内置的Date
类型,只有DateTime
。因此,这就是你所看到的。获得日期时间后,您可以使用正常的格式化操作将其格式化为您喜欢的格式。避免使用ToString()
-您正在简化SQL Server发送给您的类型。您应该能够使用GetDateTime(0)
而不是GateValue(0)
检索该值。这将允许您更轻松地操纵值
C#没有内置的
Date
类型,只有DateTime
。因此,这就是你所看到的。获取了日期时间后,您可以使用常规的格式化操作将其格式化为您喜欢的格式。既然您知道该字段将是日期,并且它只是一个字段,您可以使用GetDateTime吗
DateTime dateTime = Reader6.GetDateTime(0)
这可能会让您跳过字符串转换。显然,如果可能的话,首先检查dbNull 既然您知道该字段将是一个日期,而它只是一个字段,那么您可以使用GetDateTime吗
DateTime dateTime = Reader6.GetDateTime(0)
这可能会让您跳过字符串转换。显然,如果可能的话,首先检查dbNull SQL以标准的SQL格式存储
datetime
数据类型日期。但是,当您从SQL中检索datetime
数据类型时,datetime
结果将转换为C#中的datetime
结构
由于检索到的值是一个DateTime
对象,因此不需要将其转换为字符串,然后将其解析出来。这样做会加倍检索日期时间的转换工作
试试这个例子
protected void btnUser_Click(object sender, EventArgs e)
{
{
string Name = cmbName.Text;
DateTime start = default(DateTime);
SqlConnection myConn = new SqlConnection("Data Source=localhost;" + "Initial Catalog=IBBTS_DB; Integrated Security =SSPI");
SqlCommand retrieveStart_DateCmd = new SqlCommand("SELECT startDate FROM testSet where TS_ID = 121 ;", myConn);
SqlDataReader reader6 = retrieveStart_DateCmd.ExecuteReader();
while (reader6.Read())
{
start = (DateTime)reader6[0];
}
reader6.Close();
string myFormattedString = start.ToString("dd/MM/yyyy");
}
}
SQL以SQL的标准格式存储数据类型日期。但是,当您从SQL中检索datetime
数据类型时,datetime
结果将转换为C#中的datetime
结构
由于检索到的值是一个DateTime
对象,因此不需要将其转换为字符串,然后将其解析出来。这样做会加倍检索日期时间的转换工作
试试这个例子
protected void btnUser_Click(object sender, EventArgs e)
{
{
string Name = cmbName.Text;
DateTime start = default(DateTime);
SqlConnection myConn = new SqlConnection("Data Source=localhost;" + "Initial Catalog=IBBTS_DB; Integrated Security =SSPI");
SqlCommand retrieveStart_DateCmd = new SqlCommand("SELECT startDate FROM testSet where TS_ID = 121 ;", myConn);
SqlDataReader reader6 = retrieveStart_DateCmd.ExecuteReader();
while (reader6.Read())
{
start = (DateTime)reader6[0];
}
reader6.Close();
string myFormattedString = start.ToString("dd/MM/yyyy");
}
}
尝试以以下格式分配日期:
protected void btnUser_Click(object sender, EventArgs e)
{
string Name = cmbName.Text, start = string.empty;
SqlConnection myConn = new SqlConnection("Data Source=localhost;" + "Initial Catalog=IBBTS_DB; Integrated Security =SSPI");
SqlCommand retrieveStart_DateCmd = new SqlCommand("SELECT startDate FROM testSet where TS_ID = 121 ;", myConn);
SqlDataReader reader6 = retrieveStart_DateCmd.ExecuteReader();
while (reader6.Read())
{
start = (reader6.GetValue(0).ToString());
}
reader6.Close();
DateTime dateTime = DateTime.ParseExact(start, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);//DateTime.ParseExact(start, "dd/MM/yyyy", CultureInfo.InvariantCulture);
}
并在Web.config文件中添加此
<configuration>
<system.web>
<globalization culture="en-US" uiCulture="en-US" />
</system.web>
尝试以以下格式分配日期:
protected void btnUser_Click(object sender, EventArgs e)
{
string Name = cmbName.Text, start = string.empty;
SqlConnection myConn = new SqlConnection("Data Source=localhost;" + "Initial Catalog=IBBTS_DB; Integrated Security =SSPI");
SqlCommand retrieveStart_DateCmd = new SqlCommand("SELECT startDate FROM testSet where TS_ID = 121 ;", myConn);
SqlDataReader reader6 = retrieveStart_DateCmd.ExecuteReader();
while (reader6.Read())
{
start = (reader6.GetValue(0).ToString());
}
reader6.Close();
DateTime dateTime = DateTime.ParseExact(start, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);//DateTime.ParseExact(start, "dd/MM/yyyy", CultureInfo.InvariantCulture);
}
并在Web.config文件中添加此
<configuration>
<system.web>
<globalization culture="en-US" uiCulture="en-US" />
</system.web>
为什么不尝试使用转换功能。为什么不尝试使用转换功能。