Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将文本框数据保存到多个表中?_C#_Asp.net_Sql - Fatal编程技术网

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

C# 如何将文本框数据保存到多个表中?,c#,asp.net,sql,C#,Asp.net,Sql,我正在做一个就业申请项目。我想将用户的文本框输入保存到多个表中。我下面的代码在构建时没有显示任何警告或错误,但当我去调试时,它只是冻结。我认为部分问题在于我的命令。我不确定是否可以有多个,但我为每个表创建了不同的命令,然后在最后关闭与数据库的连接。如果可以的话,我想把它保留为一个页面,因为我已经做了很多工作来保持这种方式,但也考虑过将它拆分为多个页面,并在每个页面上发出一个新的sqlcommand。代码如下: using System; using System.Collections.Gene

我正在做一个就业申请项目。我想将用户的文本框输入保存到多个表中。我下面的代码在构建时没有显示任何警告或错误,但当我去调试时,它只是冻结。我认为部分问题在于我的命令。我不确定是否可以有多个,但我为每个表创建了不同的命令,然后在最后关闭与数据库的连接。如果可以的话,我想把它保留为一个页面,因为我已经做了很多工作来保持这种方式,但也考虑过将它拆分为多个页面,并在每个页面上发出一个新的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();
            }
        }
    }
}
}

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

根据您的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语句创建一个函数,使其保持良好的分隔?然后,为了进行调试,请尝试一次调用一个函数,然后一次将它们添加到一个函数中,以查看它是否仍然冻结?

以下是您的存储过程:

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代码


你需要调试你的应用程序,找出它在“冻结”时试图做什么。代替
if(Page.IsPostBack)
try
if(!IsPostBack)
。您不需要为每个文本框文本创建变量。对于ex,替换
string FirstName=firstNameTextBox.Text;command.Parameters.AddWithValue(“firstName”,firstName)
您可以编写
命令.Parameters.AddWithValue(“firstName”,firstNameTextBox.Text)。这将帮助您减少编码行,并很容易找到它冻结的位置。
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();
}