如何将文本框数据保存到多个表?

时间:2013-12-02 11:24:45

标签: c# asp.net sql

我正在开展一个就业申请项目。我想将用户的文本框输入保存到多个表。我的代码在构建时没有显示警告或错误,但是当我进行调试时,它只是冻结。我认为部分问题是我的命令。我不确定我是否可以拥有多个,但是我为每个表创建了一个不同的命令,然后在最后关闭与数据库的连接。我想把它作为一个页面保留,如果可以的话,因为我已经做了很多工作来保持这种方式,但也考虑将它分成多个页面,并且只是在每个页面上发布一个新的sqlcommand。这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace SPApplication
{
public partial class Index : System.Web.UI.Page
{

    protected void Button1_Click1(object sender, EventArgs e)
    {

        if (Page.IsPostBack)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Conn2"].ConnectionString;
            string sqlquery = "INSERT INTO [Applicant] (firstName, lastName, streetAddress, city, ZIP, phone, email) VALUES (@firstName, @lastName, @streetAddress, @city, @zip, @phone, @email)";
            string sqlquery1 = "INSERT INTO [College] (CollegeName, CollegeCity, Major, CGPA, CGraduate) VALUES (@collegeName, @collegeCity, @major, @cGPA, @cGraduate)";
            string sqlquery2 = "INSERT INTO [HS] (HSName, HSCity, HSGradYear, HSGraduate) VALUES (@hsName, @hsCity, @hsGradYear, @hsGraduate)";
            string sqlquery3 = "INSERT INTO [Company] (CAddress, CCity, CState, CZIP, SupervisorName, SupervisorPhone, SupervisorEmail) VALUES (@cAddress, @cCity, @cStae, @cZIP, @supervisorName, @supervisorPhone, @supervisorEmail)";
            string sqlquery4 = "INSERT INTO [References] (RName, RPhone, REmail, Relationship) VALUES (@rName, @rPhone, @rEmail, @relationship)";
            SqlCommand command = new SqlCommand(sqlquery, conn);
            SqlCommand command1 = new SqlCommand(sqlquery1, conn);
            SqlCommand command2 = new SqlCommand(sqlquery2, conn);
            SqlCommand command3 = new SqlCommand(sqlquery3, conn);
            SqlCommand command4 = new SqlCommand(sqlquery4, conn);

            try
            {
                //open the connection to the database.
                conn.Open();
                //First Name
                string FirstName = firstNameTextBox.Text;
                command.Parameters.AddWithValue("firstName", FirstName);
                //Last Name
                string LastName = lastNameTextBox.Text;
                command.Parameters.AddWithValue("lastName", LastName);
                //Address
                string StreetAddress = streetAddressTextBox.Text;
                command.Parameters.AddWithValue("streetAddress", StreetAddress);
                //City
                string City = CityTextBox.Text;
                command.Parameters.AddWithValue("city", City);
                //ZIP
                string zip = ZIPTextBox.Text;
                command.Parameters.AddWithValue("zip", zip);
                //Phone Number
                string PhoneNumber = telTextBox.Text;
                command.Parameters.AddWithValue("phone", PhoneNumber);
                //Email
                string Email = emailTextBox.Text;
                command.Parameters.AddWithValue("email", Email);
                //Command ExecuteNonQuery to return number of rows affected. 
                command.ExecuteNonQuery();
                //*************************************************************************************************
                //Add information to the College table
                //College Name
                string CollegeName = CollegeNameTextBox.Text;
                command1.Parameters.AddWithValue("collegeName", CollegeName);
                //College City
                string CollegeCity = CollegeCityTextBox.Text;
                command1.Parameters.AddWithValue("collegeCity", CollegeCity);
                //College State
                string CollegeState = CollegeStateTextBox.Text;
                command1.Parameters.AddWithValue("collegeState", CollegeName);
                //College Grad Year
                string CollegeGradYear = CollegeGradYearTextBox.Text;
                command1.Parameters.AddWithValue("collegeGradYear", CollegeName);
                //College Graduate    
                string Graduate;
                if (CollegeGradCheckBox.Checked)
                {
                    Graduate = "Yes";
                }
                else
                {
                    Graduate = "No";
                }
                command1.Parameters.AddWithValue("cGraduate", Graduate);
                //Command1 ExecuteNonQuery to return number of rows affected. 
                command1.ExecuteNonQuery();
                //******************************************************************************************************
                //Insert information into the HS table. 
                //High School Name
                string HSName = HSNameTextBox.Text;
                command2.Parameters.AddWithValue("hsName", HSName);
                //High School City
                string HSCity = HSCityTextBox.Text;
                command2.Parameters.AddWithValue("hsCity", HSCity);
                //High School State
                string HSState = HSStateTextBox.Text;
                command2.Parameters.AddWithValue("hsState", HSState);
                //High School Graduation Year
                string HSGradYear = HSGradYearTextBox.Text;
                command2.Parameters.AddWithValue("hsGradYear", HSGradYear);
                //High School Graduate? 
                string HSGraduate;
                if (HSGradCheckBox.Checked)
                {
                    HSGraduate = "Yes";
                }
                else
                {
                    HSGraduate = "No";
                }
                command2.Parameters.AddWithValue("hsGraduate", HSGraduate);
                //Command 2 Execute NonQuery to return number of rows affected
                command2.ExecuteNonQuery();
                //***************************************************************************************
                //Insert information into the Employers table
                //Employer 1 Name
                string CName1 = CNameTextBox.Text;
                command3.Parameters.AddWithValue("cName", CName1);
                //Employer 1 Address
                string CAddress1 = CAddressTextBox.Text;
                command3.Parameters.AddWithValue("cAddress", CAddress1);
                //Employer 1 City
                string CCity1 = CCityTextBox.Text;
                command3.Parameters.AddWithValue("cCity", CCity1);
                //Employer 1 State
                string CState1 = CStateTextBox.Text;
                command3.Parameters.AddWithValue("cState", CState1);
                //Employer 1 ZIP
                string CZIP1 = CZIPTextBox.Text;
                command3.Parameters.AddWithValue("cZIP", CZIP1);
                //Employer 1 Supervisor Name
                string SupervisorName = SupNameTextBox.Text;
                command3.Parameters.AddWithValue("supervisorName", SupervisorName);
                //Employer 1 Supervisor Phone Number
                string SupervisorPhone = SupPhoneTextBox.Text;
                command3.Parameters.AddWithValue("supervisorPhone", SupervisorPhone);
                //Employer 1 Supervisor Email Address
                string SupervisorEmail = SupEmailTextBox.Text;
                command3.Parameters.AddWithValue("supervisorEmail", SupervisorEmail);
                //Ok to Contact?
                string OkToContact1;
                if (OkToContactCheckBox.Checked)
                {
                    OkToContact1 = "Yes";
                }
                else
                {
                    OkToContact1 = "No";
                }
                command3.Parameters.AddWithValue("okToContact", OkToContact1);
                //**************************************************************************************************
                //Employer 2 Name
                string CName2 = CName2TextBox.Text;
                command3.Parameters.AddWithValue("cName", CName2);
                //Employer 2 Address
                string CAddress2 = CAddress2TextBox.Text;
                command3.Parameters.AddWithValue("cAddress", CAddress2);
                //Employer 2 City
                string CCity2 = CCity2TextBox.Text;
                command3.Parameters.AddWithValue("cCity", CCity2);
                //Employer 2 State
                string CState2 = CState2TextBox.Text;
                command3.Parameters.AddWithValue("cState", CState2);
                //Employer 2 ZIP
                string CZIP2 = CZIP2TextBox.Text;
                command3.Parameters.AddWithValue("cZIP", CZIP2);
                //Employer 2 Supervisor Name
                string SupervisorName2 = SupName2TextBox.Text;
                command3.Parameters.AddWithValue("supervisorName", SupervisorName2);
                //Employer 2 Supervisor Phone Number
                string SupervisorPhone2 = SupPhone2TextBox.Text;
                command3.Parameters.AddWithValue("supervisorPhone", SupervisorPhone2);
                //Employer 2 Supervisor Email Address
                string SupervisorEmail2 = SupEmail2TextBox.Text;
                command3.Parameters.AddWithValue("supervisorEmail", SupervisorEmail2);
                //Ok to Contact?
                string OkToContact2;
                if (OkToContact2CheckBox.Checked)
                {
                    OkToContact2 = "Yes";
                }
                else
                {
                    OkToContact2 = "No";
                }
                command3.Parameters.AddWithValue("okToContact", OkToContact2);

                //********************************************************************************************
                //Employer 3 Name
                string CName3 = CName3TextBox.Text;
                command3.Parameters.AddWithValue("cName", CName3);
                //Employer 3 Address
                string CAddress3 = CAddress3TextBox.Text;
                command3.Parameters.AddWithValue("cAddress", CAddress3);
                //Employer 3 City
                string CCity3 = CCity3TextBox.Text;
                command3.Parameters.AddWithValue("cCity", CCity3);
                //Employer 3 State
                string CState3 = CState3TextBox.Text;
                command3.Parameters.AddWithValue("cState", CState3);
                //Employer 3 ZIP
                string CZIP3 = CZIP3TextBox.Text;
                command3.Parameters.AddWithValue("cZIP", CZIP3);
                //Employer 3 Supervisor Name
                string SupervisorName3 = SupName3TextBox.Text;
                command3.Parameters.AddWithValue("supervisorName", SupervisorName3);
                //Employer 3 Supervisor Phone Number
                string SupervisorPhone3 = SupPhone3TextBox.Text;
                command3.Parameters.AddWithValue("supervisorPhone", SupervisorPhone3);
                //Employer 3 Supervisor Email Address
                string SupervisorEmail3 = SupEmail3TextBox.Text;
                command3.Parameters.AddWithValue("supervisorEmail", SupervisorEmail3);
                //Ok to contact? 
                string OkToContact3;
                if (OkToContact3CheckBox.Checked)
                {
                    OkToContact3 = "Yes";
                }
                else
                {
                    OkToContact3 = "No";
                }
                command4.Parameters.AddWithValue("okToContact", OkToContact3);
                //Command3 Execute Non Query to return number of rows affected
                command3.ExecuteNonQuery();
                //*****************************************************************************************************
                //Insert information into the References database
                //Reference 1 Name
                string RName1 = RName1TextBox.Text;
                command4.Parameters.AddWithValue("rName", RName1);
                //Reference 1 Phone
                string RPhone1 = RPhone1TextBox.Text;
                command4.Parameters.AddWithValue("rPhone", RPhone1);
                //Reference 1 Email
                string REmail1 = REmail1TextBox.Text;
                command4.Parameters.AddWithValue("rEmail", REmail1);
                //Reference 1 Relationship
                string Relationship1 = Relationship1TextBox.Text;
                command4.Parameters.AddWithValue("relationship", Relationship1);
                //
                //
                //
                //Reference 2 Name
                string RName2 = RName2TextBox.Text;
                command4.Parameters.AddWithValue("rName", RName2);
                //Reference 2 Phone
                string RPhone2 = RPhone2TextBox.Text;
                command4.Parameters.AddWithValue("rPhone", RPhone2);
                //Reference 2 Phone Number
                string REmail2 = REmail2TextBox.Text;
                command4.Parameters.AddWithValue("rEmail", REmail2);
                //Reference 2 Email
                string Relationship2 = Relationship2TextBox.Text;
                command4.Parameters.AddWithValue("relationship", Relationship2);
                //Reference 2 Relationship
                //
                //Reference 2 Name
                string RName3 = RName3TextBox.Text;
                command4.Parameters.AddWithValue("rName", RName3);
                //Reference 2 Phone
                string RPhone3 = RPhone3TextBox.Text;
                command4.Parameters.AddWithValue("rPhone", RPhone3);
                //Reference 3 Email
                string REmail3 = REmail3TextBox.Text;
                command4.Parameters.AddWithValue("rEmail", REmail3);
                //Reference 3 Relationship
                string Relationship3 = Relationship3TextBox.Text;
                command4.Parameters.AddWithValue("relationship", Relationship3);
                //Execute Non Query to return the amount of rows affected
                command4.ExecuteNonQuery();
            }
            //
            //close the connection to the database.

            catch (System.Data.SqlClient.SqlException exception)
            {
                string msg = "Some information has not been entered. Please go back and correct information.";
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
}

感谢大家的帮助。请原谅我,我是新手,不是专业人士,而是学生。我拿出了ExecuteNonQuery();对于每个块,页面将加载,但不会将任何信息保存到数据库。我现在要尝试你的建议,我回到这里,看看你们有什么话要说。

2 个答案:

答案 0 :(得分:1)

这是您的存储过程:

CREATE PROCEDURE [proSingle]
    @firstName varchar(255), 
    @lastName varchar(255), 
    @streetAddress varchar(255), 
    @city varchar(255), 
    @zip varchar(255), 
    @phone varchar(255), 
    @email varchar(255), 
    @collegeName varchar(255), 
    @collegeCity varchar(255), 
    @major varchar(255),  
    @cGPA varchar(255),  
    @cGraduate varchar(255),  /* carry on making variables  for Table HS , Company , Refrence */
AS  
BEGIN
    INSERT INTO [Applicant] (firstName, lastName, streetAddress, city, ZIP, phone, email) 
    VALUES (@firstName, @lastName, @streetAddress, @city, @zip, @phone, @email)

    INSERT INTO [College] (CollegeName, CollegeCity, Major, CGPA, CGraduate) 
    VALUES (@collegeName, @collegeCity, @major, @cGPA, @cGraduate)

    INSERT INTO [HS] (HSName, HSCity, HSGradYear, HSGraduate) 
    VALUES (@hsName, @hsCity, @hsGradYear, @hsGraduate)

    INSERT INTO [Company] (CAddress, CCity, CState, CZIP, SupervisorName, SupervisorPhone, SupervisorEmail) 
    VALUES (@cAddress, @cCity, @cStae, @cZIP, @supervisorName, @supervisorPhone, @supervisorEmail)

    INSERT INTO [References] (RName, RPhone, REmail, Relationship) 
    VALUES (@rName, @rPhone, @rEmail, @relationship)
END

现在在您的C#代码中

SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Conn2"].ConnectionString;

string sqlquery = "exec proSingle @firstName, @lastName, @streetAddress, @city, @zip, @phone, @email";
// along with all the other variable you created in stored procedure

SqlCommand command = new SqlCommand(sqlquery, conn);

try
{
    // open the connection to the database.
    conn.Open();
    string FirstName = firstNameTextBox.Text;

    // make sure to use @ variable name
    command.Parameters.AddWithValue("@firstName", FirstName);

    // last name
    string LastName = lastNameTextBox.Text;
    command.Parameters.AddWithValue("@lastName", LastName);

    //add all the Variable as you have been adding but only to command

    command.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException exception)
{
    string msg = "Some information has not been entered. Please go back and correct information.";
}
finally
{
    conn.Close();
}

答案 1 :(得分:0)

您应该能够将INSERTS串在一起作为单个命令,具体取决于您的SQL设置,用分号分隔的命令。

string sqlquery = "INSERT INTO [Applicant] (firstName, lastName, streetAddress, city, ZIP, phone, email) VALUES (@firstName, @lastName, @streetAddress, @city, @zip, @phone, @email); INSERT INTO [College] (CollegeName, CollegeCity, Major, CGPA, CGraduate) VALUES (@collegeName, @collegeCity, @major, @cGPA, @cGraduate)";

另一方面,如果你想把它作为一个单独的页面,为什么不为每个insert语句创建一个函数来保持它们完全分开?然后为了调试,尝试一次调用一个函数,然后一次添加一个函数以查看它是否仍然冻结?