gpt4 book ai didi

c# - 将出生日期插入sql server

转载 作者:行者123 更新时间:2023-12-03 12:21:23 25 4
gpt4 key购买 nike

我为出生日期编写了一个JavaScript,调试时您可以在三个下拉框中选择您的出生日期。一个是几天,一个是几个月,一个是几年。我正在研究 C# asp.net。问题是当我单击测试(提交或确认)时,日期不会被记录到数据库表中。就填空了!任何帮助将不胜感激..

代码如下:

出生日期.js:

function date_populate(dayfield, monthfield, yearfield) 
{
var today = new Date();
var dayfield = document.getElementById(dayfield);
var monthfield = document.getElementById(monthfield);
var yearfield = document.getElementById(yearfield);

for (var i = 0; i < 32; i++)
{
dayfield.options[i] = new Option(i , i + 1)
dayfield.options[today.getDate()] = new Option(today.getDate(), today.getDate(), true, true)
}

for (var m = 0; m < 12; m++)
{
monthfield.options[m] = new Option(monthtext[m], monthtext[m])
monthfield.options[today.getMonth()] = new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true)
}

var thisyear = today.getFullYear()
for (var y = 0; y < 100; y++)
{
yearfield.options[y] = new Option(thisyear, thisyear)
thisyear -= 1
}

yearfield.options[0] = new Option(today.getFullYear(), today.getFullYear(), true, true)
}

表单.asp.cs

protected void Button1_Click(object sender, EventArgs e)
{
String D, M, Y, Full;

D = Day.Value.ToString();
M = Month.Value.ToString();
Y = Year.Value.ToString();

Full = D + "/" + M + "/" + Y;
}

最佳答案

不要将日期构造为字符串。将它们作为日期传递。例如:

DateTime dob = new DateTime(Year.Value, Month.Value, Day.Value);
...
cmd.Parameters.AddWithValue("dob", dob);

当您想要引用出生日期时,cmd.CommandText 涉及 @dob

关于c# - 将出生日期插入sql server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24427981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com