我想使用Ado.net将图像保存在数据库中。我不知道将图像转换为字节并保存在数据库中。友善的告诉我,将图像转换为字节并保存在数据库中的最简单方法是什么。 并显示在最前面的语法。我正在使用MVC 5版本,这是我的C#代码。 我正在等待专家的答复,这是一种将图像保存到数据库中的简便方法。
注意:数据库varchar(100)中的图像类型
C#
public int QuotationInsert(int Qt_ID, string EnteryDate, string Purpose, Quotation[] Quot, string AddNew)
{
try
{
con.Open();
tr = con.BeginTransaction();
if (AddNew == "New")
{
cmd = new SqlCommand("Select Right('00' + Cast(ISNULL(MAX(Qt_ID),0)+1 as varchar(2)) + '', 2) from QuotationMain", con);
cmd.Transaction = tr;
Qt_ID = Convert.ToInt32(cmd.ExecuteScalar().ToString());
cmd = new SqlCommand("Sp_QuotationMainInsert", con);
}
else
cmd = new SqlCommand("Sp_QuotationMainUpdate", con);
cmd.Parameters.AddWithValue("@Qt_ID", Qt_ID);
cmd.Parameters.AddWithValue("@Comp_ID", 1);
if (EnteryDate != null)
cmd.Parameters.AddWithValue("@EnteryDate", EnteryDate);
else
cmd.Parameters.AddWithValue("@EnteryDate", string.Empty);
cmd.Parameters.AddWithValue("@Username", HttpContext.Current.Session["AgentName"]);
cmd.Parameters.AddWithValue("@Purpose", Purpose);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
if(Quot !=null)
{
for (int i = 0; i < Quot.Length; i++)
{
try
{
if (AddNew == "New")
{
cmd = new SqlCommand("Select ISNULL(MAX(Qt_Dt_ID), 0) + 1 from QuotationDetail", con);
cmd.Transaction = tr;
mQt_Det_ID = Convert.ToInt32(cmd.ExecuteScalar());
cmd = new SqlCommand("Sp_QuotationDetailInsert", con);
cmd.Parameters.AddWithValue("@Qt_Dt_ID", mQt_Det_ID);
}
else if (AddNew == "Edit")
{
cmd = new SqlCommand("Sp_QuotationDetailUpdate", con);
cmd.Parameters.AddWithValue("@Qt_Dt_ID", Quot[i].Qt_Dt_ID);
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Qt_Dt_ID", mQt_Det_ID);
cmd.Parameters.AddWithValue("@Qt_ID", Qt_ID);
cmd.Parameters.AddWithValue("@SrNo", Quot[i].Srno);
cmd.Parameters.AddWithValue("@PartyName", Quot[i].PartyName);
cmd.Parameters.AddWithValue("@Image", Quot[i].Image);
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
}
}
tr.Commit();
return i;
}
catch (SqlException sqlex)
{
tr.Rollback();
throw sqlex; // read all sql error
}
catch (Exception ex)
{
tr.Rollback();
throw ex; // General execption
}
finally
{
con.Close();
}