.Fill Overflow on Phone 数字字符串自动转换为 Int
.Fill Overflow on Phone Number string Auto-converting to Int
我正在根据正确的 phone 号码格式检查用户上传的 phone 号码列表和“请勿致电”phone 的数据库 table数字并返回格式不正确或 table 中不存在的列表条目。 phone 数字都定义为 varchar,但 .Fill 会自动将其转换为 int 并引发溢出错误。有谁知道如何获取 .Fill 以将 phone 数字保留为 varchar?我试过预先声明填充的 table 列并在存储过程中将 phone 数字输出定义为 bigint,但这些都没有用。应用程序中的所有其他代码都可以正常工作。提前致谢!
错误消息,其中 'x' 表示一个 phone 数字:
System.Data.SqlClient.SqlException (0x80131904): varchar 值 'xxxxxxxxxx' 的转换溢出了一个 int 列。声明已终止。
C#:
System.Data.DataTable cleanedList = new System.Data.DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("dbo.uspCleanScrubberLeads", conn))
using (var da = new SqlDataAdapter(cmd))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ProcedureDate", DateTime.Now);
cmd.Parameters.AddWithValue("@LeadListTable", dt);
da.Fill(cleanedList);//Error occurs here!!!
}
SQL:
ALTER PROCEDURE [dbo].[uspCleanScrubberLeads]
@ProcedureDate datetime2(0),
@LeadListTable AS LeadListTable READONLY
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #CleanedLeads (PhoneNumber varchar(20), Reason varchar(200))
INSERT INTO #CleanedLeads (PhoneNumber, Reason)
SELECT PhoneNumber, '' FROM @LeadListTable AS t1
UPDATE #CleanedLeads
SET Reason = 'Non-numeric phone number' WHERE ISNUMERIC(PhoneNumber) <> 1
UPDATE #CleanedLeads
SET Reason = 'Phone is greater than 10 digits' WHERE Reason = '' AND LEN(PhoneNumber) > 10
UPDATE #CleanedLeads
SET Reason = 'Phone is fewer than 10 digits' WHERE Reason = '' AND LEN(PhoneNumber) < 10
--Remove numbers in the DoNotCall table
DELETE FROM #CleanedLeads WHERE EXISTS (SELECT * FROM DoNotCall WHERE ISNUMERIC(DNCTelephone) = PhoneNumber)
AND Reason = ''
SELECT * FROM #CleanedLeads
END
在您的声明中:
DELETE FROM
#CleanedLeads
WHERE
EXISTS (
SELECT
*
FROM
DoNotCall
WHERE
ISNUMERIC(DNCTelephone) = PhoneNumber
AND Reason = ''
)
在这里,您正在比较 ISNUMERIC(DNCTelephone)
和 VARCHAR(20)
字段 PhoneNumber
。
我正在根据正确的 phone 号码格式检查用户上传的 phone 号码列表和“请勿致电”phone 的数据库 table数字并返回格式不正确或 table 中不存在的列表条目。 phone 数字都定义为 varchar,但 .Fill 会自动将其转换为 int 并引发溢出错误。有谁知道如何获取 .Fill 以将 phone 数字保留为 varchar?我试过预先声明填充的 table 列并在存储过程中将 phone 数字输出定义为 bigint,但这些都没有用。应用程序中的所有其他代码都可以正常工作。提前致谢!
错误消息,其中 'x' 表示一个 phone 数字: System.Data.SqlClient.SqlException (0x80131904): varchar 值 'xxxxxxxxxx' 的转换溢出了一个 int 列。声明已终止。
C#:
System.Data.DataTable cleanedList = new System.Data.DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("dbo.uspCleanScrubberLeads", conn))
using (var da = new SqlDataAdapter(cmd))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ProcedureDate", DateTime.Now);
cmd.Parameters.AddWithValue("@LeadListTable", dt);
da.Fill(cleanedList);//Error occurs here!!!
}
SQL:
ALTER PROCEDURE [dbo].[uspCleanScrubberLeads]
@ProcedureDate datetime2(0),
@LeadListTable AS LeadListTable READONLY
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #CleanedLeads (PhoneNumber varchar(20), Reason varchar(200))
INSERT INTO #CleanedLeads (PhoneNumber, Reason)
SELECT PhoneNumber, '' FROM @LeadListTable AS t1
UPDATE #CleanedLeads
SET Reason = 'Non-numeric phone number' WHERE ISNUMERIC(PhoneNumber) <> 1
UPDATE #CleanedLeads
SET Reason = 'Phone is greater than 10 digits' WHERE Reason = '' AND LEN(PhoneNumber) > 10
UPDATE #CleanedLeads
SET Reason = 'Phone is fewer than 10 digits' WHERE Reason = '' AND LEN(PhoneNumber) < 10
--Remove numbers in the DoNotCall table
DELETE FROM #CleanedLeads WHERE EXISTS (SELECT * FROM DoNotCall WHERE ISNUMERIC(DNCTelephone) = PhoneNumber)
AND Reason = ''
SELECT * FROM #CleanedLeads
END
在您的声明中:
DELETE FROM
#CleanedLeads
WHERE
EXISTS (
SELECT
*
FROM
DoNotCall
WHERE
ISNUMERIC(DNCTelephone) = PhoneNumber
AND Reason = ''
)
在这里,您正在比较 ISNUMERIC(DNCTelephone)
和 VARCHAR(20)
字段 PhoneNumber
。