您的位置:程序门 -> .net技术 -> c#



如何将textbox.text(日期)存入datetime型的数据字段中去


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何将textbox.text(日期)存入datetime型的数据字段中去[已结贴,结贴人:erp2006]
发表于:2007-09-17 10:18:24 楼主
sqlconnection   myconn   =   new   sqlconnection( "data   source=localhost;initial   catalog=oadb;   uid=sa;pwd=6 ");
                sqlcommand   mycmd   =   new   sqlcommand( "insert   into   ww_news(date,bigtype,smalltpe,tip,nr)values( 'datetextbox.text ', ' "   +   dropdownlist1.selectedvalue   +   " ', ' "   +   dropdownlist2.selectedvalue   +   " ', ' "   +   textbox1.text   +   " ', ' "   +   textbox13.text   +   " ') ",   myconn);
                myconn.open();
                mycmd.EXECutenonquery();
                myconn.close();

出错提示:datetime转换错误
发表于:2007-09-17 10:24:371楼 得分:0
up
发表于:2007-09-17 10:27:312楼 得分:5
convert.todatetime()   转化一下
发表于:2007-09-17 10:33:213楼 得分:0
-_-#...

values( 'datetextbox.text '
--->
values( ' "   +   datetextbox.text   +   " '

不过在这么做之前,首先要保证你的datetextbox.text是有效的datatime
发表于:2007-09-17 10:34:594楼 得分:5
convert.todatetime(datetextbox.text);转换之后再insert
发表于:2007-09-17 10:37:135楼 得分:0
调试看一下你的textbox里文本是否是datetime的格式

不用转换,如果格式正确的话,可以直接插入到库的 '1999-8-8   12:54:06 '
发表于:2007-09-17 10:38:456楼 得分:0
数据类型错误,进行一下类型转换。
发表于:2007-09-17 10:46:407楼 得分:0
日期格式为什么不用datetimepicker控件呢?
发表于:2007-09-17 10:57:448楼 得分:5
datetextbox.text是string类型的
convert.todatetime(datetextbox.text)进行一次转换就ok了
最好对datetextbox.text的输入格式进行一下控制,比如说用datetimepicker控件就很不错~
否则有可能要出现转换错误
发表于:2007-09-17 11:38:289楼 得分:0
convent.todatetime...
发表于:2007-09-17 11:39:1410楼 得分:0
可以用正则表达式来验证下是否符合条件再转换
发表于:2007-09-17 11:45:0911楼 得分:5
( "insert   into   ww_news(date,bigtype,smalltpe,tip,nr)values( 'datetextbox.text ', ' "   +   dropdownlist1.selectedvalue   +   " ', ' "   +   dropdownlist2.selectedvalue   +   " ', ' "   +   textbox1.text   +   " ', ' "   +   textbox13.text   +   " ') "

仔细看了一下
insert   into   ww_news   (date,bigtype,   smalltpe,tip,nr)
values   ( 'datetextbox.text ',....)

你的第一个date是否需要传递datetime类型?但是value部分对应的 'datetextbox.text '显然是字符型的。你是否想要写成
( "insert   into   ww_news(date,bigtype,smalltpe,tip,nr)values( ' "+datetextbox.text+ " ', ' "   +   dropdownlist1.selectedvalue   +   " ', ' "   +   dropdownlist2.selectedvalue   +   " ', ' "   +   textbox1.text   +   " ', ' "   +   textbox13.text   +   " ') "
发表于:2007-09-17 19:48:2812楼 得分:0
运行成功:
           
    datetime   a   =   convert.todatetime(datetextbox.text);
                sqlconnection   myconn   =   new   sqlconnection( "data   source=localhost;initial   catalog=oadb;   uid=sa;pwd=666 ");
                sqlcommand   mycmd   =   new   sqlcommand( "insert   into   ww_news(date,bigtype,smalltpe,tip,nr)values( ' "+a+ " ', ' "   +   dropdownlist1.selectedvalue   +   " ', ' "   +   dropdownlist2.selectedvalue   +   " ', ' "   +   textbox1.text   +   " ', ' "   +   freetextbox1.text   +   " ') ",   myconn);

送分


快速检索

最新资讯
热门点击