如何在SQL Server

时间:2016-12-08 21:22:21

标签: c# asp.net sql-server

我目前正在尝试使用Visual Studio在c#中创建一个asp.net Web应用程序。我有一个页面作为父母或孩子的注册页面,具体取决于您选择的单选按钮。注册孩子时,您需要从三个单独的下拉列表中输入DOB。按照目前的情况,我将数据库中的DOB数据类型设置为varchar,这意味着05/05/2005的DOB将'552005'保存在表中。

当我将数据类型设置为datedatetime时,会抛出此错误:

  

System.Data.dll中出现'System.Data.SqlClient.SqlException'类型的异常,但未在用户代码中处理。附加信息:从字符串转换日期和/或时间时转换失败。

这是否意味着我必须在代码中的某个地方将字符串解析为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 = "";
         }              
     }
 }

2 个答案:

答案 0 :(得分:0)

需要约会,约会:

new DateTime(int.Parse(dayDobList.Text), 
             int.Parse(monthDobList.Text), 
             int.Parse(yearDobList.Text))

答案 1 :(得分:0)

您的问题是您的表中有一些旧格式'552005'的数据,因此当您想要更改列类型时会出现错误。所以你有两个选择:

1-从该表中删除任何内容,更改列类型并开始将日期保存为datetimedatetime2

2-将所有当前数据转换为日期字符串,例如。 '05/05/2005 00:00:00'然后将列类型更改为datetime

更新:

另外,不要忘记将数据类型添加到SqlParameters:

SqlParameter dob = new SqlParameter("@sinceDateTime", SqlDbType.DateTime);
dob.Value = new DateTime( Int32.Parse(yearDobList.Text), Int32.Parse(monthDobList.Text), Int32.Parse(dayDobList.Text));
ca.Parameters.Add(dob);