我有一个简单的应用程序,可以保存用户详细信息并生成一个ID,并根据该ID将他们的教育详细信息保存到表中。
这是存储过程:
CREATE PROCEDURE SAVE_USER_DETAILS
@tablePersonalDetails Resume_Personal_Details READONLY,
@tableEducation Resume_Education READONLY,
@returnValue BIGINT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @HeaderID BIGINT;
BEGIN TRANSACTION Tran_Resume
INSERT INTO YCTC_CV_USER_PERSONAL_DETAIL (CV_USER_NAME, CV_USER_FATHER_NAME, CV_USER_DOB,
CV_USER_MARITAL_STATUS,
CV_USER_PERMANENT_ADDRESS, CV_USER_PRESENT_ADDRESS,
CV_USER_CONTACT, CV_USER_EMAIL, CV_USER_GENDER)
SELECT
user_name, father_name, user_dob,
marital_status,
permanent_address, present_address,
user_contact, user_email, user_gender
FROM
@tablePersonalDetails
SET @HeaderID = (SELECT SCOPE_IDENTITY());
INSERT INTO YCTC_CV_USER_EDUCATION (CV_USER_ID, QUALIFICATION, INSTITUTION, BOARD_UNIVERSITY, PYEAR,RESULT)
SELECT
@HeaderID, qualification, institution, board_university, pyear, result
FROM
@tableEducation;
COMMIT TRANSACTION Tran_Resume
SET @returnValue = @HeaderID
SELECT @returnValue;
RETURN(0) ;
END
下面是用户定义的表类型:
CREATE TYPE Resume_Personal_Details AS TABLE
(
[user_name] [VARCHAR](50) NOT NULL,
[father_name] [VARCHAR](50) NOT NULL,
[user_dob] [DATE] NOT NULL,
[marital_status] [TINYINT] NULL,
[permanent_address] [VARCHAR](100) NOT NULL,
[present_address] [VARCHAR](100) NOT NULL,
[user_contact] [VARCHAR](20) NOT NULL,
[user_email] [VARCHAR](50) NOT NULL,
[user_gender] [TINYINT] NULL,
[Job_obj] [VARCHAR](MAX) NOT NULL
)
CREATE TYPE Resume_Education AS TABLE
(
[qualification] [VARCHAR](100) NOT NULL,
[institution] [VARCHAR](100) NOT NULL,
[board_university] [VARCHAR](100) NOT NULL,
[pyear] [VARCHAR](4) NOT NULL,
[result] [VARCHAR](10) NOT NULL
)
下面是C#代码(将所有行发送到用户定义的表并将ID发送到C#代码)
ReturnMessage _returnMessage = new ReturnMessage();
// Above class has 2 variables : bool ReturnStatus; string Message ;
try
{
string DateOfBirth = data.UserDOB.ToString().Replace("/", "-");
//.Substring(6, 4) + "-" + data.UserDOB.ToString().Substring(3, 2) + "-" + data.UserDOB.ToString().Substring(0, 2);
DataTable dt = new DataTable();
// Add columns
dt.Columns.Add(new DataColumn("user_name", typeof(string)));
dt.Columns.Add(new DataColumn("father_name", typeof(string)));
dt.Columns.Add(new DataColumn("user_dob", typeof(DateTime)));
dt.Columns.Add(new DataColumn("marital_status", typeof(int)));
dt.Columns.Add(new DataColumn("permanent_address", typeof(string)));
dt.Columns.Add(new DataColumn("present_address", typeof(string)));
dt.Columns.Add(new DataColumn("user_contact", typeof(string)));
dt.Columns.Add(new DataColumn("user_email", typeof(string)));
dt.Columns.Add(new DataColumn("user_gender", typeof(int)));
dt.Columns.Add(new DataColumn("Job_obj", typeof(string)));
// Add rows
dt.Rows.Add(data.UserName, data.FatherName, DateOfBirth, data.MaritalStatus,
data.UserPermanentAddress, data.UserPresentAddress,
data.Contact, data.UserEmail, data.Gender, data.User_Job_Objective);
DataTable EducationDataTable = new DataTable();
EducationDataTable.Columns.Add(new DataColumn("qualification", typeof(string)));
EducationDataTable.Columns.Add(new DataColumn("institution", typeof(string)));
EducationDataTable.Columns.Add(new DataColumn("board_university", typeof(string)));
EducationDataTable.Columns.Add(new DataColumn("pyear", typeof(string)));
EducationDataTable.Columns.Add(new DataColumn("result", typeof(string)));
foreach(EducationDetails _educationalDetails in data.EducationDetails)
{
EducationDataTable.Rows.Add(_educationalDetails.Qualification,
_educationalDetails.Institution,
_educationalDetails.University,
_educationalDetails.Year,
_educationalDetails.Result);
}
long ReturnValue = 0;
DBHelper.Parameters[] colParameters = new DBHelper.Parameters[3] {
new DBHelper.Parameters("@tablePersonalDetails", dt),
new DBHelper.Parameters("@tableEducation", EducationDataTable),
new DBHelper.Parameters("@returnValue", "", ParameterDirection.Output),
};
int RetCount = objHelper.gExecuteNonQuery(CommandType.StoredProcedure, "SAVE_USER_DETAILS", colParameters);
intHeaderID = Convert.ToInt64(objHelper.oCommand.Parameters["@returnValue"].Value);
_returnMessage.Message = Convert.ToString(intHeaderID);
}
catch (Exception ex)
{
_returnMessage.Message = "Fail";
}
调用ExecuteNonQuery()
时,它生成了一个异常,但所有数据都已成功插入。
将数据类型bigint转换为nvarchar时出错
从我所有的测试中,我确定问题出在存储过程中,但这是我没有得到的。
如果需要其他任何信息,请通知我。
答案 0 :(得分:0)
如果成功插入数据,则从proc返回参数时会发生错误。 检查输出参数的大小。快速演示
create proc test (@retval bigint out)
as
set @retval = 123;
go
declare @res nvarchar(2);
exec test @res out;
消息8114,级别16,状态2,过程测试,第4行错误转换 数据类型bigint转换为nvarchar。
答案 1 :(得分:0)
错误在于我发送给proc的Parameter中, 我之前在做
DBHelper.Parameters[] colParameters = new DBHelper.Parameters[1] {
new DBHelper.Parameters("@returnValue","",ParameterDirection.Output),
};
objHelper.gExecuteNonQuery(CommandType.StoredProcedure, "SAVE_USER_DETAILS", colParameters);
Parameters方法采用string Name, object Value, ParameterDirection Direction
,因此当gExecuteNonQuery()命中时,预期值是int(即0),而是我发送的是字符串(“”),这会创建一个异常
'Error converting data type bigint to nvarchar '
所以当我做下面的代码
DBHelper.Parameters[] colParameters = new DBHelper.Parameters[1] {
new DBHelper.Parameters("@returnValue",0,ParameterDirection.Output),
};
objHelper.gExecuteNonQuery(CommandType.StoredProcedure, "SAVE_USER_DETAILS", colParameters);
解决了错误