我正在尝试在 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 列,以便它接受字母和数字。
任何帮助将不胜感激。