如何将textbox.text(日期)存入datetime型的数据字段中去
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转换错误
------解决方案--------------------Convert.ToDateTime() 转化一下
------解决方案--------------------Convert.ToDateTime(dateTextBox.Text);转换之后再insert
------解决方案--------------------dateTextBox.Text是string类型的
Convert.ToDateTime(dateTextBox.Text)进行一次转换就OK了
最好对dateTextBox.Text的输入格式进行一下控制,比如说用dateTimePicker控件就很不错~
否则有可能要出现转换错误
------解决方案--------------------( "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 + " ') "