我正在创建一个帮助台系统,但似乎遇到了一个小问题,当用户单击一个按钮以“预注册”帮助台票号时,我的脚本将触发到 SQL Express 服务器,创建一个新行 -> 获取行 ID -> 使用行 ID 创建最终票证参考(以 RAD000001 的格式,例如第 1 行)这个过程似乎对我很好,直到我到达第 9 号......当用户点击“创建新票证”按钮(生成票号 10)我收到以下错误:
Arithmetic overflow error converting expression to data type nvarchar.
它创建行,但似乎在更新时失败(创建行,返回 ID,然后从 ID 创建 ref)
我的 SQL 表如下所示:
CREATE TABLE [dbo].[tickets](
[id] [int] IDENTITY(1,1) NOT NULL,
[ticket_ref] [varchar](15) NULL,
[details_id] [int] NULL,
[state] [int] NULL,
[create_date_Time] [datetime] NOT NULL,
[details_subject] [varchar](255) NULL,
[details_main_body] [text] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
这是后面的c#代码:
private string Reform_TicketId(string original_ticket_id)
{
switch (original_ticket_id.Length)
{
case 1:
return "RAD00000" + original_ticket_id.ToString();
case 2:
return "RAD0000" + original_ticket_id.ToString();
case 3:
return "RAD000" + original_ticket_id.ToString();
case 4:
return "RAD00" + original_ticket_id.ToString();
case 5:
return "RAD0" + original_ticket_id.ToString();
case 6:
return "RAD" + original_ticket_id.ToString();
default:
return "ERROR!";
}
}
protected void DsCreateTicketEntry_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
string original_id = e.Command.Parameters["@ticket_id"].Value.ToString();
string reformed_id = Reform_TicketId(original_id.ToString()).ToString();
if (original_id != "0")
{
//It has returned a value that is a DB ID field and seems correct! lets proceed!
DsCreateTicketEntry.UpdateParameters["ticket_db_id"].DefaultValue = original_id;
//DsCreateTicketEntry.UpdateParameters["new_ticket_id"].DefaultValue = reformed_id;
DsCreateTicketEntry.UpdateParameters["new_ticket_id"].DefaultValue = "RAD000002";
DsCreateTicketEntry.Update();
LblTicketOutput.Text = reformed_id;
}
else
{
//@TODO: Write to the label stating we had problems generating a Ticket ID!
}
}
protected void btnCreateTicket_Click(object sender, EventArgs e)
{
DsCreateTicketEntry.Insert();
btnCreateTicket.Enabled = false;
}
在我的 SQLDataSource 中使用以下查询:
InsertCommand="INSERT INTO tickets(state, create_date_Time) VALUES (1,GETDATE()) SET @ticket_id=SCOPE_IDENTITY();"
UpdateCommand="UPDATE tickets SET ticket_ref = @new_ticket_id WHERE (id = @ticket_db_id)"
希望有人能够阐明我哪里出错了!?我整天都在看这段代码,它稍微超出了我的学习范围......