我目前正在嘗試使用Visual Studio在c#中創建一個asp.net web應用程序。我有一個頁面充當父母或孩子的註冊頁面,具體取決於您選擇的單選按鈕。註冊孩子時,您需要從三個單獨的下拉列表中輸入DOB。現在,我將DOB數據類型設置爲varchar
,這意味着05/05/2005
的DOB在表中保存爲'552005'
。如何在SQL Server中使用'date'數據類型
當我的數據類型設置爲date
或datetime
它拋出這個錯誤:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Conversion failed when converting date and/or time from character string.
這是否意味着我必須將字符串解析成一個int某處的代碼?如果是這樣,我的代碼中究竟應該如何以及在哪裏?我將包含一些截圖以及我的代碼。
提前致謝!
Showing how my form looks and how DOB currently stores in table
protected void submitBtn_Click(object sender, EventArgs e)
{
SqlConnection connect = new SqlConnection("Data Source=THEBEAST;Initial Catalog=newregDB;Integrated Security=True;Pooling=False");
if (parentRadBtn.Checked)
{
if (firstNameBox.Text == "" || surnameBox.Text == "" || postcodeBox.Text == "" || teleBox.Text == "" || emailBox.Text == "" || userBox.Text == "" || passwordBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
successLabel.Text = ("");
userBox.Text = "";
firstNameBox.Text = "";
surnameBox.Text = "";
postcodeBox.Text = "";
teleBox.Text = "";
emailBox.Text = "";
passwordBox.Text = "";
}
else
{
SqlCommand pa = new SqlCommand("INSERT INTO parent(parentID, firstname, surname, postcode, telephone, email, password) VALUES (@parentID, @firstname, @surname, @postcode, @telephone, @email, @password)", connect);
pa.Parameters.AddWithValue("@parentID", userBox.Text);
pa.Parameters.AddWithValue("@firstname", firstNameBox.Text);
pa.Parameters.AddWithValue("@surname", surnameBox.Text);
pa.Parameters.AddWithValue("@postcode", postcodeBox.Text);
pa.Parameters.AddWithValue("@telephone", teleBox.Text);
pa.Parameters.AddWithValue("@email", emailBox.Text);
pa.Parameters.AddWithValue("@password", passwordBox.Text);
connect.Open();
pa.ExecuteNonQuery();
connect.Close();
}
if (IsPostBack)
{
userBox.Text = "";
firstNameBox.Text = "";
surnameBox.Text = "";
postcodeBox.Text = "";
teleBox.Text = "";
emailBox.Text = "";
passwordBox.Text = "";
}
}
else if (childRadBtn.Checked)
{
if (firstNameBox.Text == "" || dayDobList.Text == "" || monthDobList.Text == "" || yearDobList.Text == "" || genderList.Text == "" || userBox.Text == "" || passwordBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
successLabel.Text = ("");
userBox.Text = "";
firstNameBox.Text = "";
dayDobList.Text = "";
monthDobList.Text = "";
yearDobList.Text = "";
genderList.Text = "";
passwordBox.Text = "";
}
else
{
SqlCommand ca = new SqlCommand("INSERT INTO children(childID, firstname, dob, gender, password) VALUES (@childID, @firstname, @dob, @gender, @password)", connect);
ca.Parameters.AddWithValue("@childID", userBox.Text);
ca.Parameters.AddWithValue("@firstname", firstNameBox.Text);
ca.Parameters.AddWithValue("@dob", dayDobList.Text + monthDobList.Text + yearDobList.Text);
ca.Parameters.AddWithValue("@gender", genderList.Text);
ca.Parameters.AddWithValue("@password", passwordBox.Text);
connect.Open();
ca.ExecuteNonQuery();
connect.Close();
}
if (IsPostBack)
{
userBox.Text = "";
firstNameBox.Text = "";
dayDobList.Text = "";
monthDobList.Text = "";
yearDobList.Text = "";
genderList.Text = "";
passwordBox.Text = "";
}
}
}
你怎麼知道從2016年11月2日2016年1月12日? – Hogan
爲什麼你必須把日期作爲nvarchar?爲什麼不「約會」?您輸入的值將採用格式「2014-01-01」。 – Auguste
Auguste,正如我在原始問題中所說的,當我將其更改爲鍵入'date'或'datetime'時,我收到一條錯誤消息。我認爲通過改變日期類型,它會自動接受輸入'552015',並使其5/5/2015或沿着它們行。 – ACostea