将数据从文本框中保存到sql数据库asp.net C#

时间:2016-08-16 10:27:07

标签: c# asp.net sql-server

我尝试了很多方法将文本框文本保存到SQL数据库中。我只想将文本框中的一些文本保存到SQL数据库中。我在用 Microsoft Visual Studio 2010和C#编程语言。 这是我目前的代码:

protected void BtnSave_Click(object sender, EventArgs e)
    {
        string Name = TxtFirstName.Text;
        string Address = TxtDOB.Text;
        string DOB = TxtDOB.Text;
        string Occupation = TxtOccupation.Text;
        string Home = TxtHome.Text;
        string Work = TxtWork.Text;
        string Mobile = TxtMobile.Text;
        string Email = TxtEmail.Text;
        string SportActivities = TxtSportActivities.Text;

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientsConnectionString"].ConnectionString)) ;
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO [ClientDetails] ([Name], [Address], [DOB], [Occupation], [Home], [Work], [Mobile], [Email], [SportActivities]) VALUES ( @Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities)")) ;
            {
                cmd.connection = con;
                con.Open();
                cmd.Parameters.AddWithValue("@Name", TxtFirstName.Text);
                cmd.Parameters.AddWithValue("@Address", TxtAddress.Text);
                cmd.Parameters.AddWithValue("@DOB", TxtDOB.Text);
                cmd.Parameters.AddWithValue("@Occupation", TxtOccupation.Text);
                cmd.Parameters.AddWithValue("@Home", TxtHome.Text);
                cmd.Parameters.AddWithValue("@Work", TxtWork.Text);
                cmd.Parameters.AddWithValue("@Mobile", TxtMobile.Text);
                cmd.Parameters.AddWithValue("@Email", TxtEmail.Text);
                cmd.Parameters.AddWithValue("@SportActivities", TxtSportActivities.Text);
                cmd.ExecuteNonQuery();
                con.close();
            }
        }
    }

protected void Button1_Click(object sender, EventArgs e)
        {
            string Name = TxtFirstName.Text;
            string Address = TxtDOB.Text;
            string DOB = TxtDOB.Text;
            string Occupation = TxtOccupation.Text;
            string Home = TxtHome.Text;
            string Work = TxtWork.Text;
            string Mobile = TxtMobile.Text;
            string Email = TxtEmail.Text;
            string SportActivities = TxtSportActivities.Text;

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientDetailsConnectionString"].ConnectionString);

            SqlCommand cmd = new SqlCommand("INSERT INTO [ClientDetails] ([ID], [Name], [Address], [DOB], [Occupation], [Home], [Work], [Mobile], [Email], [SportActivities]) VALUES (@ID, @Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities)");

            cmd.Connection = con;
            con.Open();
            cmd.Parameters.AddWithValue("@ID", TxtID.Text);
            cmd.Parameters.AddWithValue("@Name", TxtFirstName.Text);
            cmd.Parameters.AddWithValue("@Address", TxtAddress.Text);
            cmd.Parameters.AddWithValue("@DOB", TxtDOB.Text);
            cmd.Parameters.AddWithValue("@Occupation", TxtOccupation.Text);
            cmd.Parameters.AddWithValue("@Home", TxtHome.Text);
            cmd.Parameters.AddWithValue("@Work", TxtWork.Text);
            cmd.Parameters.AddWithValue("@Mobile", TxtMobile.Text);
            cmd.Parameters.AddWithValue("@Email", TxtEmail.Text);
            cmd.Parameters.AddWithValue("@SportActivities", TxtSportActivities.Text);
            cmd.ExecuteNonQuery();
            con.Close();
        }

我修复了我的代码。我所做的只是在aspx.cs文件中将'CodeBehind'更改为CodeFile'并修复了一些小错误。这是我的工作代码: 我所做的就是删除括号,因为代码没有识别'cmd'并添加'ID'字段,因为那是我数据库的主键所以它必须在那里。一切都很好。谢谢你的帮助。

0 个答案:

没有答案