我想在SQL數據庫類型中創建一個VarChar的zip代碼,並讓它在數據庫中用字母和數字提交。當郵政編碼具有int SQL數據庫類型時,表單將被提交到數據庫的zip列中,但是當郵政編碼的值爲VarChar 50時,該值不會被提交到名爲zipPostal的數據庫中的新列。在C#中的郵編和aspx爲VarChar而不是int
這裏是在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數據庫中,存在一種用於拉鍊的柱(INT非空)和zipPostal柱(VARCHAR (50),null)。我希望郵政編碼數據在郵政郵遞欄中顯示,以便接受字母和數字。
任何幫助將不勝感激。
它是一個查詢或存儲過程?以及查詢或存儲過程是什麼樣子? – 2013-03-11 16:42:06
你不能只希望SQL Server神奇地爲你做出決定。 – 2013-03-11 16:42:23
請顯示更多你現有的代碼。上面顯示的「片段」實際上還不足以繼續。 – 2013-03-11 16:44:39