将信息从ASP.NET网页插入数据库

时间:2015-03-15 22:38:53

标签: c# asp.net sql-server database

我正在尝试允许用户使用教科书和下拉菜单输入员工信息。由于某些原因我目前看不到,它没有正确插入数据库。我尝试了SqlCommandMode中的所有查询,它们都正常工作,但那是在我使用函数中的参数之前。这是我在DBconn.cs页面中的代码

    public void insertEmployeeInfo(string firstName, string middleName, string lastName, string address1, string address2, string city, string postalCode, string state)
    {
        SqlConnection conn = new SqlConnection(strConnString);
        string sql1 = "INSERT INTO Person.BusinessEntity(ModifiedDate) VALUES (GETDATE())";
        string sql2 = "INSERT INTO Person.Person (ModifiedDate, BusinessEntityID, PersonType, Title, FirstName, MiddleName, LastName, Suffix) VALUES (GETDATE(), (SELECT MAX(PB.BusinessEntityID) AS IDNUM FROM PErson.BusinessEntity AS PB), 'EM', @title, @firstName, @lastName, @suffix)";
        string sql3 = "INSERT INTO Person.Address (AddressLine1, AddressLine2, City, StateProvinceID, PostalCode, ModifiedDate) VALUES (@address1, @address2, @city, @postalCode, (SELECT StateProvinceID FROM Person.StateProvince WHERE StateProvinceCode=@state))";

        try
        {
            conn.Open();
            SqlCommand cmd1 = new SqlCommand(sql1, conn);
            SqlCommand cmd2 = new SqlCommand(sql2, conn);
            SqlCommand cmd3 = new SqlCommand(sql3, conn);
            SqlParameter[] personParams = new SqlParameter[3];
            SqlParameter[] addressParams = new SqlParameter[5];

            //Params for inserts into Person.Person
            personParams[0] = new SqlParameter("@firstName", SqlDbType.VarChar, 50);
            personParams[1] = new SqlParameter("@middleName", SqlDbType.VarChar, 50);
            personParams[2] = new SqlParameter("@lastName", SqlDbType.VarChar, 50);

            //Params for inserts into Person.Address
            addressParams[0] = new SqlParameter("@address1", SqlDbType.VarChar, 50);
            addressParams[1] = new SqlParameter("@address2", SqlDbType.VarChar, 50);
            addressParams[2] = new SqlParameter("@city", SqlDbType.VarChar, 50);
            addressParams[3] = new SqlParameter("@postalCode", SqlDbType.VarChar, 50);
            addressParams[4] = new SqlParameter("@state", SqlDbType.VarChar, 50);

            personParams[0].Value = firstName;
            personParams[1].Value = middleName;
            personParams[2].Value = lastName;

            addressParams[0].Value = address1;
            addressParams[1].Value = address2;
            addressParams[2].Value = city;
            addressParams[3].Value = postalCode;
            addressParams[4].Value = state;

            //insert ModifiedDate into Person.BusinessEntity
            cmd1.CommandType = CommandType.Text;
            cmd1.ExecuteNonQuery();

            for (int i = 0; i < personParams.Length; i++)
            {
                cmd2.Parameters.Add(personParams[i]);
            }

            //insert new employee name information into Person.Person
            cmd2.CommandType = CommandType.Text;
            cmd2.ExecuteNonQuery();

            for (int i = 0; i < addressParams.Length; i++)
            {
                cmd3.Parameters.Add(addressParams[i]);
            }

            //insert new employee address information
            cmd3.CommandType = CommandType.Text;
            cmd3.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            conn.Close();
        }
    }

然后这就是我用来在page.aspx.cs页面的按钮上调用sql

    protected void Insert_Click(object sender, EventArgs e)
{
    dbcon.insertEmployeeInfo(TxtFirstName.Text, TxtMiddleName.Text, TxtLastName.Text, TxtAddress1.Text, TxtAddress2.Text, TxtCity.Text, TxtPostalCode.Text, State.Text);
    Response.Write("Record was successfully added!");
}

1 个答案:

答案 0 :(得分:2)