asp.net 日期转换后插入sql数据库
前台日期输入是通过dropdownlist列表框选择在组合构成的后台代码:protectedvoidPage_Load(objectsender,EventArgse){...
前台日期输入是通过dropdownlist列表框选择在组合构成的
后台代码: protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1900; i < 2008; i++)
D1.Items.Add(i.ToString());
for (int i = 1; i < 12; i++)
D2.Items.Add(i.ToString());
for (int i = 1; i < 31; i++)
D3.Items.Add(i.ToString());
}
protected void TJ_click(object sender, EventArgs e)
{
age.Text = D1.Text + D2.Text + D3.Text;
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>"); 展开
后台代码: protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1900; i < 2008; i++)
D1.Items.Add(i.ToString());
for (int i = 1; i < 12; i++)
D2.Items.Add(i.ToString());
for (int i = 1; i < 31; i++)
D3.Items.Add(i.ToString());
}
protected void TJ_click(object sender, EventArgs e)
{
age.Text = D1.Text + D2.Text + D3.Text;
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>"); 展开
展开全部
如果你的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
}
}
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)
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);
第二个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();}
}
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();}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询