c# - 将varchar数据类型转换为日期时间数据类型会导致超出范围的值

时间:2013-07-04 07:29:30

标签: c# asp.net sql-server

我是.net开发的新手

我正在尝试从Facebook应用程序获取数据,更新查询时我在更新客户详细信息时遇到上述错误

,我在cust_date数据类型中采用的是DATETIME。

所以如何将这个DATETIME格式转换为'MM / DD / YYYY HH:MM'

    public void Customer(string Customerid)
    {
        var accessToken = "CAACEdEose0cBAMTINaTZCrm67pT6cO16KHsR3UNgOTZAGH03GHmhKozUOxEXYpL3ZB9pfbFWm4Oj2VMVy8xMf5vXMpFtY6LZm2Hej0WmrHPlwk7pWyibf9gXbCaDBoIih26nRqffAfwZD";
        var client = new FacebookClient(accessToken);

        string clientfeed = client.Get(Customerid).ToString();

        JObject obj = JObject.Parse(clientfeed);



        string custid = obj["id"].ToString();
        string name = obj["name"].ToString();
        string fst_name = obj["first_name"].ToString();
        string lst_name = obj["last_name"].ToString();
        string link = obj["link"].ToString();
        string[] splitlink = link.Split('/');
        if (splitlink[3].StartsWith("profile"))
        {
            splitlink[3] =name.ToString();
            link = splitlink[0] + '/' + splitlink[1] + '/' + splitlink[2] + '/' + splitlink[3];
        }
        string gender = obj["gender"].ToString();
        string cust_updated_time = obj["updated_time"].ToString();
        string abctime = cust_updated_time.Substring(0, cust_updated_time.ToString().Length - 3);

        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["IIPLDOTNETConnectionString"].ConnectionString);
        myConnection.Open();
        string qy3 = "select count(*) from fb_customer where customer_fb_id='" + custid + "'";
        string count;
        SqlCommand myCommand = new SqlCommand(qy3, myConnection);
        SqlDataReader myReader = myCommand.ExecuteReader();
        while (myReader.Read())
        {
            count = myReader[0].ToString();
            int cnt = Convert.ToInt32(count);
            if (cnt == 0)
            {
                SqlConnection myOleConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["IIPLDOTNETConnectionString"].ConnectionString);
                string qy = "select * from fb_customer";
                myOleConnection.Open();
                SqlCommand myOleCommand = new SqlCommand(qy, myOleConnection);
                myOleCommand.CommandText = "SET DATEFORMAT MDY insert into fb_customer(customer_fb_id,name,firstname,lastname,link,gender,cust_date,New) values('" + custid + "','" + name + "','" + fst_name + "','" + lst_name + "','" + link + "','" + gender + "','" + abctime + "','1')";
                SqlDataReader myOleDataReader = myOleCommand.ExecuteReader();
                myOleDataReader.Close();
                myOleConnection.Close();
            }
            else
            {
                SqlConnection myOleDb = new SqlConnection(ConfigurationManager.ConnectionStrings["IIPLDOTNETConnectionString"].ConnectionString);
                string qy1 = "select * from fb_customer";
                myOleDb.Open();
                SqlCommand mycmd = new SqlCommand(qy1, myOleDb);
                mycmd.CommandText = " UPDATE fb_customer set name='" + name + "',firstname='" + fst_name + "',lastname='" + lst_name + "',link='" + link + "',gender='" + gender + "',cust_date= '" + abctime + "' where customer_fb_id = '" + custid + "'";
                SqlDataReader mydatareader = mycmd.ExecuteReader();
                mydatareader.Close();
                myOleDb.Close();
            }
        }
        myReader.Close();
        myConnection.Close();

    }

    public void IIPLCustomer(string iiplcustid, string abctime)
    {
        var accessToken = "CAACEdEose0cBADKYiSkFVcqeZAYxmYlytKM5pQT6zuEvnNoU9soPlWc2pZAJvKDL557BKTGIRNhbcuZBR6Li8TlrcM8yG1yXYGzikVlUPyFgDPzEDMiIZAnuHe5y3gTZCxkyTeA12ISPPRhJev6B63rTr05slMfwZD";
        var client = new FacebookClient(accessToken);

        string clientfeed = client.Get(iiplcustid).ToString();

        JObject obj = JObject.Parse(clientfeed);

        string custid = obj["id"].ToString();
        string name = obj["name"].ToString();
        string[] splitname = name.Split(' ');
        string link = obj["link"].ToString();           


        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["IIPLDOTNETConnectionString"].ConnectionString);
        myConnection.Open();
        string qy = "select count(*) from fb_customer where customer_fb_id='" + custid + "'";
        string count;
        SqlCommand myCommand = new SqlCommand(qy, myConnection);
        SqlDataReader myReader = myCommand.ExecuteReader();
        while (myReader.Read())
        {
            count = myReader[0].ToString();
            int cnt = Convert.ToInt32(count);
            if (cnt == 0)
            {
                SqlConnection myConnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["IIPLDOTNETConnectionString"].ConnectionString);
                string qy1 = "select * from fb_customer";
                myConnection1.Open();
                SqlCommand myCommand1 = new SqlCommand(qy1, myConnection1);
                myCommand1.CommandText = "SET DATEFORMAT MDY insert into fb_customer(customer_fb_id,name,link,firstname,lastname,cust_date,New) values('" + custid + "','" + name + "','" + link + "','" + splitname[0] + "','" + splitname[1] + "','" + abctime + "','1')";
                SqlDataReader myReader1 = myCommand1.ExecuteReader();
                myReader1.Close();
                myConnection1.Close();
            }
            else
            {
                SqlConnection myOleDb = new SqlConnection(ConfigurationManager.ConnectionStrings["IIPLDOTNETConnectionString"].ConnectionString);
                string qy1 = "select * from fb_customer";
                myOleDb.Open();
                SqlCommand mycmd = new SqlCommand(qy1, myOleDb);
                mycmd.CommandText = "SET DATEFORMAT MDY UPDATE fb_customer set name='" + name + "',firstname='" + splitname[0] + "',lastname='" + splitname[1] + "',link='" + link + "',cust_date='" + abctime + "' where customer_fb_id = '" + custid + "'";

                //Here I'm getting an error
                SqlDataReader mydatareader = mycmd.ExecuteReader();
                mydatareader.Close();
                myOleDb.Close();
            }
        }
        myReader.Close();
        myConnection.Close();
    }

2 个答案:

答案 0 :(得分:2)

您可以使用Datetime.ParseExact方法将日期转换为适当的格式。像这样使用

  

DateTime.ParseExact('07 -12-2013','dd / MM / yyyy',   System.Globalization.CultureInfo.InvariantCulture)的ToString( 'YYYY / MM / DD')

您需要随意更改格式。

答案 1 :(得分:0)

这将非常简单。请尝试以下格式。

string date = DateTime.Now.ToString("MM/dd/yyyy HH:MM");

我刚给了DateTime.Now并转换了它。您可以提供所获得的DateTime并将其转换为上述语法中指定的格式。