1

我正在尝试在 SQL 数据库类型中将邮政编码设为 VarChar,并将其与字母和数字一起提交到数据库中。当邮政编码具有 int SQL 数据库类型时,表单将提交到数据库中的 zip 列,但当邮政编码的值为 VarChar 50 时,该值不会提交到名为 zipPostal 的数据库中的新列。

这是aspx中的代码:

    <asp:Label runat="server" ID="zipLabel" AssociatedControlID="zip">Zip/Postal 
     Code</asp:Label>     
    <asp:TextBox ID="zip" name="zip" runat="server"></asp:TextBox>

以下是 C# 中的代码片段:

    /// Form post data from registration form
    string username = Request.Form["UserName"];
    string first = Request.Form["first"];
    string last = Request.Form["last"];
    string zip = Request.Form["zip"];

    sqlCommand.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar, 50,  
    ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, emails));

    sqlCommand.Parameters.Add(new SqlParameter("@username", SqlDbType.VarChar, 15,   
    ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, username));

    sqlCommand.Parameters.Add(new SqlParameter("@firstName", SqlDbType.VarChar, 20,    
    ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, first));

    sqlCommand.Parameters.Add(new SqlParameter("@lastName", SqlDbType.VarChar, 20,      
    ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, last));


sqlCommand.Parameters.Add(new SqlParameter("@zip", SqlDbType.VarChar, 50, 
    ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, zip));

SQL 命令类型:

     int chk = sqlCommand.ExecuteNonQuery();

     public string CheckEmail(string Email)
     {

     string returnValue = string.Empty;
     SqlConnection conn = new SqlConnection(strConnection);
     SqlCommand sqlCommand = new SqlCommand

     ("CheckEmailAvailability", conn);
     try
     {
        sqlCommand.CommandType = 

     CommandType.StoredProcedure;
     sqlCommand.Parameters.AddWithValue("@Email", 

     Email.Trim());
        conn.Open();
        returnValue = sqlCommand.ExecuteScalar().ToString();
        conn.Close();
     }
     catch
     {

     }

     finally
     {
        conn.Close();
        sqlCommand.Dispose();
     }

     return returnValue;
     }

////

    public string CheckUsername(string Username)
    {

    string returnValue = string.Empty;
    SqlConnection conn = new SqlConnection(strConnection);
    SqlCommand sqlCommand = new SqlCommand

    ("CheckUsernameAvailability", conn);
    try
    {
        sqlCommand.CommandType = 

    CommandType.StoredProcedure;
        sqlCommand.Parameters.AddWithValue("@UserName", 

    Username.Trim());
        conn.Open();
        returnValue = sqlCommand.ExecuteScalar().ToString();
        conn.Close();
    }
    catch
    {

    }

    finally
    {
        conn.Close();
        sqlCommand.Dispose();
    }

    return returnValue;
    }

    }

