无法将日期时间类型插入sql数据库 [英] can't insert datetime type to sql database

查看:403
本文介绍了无法将日期时间类型插入sql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我想使用文本框和日历内容插入日期



错误: - 类型日期时间无法插入表格但其他类型正常插入




我的例子:



我有以下表格列:



userid(int& identity)| name(nvarchar(50))| expdate(datetime)

-------------------------------------- ---------------------------



存储过程代码



Dear All,

I want to insert date using textbox and calendar extendar

Error :- The type datetime can't insert to table but other types inserted normally


My Example :

I have the following table columns :

userid (int & identity) | name (nvarchar(50)) | expdate (datetime)
-----------------------------------------------------------------

Stored Procedure code

ALTER PROCEDURE dbo.Manageusers
(

@name nvarchar(50),
@expdate  datetime

)

AS

   

INSERT INTO users (name,expdate) Values (@name,@expdate)

    END


    RETURN



< br $> b $ b



- C#班级:







- C# Class :

public class insertusers:opsMaintable 
{
 protected string ProcedureName;

    #region Fields

    private string _name;
     private  DateTime _expdate;
    

    #endregion

    #region Properties


    public string name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }
     public DateTime expdate
    {
        get
        {
            return _expdate;
        }
        set
        {
            _expdate = value;
        }
    }

    #endregion

    //Insert & Delete & Update Method
    protected override bool LoadProperties2Parameters(string TypeofOperation)
    {
        SortedList SL = new SortedList();
       
        SL.Add("@name", name);
SL.Add("@expdate", expdate);
        


        ProcedureName = "Manageusers";
        if (db.RunProcedure(ProcedureName, SL) == 1)
            return true;
        else
            return false;
    }
}







- 插入按钮代码






- insert button code behind

 insertusers ingang = new insertusers();
ingang.name = Textuser.Text;
ingang.expdate =Convert.ToDateTime(Textdate.Text);

ingang.Add()

推荐答案

SL.Add("@expdate", expdate.ToString("yyyy-MM-dd HH:mm:ss"));


你为什么为此使用SortedList?

想一想:如果添加的第一项是Rony而第二项是2013-08-03 14:39,那么对象的顺序是什么?它们可能会以什么顺序传递给您的程序?
Why are you using a SortedList for this?
Think about it: what order are the objects in if the first item added is "Rony" and the second is "2013-08-03 14:39"? And what order are they likely to be passed to your procedure?


您可以将datetime类型转换为字符串,然后将其插入数据库:

试试这个:< br $> b $ b

you can convert datetime type to string and then insert it into database:
try this:

public string ConvertDatetimeToValid(DateTime dateTime)
       {
           string datetime = string.Concat(dateTime.Year.ToString(), "/",      dateTime.Month.ToString(), "/", dateTime.Day.ToString());
           return datetime;
       }


这篇关于无法将日期时间类型插入sql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