我希望你能在我的项目中帮助我。我很久以前就一直在尝试解决这个问题,但它不起作用。
我正在尝试将日期时间从 c# 传递到 MS sql 服务器,请注意服务器存储的日期时间格式为 '1900-01-01 00:00:00.000'
当我在本地使用它时它工作得很好,但是当我将页面上传到服务器时出现错误。
string connectionString;
SqlConnection mySqlConnection;
connectionString = ConfigurationManager.ConnectionStrings[("connectionNamr")].ConnectionString;
mySqlConnection = new SqlConnection(connectionString);
SqlCommand mySqlComd = new SqlCommand();
SqlDataReader reader;
mySqlComd.CommandText = "ProcedureName";
mySqlComd.CommandType = CommandType.StoredProcedure;
mySqlComd.Connection = mySqlConnection;
SqlParameter depParam = new SqlParameter("@colName1", SqlDbType.NVarChar, 255);
SqlParameter empParam = new SqlParameter("@colName2", SqlDbType.NVarChar, 20);
SqlParameter startDateParam = new SqlParameter("@EVcolName3", SqlDbType.DateTime);
SqlParameter endDateParam = new SqlParameter("@colName4", SqlDbType.DateTime);
SqlParameter filterParam = new SqlParameter("@colName5", SqlDbType.NVarChar, 20);
depParam.Value = string.Empty;
empParam.Value = employeeID;
DateTime datet = new DateTime(year,month,day);
string datet1 = datet.ToString();
//string datet1 = datet.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
// datet1 = datet1.Substring(0, 19);
startDateParam.Value = Convert.ToDateTime(datet1.Trim()); ;
endDateParam.Value = Convert.ToDateTime(datet1.Trim()); ;
filterParam.Value = string.Empty;
mySqlComd.Parameters.Add(depParam);
mySqlComd.Parameters.Add(empParam);
mySqlComd.Parameters.Add(startDateParam);
mySqlComd.Parameters.Add(endDateParam);
mySqlComd.Parameters.Add(filterParam);
mySqlConnection.Open();
mySqlComd.ExecuteNonQuery();
reader = mySqlComd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
attendance_date = reader["SITE_IN_TIME"].ToString();
}
mySqlConnection.Close();
return attendance_date;
}
catch (Exception exp)
{
return "Error, Exception: " + exp;
}