//存储过程

   if (username != "")
   {
        string checkUN = CheckUsername(username);

        if (checkUN == "true")
        {
            Label2.Text = "Username already exists.";
            gotoauth = 0;
            return;

        }
    }
    //[ckc] end check username 

    if (password != confirmpwd)
    {
        Label2.Text = "The Password and Confirmation 

   Password must match.";
       gotoauth = 0;
        return;
    }



   //connection with db and call procedure.
    SqlConnection conn = new SqlConnection(strConnection);
    SqlCommand sqlCommand = new SqlCommand

    ("customers_insert", conn);
    sqlCommand.CommandType = CommandType.StoredProcedure;
    conn.Open();


    try
    {


    if (gotoauth == 0) 
     {}


    else
        {



var customerGateway = new 

    CustomerGateway(_loginApikey, _TransactionKey, 

    ServiceMode.Live);
    var NewCustomer = customerGateway.CreateCustomer

    (emails, "Live User Account");
    var datetime = DateTime.Now.ToString(); 

string proid = NewCustomer.ProfileID;
    Session["Customerproid"] = proid;

/// SQL过程

    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[customers_insert]
    @email varchar(50) ,
    @username varchar(15) ,
    @firstName varchar(20) ,
    @lastName varchar(20) ,
    @country varchar(50) ,
    @address varchar(100) ,
    @city varchar(100) ,
    @state varchar(50) ,
    @account bigint = null ,
    @profileID bigint = null ,
    @password varchar(20) ,
    @customerNumber int = null ,
    @question varchar(50)  ,
    @answer varchar(50) ,
    @dob varchar(50) ,
    @AccountNumber varchar(50) = null ,
    @PinNumber varchar(50) = null ,
    @PhoneNumber varchar(50) = null, 
    @ApprovalID int,
    @DateTime varchar (50) = null,
    @thoughts varchar (100) = null,
    @zip varchar (50) = null


    AS

    INSERT [dbo].[customers]
    (
    [email],
    [username],
    [firstName],
    [lastName],
    [country],
    [address],
    [city],
    [state],
    [account],
    [profileID],
    [password],
    [customerNumber],
    [question],
    [answer],
    [dob],
    [AccountNumber],
    [PinNumber],
    [PhoneNumber],
    [ApprovalID],
    [DateTime],
    [thoughts],
    [zip]


    )
    VALUES
    (
    @email,
    @username,
    @firstName,
    @lastName,
    @country,
    @address,
    @city,
    @state,
    @account,
    @profileID,
    @password,
    @customerNumber,
    @question,
    @answer,
    @dob,
    @AccountNumber,
    @PinNumber,
    @PhoneNumber,
    @ApprovalID,
    @DateTime,
    @thoughts,
    @zip

    )

在 SQL 数据库中,有一列用于 zip(int,not null)和一列用于 zipPostal(varChar(50),null)。我希望邮政编码数据进入 zipPostal 列,以便它接受字母和数字。

任何帮助将不胜感激。

4

2 回答 2

1

您很可能有一个查询——在后端数据库中以存储过程的形式,或者在你的 c# 代码中以 a 的形式SqlCommand——引用该zip列。您需要更改该查询/过程以引用正确的字段。如果您的代码说该值应该插入到zip中,那么它永远不会插入到zipPostal.

编辑:现在我们可以看到您的存储过程,它确认这是问题所在。以下是解决方法:

ALTER PROCEDURE [dbo].[customers_insert]
@email varchar(50) ,
@username varchar(15) ,
@firstName varchar(20) ,
@lastName varchar(20) ,
@country varchar(50) ,
@address varchar(100) ,
@city varchar(100) ,
@state varchar(50) ,
@account bigint = null ,
@profileID bigint = null ,
@password varchar(20) ,
@customerNumber int = null ,
@question varchar(50)  ,
@answer varchar(50) ,
@dob varchar(50) ,
@AccountNumber varchar(50) = null ,
@PinNumber varchar(50) = null ,
@PhoneNumber varchar(50) = null, 
@ApprovalID int,
@DateTime varchar (50) = null,
@thoughts varchar (100) = null,
@zip varchar (50) = null


AS

INSERT [dbo].[customers]
(
[email],
[username],
[firstName],
[lastName],
[country],
[address],
[city],
[state],
[account],
[profileID],
[password],
[customerNumber],
[question],
[answer],
[dob],
[AccountNumber],
[PinNumber],
[PhoneNumber],
[ApprovalID],
[DateTime],
[thoughts],
[zipPostal] /*Here's what I changed*/


)
VALUES
(
@email,
@username,
@firstName,
@lastName,
@country,
@address,
@city,
@state,
@account,
@profileID,
@password,
@customerNumber,
@question,
@answer,
@dob,
@AccountNumber,
@PinNumber,
@PhoneNumber,
@ApprovalID,
@DateTime,
@thoughts,
@zip

)

您也可以以更复杂的方式编辑它,将邮政编码放入ZipzipPostal取决于其值,但我认为您应该将所有内容放入zipPostal. 您应该将所有数据放在一个地方,并且邮政编码数据应始终存储为字符串,而不是数字,正如 Galactic Cowboy 在下面的评论中所写。

于 2013-03-11T16:50:23.283 回答
0

您还没有发布相关的 SQL 代码。

您已经发布了调用“CheckUserNameAvailability”存储过程的代码和另一个调用名为“CheckEmailAvailability”的存储过程的代码。这些存储过程似乎都与尝试设置邮政编码的代码无关。

我怀疑您正在调用另一个以@Zip 作为参数的存储过程。必须编辑该存储过程以更新正确的列。

更新:

改变

INSERT [dbo].[customers]
(
[email],
[username],
[firstName],
[lastName],
[country],
[address],
[city],
[state],
[account],
[profileID],
[password],
[customerNumber],
[question],
[answer],
[dob],
[AccountNumber],
[PinNumber],
[PhoneNumber],
[ApprovalID],
[DateTime],
[thoughts],
[zip]


)
VALUES
(
@email,
@username,
@firstName,
@lastName,
@country,
@address,
@city,
@state,
@account,
@profileID,
@password,
@customerNumber,
@question,
@answer,
@dob,
@AccountNumber,
@PinNumber,
@PhoneNumber,
@ApprovalID,
@DateTime,
@thoughts,
@zip

)

INSERT [dbo].[customers]
(
[email],
[username],
[firstName],
[lastName],
[country],
[address],
[city],
[state],
[account],
[profileID],
[password],
[customerNumber],
[question],
[answer],
[dob],
[AccountNumber],
[PinNumber],
[PhoneNumber],
[ApprovalID],
[DateTime],
[thoughts],
[zipPostal]


)
VALUES
(
@email,
@username,
@firstName,
@lastName,
@country,
@address,
@city,
@state,
@account,
@profileID,
@password,
@customerNumber,
@question,
@answer,
@dob,
@AccountNumber,
@PinNumber,
@PhoneNumber,
@ApprovalID,
@DateTime,
@thoughts,
@zip

)
于 2013-03-11T17:29:15.657 回答