在没有CAST的情况下将字符串作为datetime插入SQL

时间:2011-08-02 11:37:01

标签: c# sql datetime types

我正在将数据从不同的源类型(如Oracle,MySQL,Access,Sharepoint等)移动到SQL。

在c#中生成SQL脚本,如下所示:

with cte (ID, changedate, SupplierID) 
as (
SELECT 1720,  '1997-12-17 12:00:00 AM',  763
UNION ALL
SELECT 1721,  '1900-01-01 12:00:00 AM',  114)
 select * into components8_2_2011_3  from cte

datatime字段创建为varchar,这是错误的。

在C#中我可以看到列类型是datetime,但是MSSQL不能将字符串识别为datetime。

如何在不添加CAST语句到changeDate字段的情况下创建datetime字段?

3 个答案:

答案 0 :(得分:2)

你做不到。除非你告诉它,否则SQL Server不知道它是一个日期时间。为什么要这样?

例如:

  • 12:00:00 AM是午夜,所以它是date吗?或smalldatetime for SQL Server 2005?
  • 你会存储时间部分吗?如果是,那么分辨率(smalldatetimedatetime2等)
  • 这是否包含任何时区信息?
  • 是UTC还是当地时间?

如果您不想要CAST(或CONVERT),请先创建表components8_2_2011_3

编辑:亚历克斯K的诀窍很聪明,但是当datetime足够时,你想要接下来的1亿行完整date ......

答案 1 :(得分:2)

如何使用“指南行”

with cte (ID, changedate, SupplierID) as (
   --type but no data
   select 0, getdate(), 0 where 0=1 union 

   SELECT 1720,  '1997-12-17 12:00:00 AM',  763
   UNION ALL
   SELECT 1721,  '1900-01-01 12:00:00 AM',  114
)
select * into components8_2_2011_3  from cte

答案 2 :(得分:0)

