c# - SQL Server中 'date'数据类型的使用方法

标签 c# asp.net sql-server

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

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

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 = "";
         }              
     }
 }

最佳答案

你的问题是你的表中有一些旧格式的数据'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);

关于c# - SQL Server中 'date'数据类型的使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41049041/

相关文章:

c# - 在 DNX PCL 中获取 ApplicationBasePath

c# - 未引用 LINQ to SQL System.Data.Linq.DataContext

asp.net - 这是企图破坏我的 ASP.Net 站点的安全吗?

java - 连接到 SQL Server 时 Android 崩溃

c# 通过环境变量分配版本号

C#重写方法并返回不同的对象

asp.net - 黑客能否从 Azure 网站窃取 dll?

javascript - 在 jQuery 中获取文本框值

sql-server - Transact-SQL 作业权限

sql - 如何通过避免在所有表中重复出现的某些列来从 SQL Server 2008 中的多个表中选择列名