本文介绍了问题插入时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我插入了日期但不知道如何插入时间..这是我的代码。 请帮助。 protected void click_insert( object sender,EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ CARRENTAL]的ConnectionString)。 SqlCommand cmd = new SqlCommand(); 尝试 { cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = 插入car_insert(city,loc,c_type,name,email,mob,date )值(@ city,@ loc,@ c_type,@ name,@ email,@ mob,@ date); cmd.Parameters.AddWithValue( @ city,DropDownList1.SelectedItem .Text.ToString()); cmd.Parameters.AddWithValue( @ loc,DropDownList2.SelectedItem.Text.ToString ()); cmd.Parameters.AddWithValue( @ c_type,DropDownList11.SelectedItem.Text.ToString ()); cmd.Parameters.AddWithValue( @ name,TextBox1.Text.ToString() ); cmd.Parameters.AddWithValue( @ email,TextBox2.Text.ToString() ); cmd.Parameters.AddWithValue( @ mob,TextBox3.Text.ToString() ); cmd.Parameters.AddWithValue( @ date,Calendar1.SelectedDate.ToString( yyyy.MM.dd)); con.Open(); cmd.ExecuteNonQuery(); con.Close(); Response.Redirect( showdata.aspx); } catch (Exception ex) {Response.Write(ex.Message); } } 解决方案 您只能从ASP.NET日历控件中获取日期。你需要找到支持日期和时间的jQuery或其他扩展日历控件,请查看以下文章,了解如何使用jQuery DateTime选择器并从后面的代码中获取所选日期时间。 ASP.Net TextBox示例的DateTimePicker控件 [ ^ ] 然后你可以插入选中的 DateTime SQl服务器中的DateTime或DateTime2列的值如下所示 DateTime dob = DateTime.Parse(Request.Form [TextBox1.UniqueID]); cmd.Parameters.AddWithValue( @ dob,dob); i inserted date but don't know how to insert time.. here's my code.Please Help.protected void click_insert(object sender, EventArgs e){ SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["carrental"].ConnectionString); SqlCommand cmd = new SqlCommand(); try { cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into car_insert (city,loc,c_type,name,email,mob,date) values(@city,@loc,@c_type,@name,@email,@mob,@date)"; cmd.Parameters.AddWithValue("@city", DropDownList1.SelectedItem.Text.ToString()); cmd.Parameters.AddWithValue("@loc", DropDownList2.SelectedItem.Text.ToString()); cmd.Parameters.AddWithValue("@c_type", DropDownList11.SelectedItem.Text.ToString()); cmd.Parameters.AddWithValue("@name", TextBox1.Text.ToString()); cmd.Parameters.AddWithValue("@email", TextBox2.Text.ToString()); cmd.Parameters.AddWithValue("@mob", TextBox3.Text.ToString()); cmd.Parameters.AddWithValue("@date", Calendar1.SelectedDate.ToString("yyyy.MM.dd")); con.Open(); cmd.ExecuteNonQuery(); con.Close(); Response.Redirect("showdata.aspx"); } catch (Exception ex) { Response.Write(ex.Message); }} 解决方案 you can get only the date from ASP.NET Calendar control. you need to find jQuery or other extended Calendar control which support both Date and Time, Check below Article for how to use jQuery DateTime picker and fetch selected datetime from code behind.DateTimePicker control for ASP.Net TextBox Example[^]Then you can insert selected DateTime value to a DateTime or DateTime2 column in your SQl server Database like belowDateTime dob = DateTime.Parse(Request.Form[TextBox1.UniqueID]);cmd.Parameters.AddWithValue("@dob", dob); 这篇关于问题插入时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 18:04