asp.net 日期转换后插入sql数据库

作者&投稿:丛昏 (若有异议请与网页底部的电邮联系)
~ 如果你的age是表示日期而不是表示年龄的话,可参考以下代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //令括号内代码只在第一次浏览这个页面的时候执行 by marco
{
for (int i = 1900; i < 2008; i++)
D1.Items.Add(i.ToString());
for (int i = 1; i <= 12; i++) //一年有12个月,需改动此句 by marco
D2.Items.Add(i.ToString());
for (int i = 1; i <= 31; i++) //每个最多有31日,需改动此句 by marco
D3.Items.Add(i.ToString());
}
}

protected void TJ_Click(object sender, EventArgs e)
{
age.Text = D1.Text +"-"+ D2.Text+ "-" + D3.Text; //转换为日期字符串,需改动此句 by marco
DateTime tempDate = DateTime.MinValue; //定义一个日期时间型的临时变量,存储正确的日期 by marco
if (DateTime.TryParse(age.Text, out tempDate)) //简单选择的日期是否正确,如 闰年2月有29天,大月31日,小月30日 by marco
{
age.Text = tempDate.ToString("yyyy-MM-dd"); //转换成正确的日期格式2011-01-05,防止存入数据库出错 by marco

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=WIN-P4450KF8CH0;database=YAN; Integrated Security = True";
conn.Open();
string strval = "'" + username.Text + "','" + password2.Text + "','" + age.Text + "','" + sex.Text + "','" + eml.Text + "','" + love.Text + "'";
//string strins = "insert into YH (username,password,age,sex,eml,love) values (" + strval + ")"; // 请问各位如何将age以日期的格式输入后台数据库,数据库用的是sql2005,如能帮助,不胜感激!
SqlCommand insCom = new SqlCommand(strins, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = insCom;
da.InsertCommand.ExecuteNonQuery();
conn.Close();
Response.Write("<script language=javascript>alert('添加成功!');</script>");
}
else
{
Response.Write("<script language=javascript>alert('出生日期不正确,请查正!');</script>"); //选择的日期出错提示 by marco
}
}

直接把日期当做字符串一样插入就可以了,只是将age.Text = D1.Text + D2.Text + D3.Text;改成
age.Text = D1.Text +"-"+ D2.Text+"-"+ D3.Text;
其它的可以不用动,另外你的page_load少了 if (!IsPostBack)

D1,D2,D3是DropDownList,能用D1.text取到值吗?D1.SelectedText吧?
第二个for循环,能i能得到12?
age.text =D1.SelectedText + "-" + D2.SelectedText + "-" + D3.SelectedText;
生日转换为日期格式: Convert.toDateTime(age.text);

using
system.data;
using
system.data.sqlclient;
//直接在记事本上面写的,有些关键字拼写可能有误差,请自行在vs.net环境中纠正
private
void
newdata(string
id,string
password)
{
string
sql="insert
into
register(id,password)
values(@id,@password)";
sqlconnection
con=new
sqlconnection(strconnection);
sqlcommand
cmd=new
sqlcommand(sql,con);
cmd.parameters.addwithvalues("@id",id);
cmd.parameters.addwithvalues("@password",password);
if(con.state!=connectionstatus.opened)
{con.open();}
cmd.executenonquery();
if(con.state!=connectionstatus.closed)
{con.close();}
}


岫岩满族自治县19558487758: asp.net 日期转换后插入sql数据库 -
种琴正红: 如果你的age是表示日期而不是表示年龄的话,可参考以下代码:<br> protected void Page_Load(object sender, EventArgs e)<br> {<br> if (!IsPostBack) //令括号内代码只在第一次浏览这个页面的时候执行 by marco<br> {<br> for (int i = 1900; i <...

岫岩满族自治县19558487758: asp.net往SQL数据库插入数据,如何判断是否插入成功? -
种琴正红: 如果你的age是表示日期而不是表示年龄的话,可参考以下代码:<br> protected void page_load(object sender, eventargs e)<br> {<br> if (!ispostback) //令括号内代码只在第一次浏览这个页面的时候执行 by marco<br> {<br> for (int i = 1900; i < ...

岫岩满族自治县19558487758: ASP.net应用中 如何插入oracle时间变量 -
种琴正红: 你没弄清楚to_date()用法,两个参数,一个是字符串,一个是日期格式,Egg:to_char(sysdate,'yyyymmdd') --> 20090711 to_date('20090711','yyyymmdd') insert into tabel(data) values(to_data(''" & textbox1.text & "'')",

岫岩满族自治县19558487758: asp.net页面怎么获取当前的时间然后插入到数据库中?
种琴正红: 这样可以直接在数据库中获取时间了,没有必要在页面中获取时间,然后再传到数据库,在数据库中获取时间的方法为getdate(),如果你要在页面中获取的话,就是DateTime.Now.ToString()这样的形式了,不过从做软件的角度来说还是应该在数据库中处理这样的问题!

岫岩满族自治县19558487758: 在ASP.NET中如何向ACCESS插入DATETIME类型的数据
种琴正红: 这可能是C#中的日期类型无法直接转换成Access中的日期类型OleDbType.DBDate所致 你可以这样写 OleDbParameter parameter = new OleDbParameter(); parameter.OleDbType = OleDbType.DBDate; parameter.Value = DateTime.Now; cmd.Parameters.Add(parameter);

岫岩满族自治县19558487758: asp.net中怎么把时间信息放到数据库中 -
种琴正红: 数据库加时间字段,类型设为Datetime(并可设默认值为getdate(),意味插入或修改数据的系统时间),对应asp.net中使用:如果是输入的利用DateTime.Parse(“时间字符串")或Convert.ToDateTime(“时间字符串") 如果想设置默认时间可在数据库设置默认值或用DateTime.Now设置当前系统时间.至于操作数据库的部分我在这里就不详细说啦!楼主觉的满意就设为满意答案呗!

岫岩满族自治县19558487758: asp 时间加一天,SQL语句怎样写? -
种琴正红: 您可以把日期先取出来,然后再用dataadd函数来得到加一天后的日期,然后再插入到数据库~

岫岩满族自治县19558487758: asp.net怎么在表中插入现在的时间和日期 -
种琴正红: DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网