DataTier.ClsGlobal dataBaseObj = new DataTier.ClsGlobal();

    string strmessage;
    int intResult;

    public string login(string username, string password)// login method
    {
        dataBaseObj.sqlconn.Open();

        dataBaseObj.sqlCmd = new SqlCommand("SELECT  clEmail, clPassword FROM Client where clEmail='" + username + "'", dataBaseObj.sqlconn);
        dataBaseObj.sqlDr = dataBaseObj.sqlCmd.ExecuteReader();


        if (dataBaseObj.sqlDr.Read())
        {
            if (dataBaseObj.sqlDr["clPassword"].Equals(password.ToString()))
            {
                strmessage = "client";
                dataBaseObj.sqlconn.Close();
            }
            else
            {
                intResult++;

                strmessage = "login  not succesfull";

                dataBaseObj.sqlconn.Close();

                if (intResult == 3)
                {
                    strmessage = "your Blocked";
                }

            }
        }    // if its not the client is gonna go to workers table 
        else
        {
            dataBaseObj.sqlCmd = new SqlCommand("SELECT wuUsername, wuPassword, wuUserType FROM  WorkUsers where wuUsername'" + username + "'", dataBaseObj.sqlconn);
            dataBaseObj.sqlDr = dataBaseObj.sqlCmd.ExecuteReader();

            if (dataBaseObj.sqlDr.Read())
            {
                if (dataBaseObj.sqlDr["wuPassword"].Equals(password.ToString()))
                {
                    strmessage = "Receptionist";
                    dataBaseObj.sqlconn.Close();
                }
                else
                {

                    intResult++;

                    strmessage = "login  not succesful";

                    dataBaseObj.sqlconn.Close();

                    if (intResult == 3)
                    {
                        strmessage = "your Blocked";
                    }
                }
            }
        }
        return strmessage;
    }

    // this method is for registering the client of the client if not registered
    public string registration(string clID, string clFirstName, string clSurname, string clStreetAddress, string clCity, string clPostCode, string clHomePhone, string clMobilePhone, string clEmail, string clPassword, string clStatus)
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlCmd = new SqlCommand("insert into Client values(@clID,@clFirstName@clSurname,@clStreetAddress,@clCity,@clPostCode,@clHomePhone,@clMobilePhone,@clEmail,@clPassword@clStatus)", dataBaseObj.sqlconn);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clID", clID);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clFirstName", clFirstName);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clSurname", clSurname);


        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clStreetAddress", clStreetAddress);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clCity", clCity);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clPostCode", clPostCode);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clHomePhone", clHomePhone);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clMobilePhone", clMobilePhone);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clEmail", clEmail);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clPassword", clPassword);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clStatus", clStatus);

        intResult = dataBaseObj.sqlCmd.ExecuteNonQuery();
        if (intResult == 1)
        {
            strmessage = "registeed";
        }
        else
        {
            strmessage = "not registeed";
        }

        return strmessage;
    }

    // this is for the admin to register the the receptionist
    public string AdminRegister(string Id, string username, string password)
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlCmd = new SqlCommand("INSERT INTO WorkUsers (wuID, wuUsername, wuPassword) VALUES ('" + Id + "','" + username + "','" + password + "')", dataBaseObj.sqlconn);
        intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery();
        if (intResult == 1)
        {
            strmessage = "registeed";
        }
        else
        {
            strmessage = "not registeed";
        }

        return strmessage;
    }

    // this method is fo receptionist to inser an appoirment
    public string ReceptioInsert(int appID, string appDateNow, string appTimeNow, string appReason, string appDateofBooking, string appTimeofBooking, int clID, int wuID)
    {
        dataBaseObj.sqlconn.Open();

       // appDateNow = Convert.ToString(System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.DayOfWeek);
        appDateNow = Convert.ToString(System.DateTime.Now.ToString("m"));
        //appTimeNow = Convert.ToString(System.DateTime.Now.TimeOfDay);
        appTimeNow = Convert.ToString(System.DateTime.Now.ToString("hh:mm:ss"));

        dataBaseObj.sqlCmd = new SqlCommand("SELECT appDateNow, appTimeNow FROM  Appointments where appDateNow='" + appTimeNow + "'AND  where appTimeNow='" + appTimeNow + "'", dataBaseObj.sqlconn);
        if (dataBaseObj.sqlDr.Read())// if it reads it means date and time are booked
        {
            strmessage = "sorry that time is not available";
        }
        else
        {

            dataBaseObj.sqlCmd = new SqlCommand("INSERT INTO Appointments (appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID) VALUES('" + appID + "','" + appDateNow + "','" + appTimeNow + "','" + appReason + "','" + appDateofBooking + "','" + appTimeofBooking + "','" + clID + "','" + wuID + "')", dataBaseObj.sqlconn);
            intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery();
            if (intResult == 1)
            {
                strmessage = "registeed";
            }
            else
            {
                strmessage = "not registeed";
            }
            dataBaseObj.sqlconn.Close();
        }
        return strmessage;

    }
    // this method is to update the appointment
    public void updateAppointmen(int appID, string appDateNow, string appTimeNow, string appReason, string appDateofBooking, string appTimeofBooking, int clID, int wuID)
    {
        dataBaseObj.sqlconn.Open();
        appDateNow = Convert.ToString(System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.DayOfWeek);

        appTimeNow = Convert.ToString(System.DateTime.Now.TimeOfDay);
        dataBaseObj.sqlCmd = new SqlCommand("UPDATE Appointments SET  appDateNow =@appDateNow, appTimeNow =@appTimeNow, appReason =@appReason, appDateofBooking =@appDateofBooking, appTimeofBooking =@appTimeofBooking, clID =@clID, wuID =@wuID where appID='" + appID + "'", dataBaseObj.sqlconn);



        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateNow", appDateNow);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeNow", appTimeNow);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appReason", appReason);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateofBooking", appDateofBooking);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeofBooking", appTimeofBooking);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clID", clID);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@wuID", wuID);
        dataBaseObj.sqlCmd.ExecuteNonQuery();
    }
    // this method is to delete the appoinment 

    public void deleteAppointment(int appID)
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlCmd = new SqlCommand("DELETE FROM Appointments whre appID='" + appID + "'");
        dataBaseObj.sqlCmd.ExecuteNonQuery();
    }
    // method for client to view the appoinments
    public DataTable CLientViewAppoitnmet(string date)
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments where appDateNow='" + date + "'", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }

