使用存储过程将新记录插入MySQL数据库时发生以下异常。
在调用Read()
之前无效尝试访问字段
我的代码在这里:
protected string InsertDoctype(string ioption, int icategory_id, string icategory_name, string iUSERID,string iGUID)
{
string iresult =string.Empty;
try
{
conString.Open();
MySqlCommand cmd = new MySqlCommand("sp_b1_Doctype", conString);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("?ioption", MySqlDbType.VarChar).Value = ioption;
cmd.Parameters.Add("?icategory_id", MySqlDbType.Int32).Value = icategory_id;
cmd.Parameters.Add("?icategory_name", MySqlDbType.VarChar).Value = icategory_name;
cmd.Parameters.Add("?iUSERID", MySqlDbType.VarChar).Value = iUSERID;
cmd.Parameters.Add("?iGUID", MySqlDbType.VarChar).Value = iGUID;
cmd.Parameters.Add(new MySqlParameter("?oresult", MySqlDbType.VarChar));
cmd.Parameters["?oresult"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
iresult = cmd.Parameters["?oresult"].Value.ToString();
}
catch (Exception Ex)
{
}
finally
{
if (conString.State == ConnectionState.Open)
conString.Close();
}
return iresult;
}
请帮帮我。
存储过程在这里:
CREATE DEFINER=`root`@`%` PROCEDURE `sp_b1_Doctype`(in ioption varchar(10),in icategory_id integer,in icategory_name varchar(45),in iUSERID varchar(8),in iGUID varchar(45), out oresult varchar (100))
BEGIN
-- Declare variables to hold diagnostics area information
DECLARE code CHAR(5) DEFAULT '00000';
DECLARE msg TEXT;
DECLARE rows INT;
DECLARE result TEXT;
-- Declare exception handler for failed insert
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
code = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
END;
-- Perform the insert
IF ioption="insert" THEN
set autocommit = 0;
start transaction;
insert into b1_category(category_id, category_name, USERID, INS_DATE, GUID) values (icategory_id, icategory_name, iUSERID, now(), iGUID);
SET result ="Insert succeeded";
END IF;
IF code = '00000' THEN
GET DIAGNOSTICS rows = ROW_COUNT;
ELSE
-- SET result = CONCAT('insert failed, error = ',code,', message = ',msg,'$e');
SET result = "Something went wrong. Please try again later ..$e";
END IF;
SET oresult = result;
commit;
END