即使我在SQL中的列数据类型已经是varbinary
,在上传文件时也会出现此错误。我不知道为什么我的参数@FileUpload
是nvarchar
。通过使用datatable函数,它可以轻松地整体存储并发送到数据库,为此我也使用了UserDefinedTableType
,我的数据类型已经是varbinary
。
public ActionResult UpdateClearanceItems(string TranNo, string CompanyID, string AtmCard, string FleetCard, string HmoCard,
string Uniform, string RSA, string EducationBenefitsRSA, string AmaBankLoan, string UsedSL,
string ProratedSL, string KingsTreat, string LastPayroll, string OthersHr, string remarksCompanyID,
string remarksAtmCard, string remarksFleetCard, string remarksHmoCard, string remarksUniform,
string remarksRSA, string remarksEducationBenefitsRSA , string remarksAmaBankLoan, string remarksUsedSL,
string remarksProratedSL, string remarksKingsTreat, string remarksLastPayroll, string remarksOthersHr,
HttpPostedFileBase[] FileUpload)
{
dynamic user = Session["UserProfile"];
string UserID = user[0].UserID.ToString();
DataTable dtItems = new DataTable();
dtItems.Columns.Add("FileName", typeof(string));
dtItems.Columns.Add("ContentType", typeof(string));
dtItems.Columns.Add("Data", typeof(string));
foreach (var file in FileUpload)
{
byte[] bytes;
using (BinaryReader br = new BinaryReader(file.InputStream))
{
bytes = br.ReadBytes(file.ContentLength);
}
dtItems.Rows.Add(file.FileName, file.ContentType, bytes);
}
using (SqlConnection con = new SqlConnection(GlobalFunction.GetConnection("PayrollConnect")))
{
con.Open();
try
{
SqlCommand cmd = new SqlCommand("spm_EmpClearancePending", con);
cmd.CommandTimeout = 1000000;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@TranNo", TranNo.ToString());
cmd.Parameters.AddWithValue("@UserID", UserID.ToString());
cmd.Parameters.AddWithValue("@CompanyID", CompanyID.ToString());
cmd.Parameters.AddWithValue("@AtmCard", AtmCard.ToString());
cmd.Parameters.AddWithValue("@FleetCard", FleetCard.ToString());
cmd.Parameters.AddWithValue("@HmoCard", HmoCard.ToString());
cmd.Parameters.AddWithValue("@Uniform", Uniform.ToString());
cmd.Parameters.AddWithValue("@RSA", RSA.ToString());
cmd.Parameters.AddWithValue("@EducationBenefitsRSA", EducationBenefitsRSA.ToString());
cmd.Parameters.AddWithValue("@AmaBankLoan", AmaBankLoan.ToString());
cmd.Parameters.AddWithValue("@UsedSL", UsedSL.ToString());
cmd.Parameters.AddWithValue("@ProratedSL", ProratedSL.ToString());
cmd.Parameters.AddWithValue("@KingsTreat", KingsTreat.ToString());
cmd.Parameters.AddWithValue("@LastPayroll", LastPayroll.ToString());
cmd.Parameters.AddWithValue("@OthersHr", OthersHr.ToString());
cmd.Parameters.AddWithValue("@remarksCompanyID", remarksCompanyID.ToString());
cmd.Parameters.AddWithValue("@remarksAtmCard", remarksAtmCard.ToString());
cmd.Parameters.AddWithValue("@remarksFleetCard", remarksFleetCard.ToString());
cmd.Parameters.AddWithValue("@remarksHmoCard", remarksHmoCard.ToString());
cmd.Parameters.AddWithValue("@remarksUniform", remarksUniform.ToString());
cmd.Parameters.AddWithValue("@remarksRSA", remarksRSA.ToString());
cmd.Parameters.AddWithValue("@remarksEducationBenefitsRSA", remarksEducationBenefitsRSA.ToString());
cmd.Parameters.AddWithValue("@remarksAmaBankLoan", remarksAmaBankLoan.ToString());
cmd.Parameters.AddWithValue("@remarksUsedSL", remarksUsedSL.ToString());
cmd.Parameters.AddWithValue("@remarksProratedSL", remarksProratedSL.ToString());
cmd.Parameters.AddWithValue("@remarksKingsTreat", remarksKingsTreat.ToString());
cmd.Parameters.AddWithValue("@remarksLastPayroll", remarksLastPayroll.ToString());
cmd.Parameters.AddWithValue("@remarksOthersHr", remarksOthersHr.ToString());
cmd.Parameters.AddWithValue("@FileUpload" ,dtItems);
cmd.ExecuteNonQuery();
}
catch(Exception err)
{ }
}
return RedirectToAction("ClearanceDashboard", "Clearance", new { Message = "Done" });
}
答案 0 :(得分:2)
数据表中"Data"
列的类型不应为string
,而应为byte[]
-更改此内容:
dtItems.Columns.Add("Data", typeof(string));
对此:
dtItems.Columns.Add("Data", typeof(byte[]));