// this method is for the client to insert a note
    public string CLinetInformRecep(int anID, int appID, string anNotes)
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlCmd = new SqlCommand("INSERT INTO AppointmentNotes (anID, appID, anNotes) VALUES ('" + anID + "','" + appID + "','" + anNotes + "')", dataBaseObj.sqlconn);
         intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery();
            if (intResult == 1)
            {
                strmessage = "registeed";
            }
            else
            {
                strmessage = "not registeed";
            }
            dataBaseObj.sqlconn.Close();
            return strmessage;
    }
      // this is for the receptionis to view notes added vy the client

    public DataTable selectAppointmentNotes()
    {


        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT FROM AppointmentNotes", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }
    // this methiod is for therapist to view cuurent day appointment
    public DataTable TherapisViewAppoiments()       
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments where appDateNow ='" +System.DateTime.Now.DayOfWeek+"'", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }
    // this method here is for the  therapist to view cuurent day appointment with oppoetunit to filter them 
    public DataTable ViewAllAppoiements(int id, string date)
    {
        // this method return the information of appointmenet where the id and date matchs since i have user the operatoe AND 
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments where appID ='" + id + "'AND where appDateNow='" + date +"'", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }
    // this method is to update appoitment by therapist
    public void UpdateAppoiment(int appID, string appDateNow, string appTimeNow, string appReason, string appDateofBooking, string appTimeofBooking, int clID, int wuID)
    {
        dataBaseObj.sqlconn.Open();
        appDateNow = Convert.ToString(System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.DayOfWeek);

        appTimeNow = Convert.ToString(System.DateTime.Now.TimeOfDay);
        dataBaseObj.sqlCmd = new SqlCommand("UPDATE Appointments SET  appDateNow =@appDateNow, appTimeNow =@appTimeNow, appReason =@appReason, appDateofBooking =@appDateofBooking, appTimeofBooking =@appTimeofBooking, clID =@clID, wuID =@wuID where appID='" + appID + "'", dataBaseObj.sqlconn);



        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateNow", appDateNow);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeNow", appTimeNow);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appReason", appReason);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateofBooking", appDateofBooking);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeofBooking", appTimeofBooking);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clID", clID);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@wuID", wuID);
        dataBaseObj.sqlCmd.ExecuteNonQuery();
    }
    // this method is to delete the appoinmet
    public string deleteAppoinment(int appID)
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlCmd = new SqlCommand("DELETE FROM Appointments whre appID='" + appID + "'");
        dataBaseObj.sqlCmd.ExecuteNonQuery();
        intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery();
        if (intResult == 1)
        {
            strmessage = "registeed";
        }
        else
        {
            strmessage = "not registeed";
        }
        dataBaseObj.sqlconn.Close();
        return strmessage; 
    }
    // report order by date

    public DataTable listOfCLient()
    {
       dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT clID, clFirstName, clSurname, clStreetAddress, clCity, clPostCode, clHomePhone, clMobilePhone, clEmail, clPassword, clStatus FROM Client", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }
    public  DataTable listOfAppoint()
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT        appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments ORDER BY appDateNow", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }

    public DataTable listAppontBytherapy()
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT  Appointments.appID, Appointments.appDateNow, Appointments.appTimeNow, Appointments.appReason, Appointments.appDateofBooking,Appointments.appTimeofBooking, Appointments.clID, Appointments.wuID, WorkUsers.wuUserType FROM Appointments INNER JOIN WorkUsers ON Appointments.wuID = WorkUsers.wuID ORDER BY WorkUsers.wuUserType", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }
}
}