一个吐血的问题:上传图片参数无效
c#中以二进制形式上传图片到数据库,在读取图片显示在PictureBox中时提示“参数无效”;找了很久终于发现问题所在:
贴出代码:修改后代码
OpenFileDialog oFd = new OpenFileDialog();
if (oFd.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image = Image.FromFile(oFd.FileName);
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
FileStream fs = new FileStream(oFd.FileName, FileMode.Open, FileAccess.Read);
byte[] buffByte = new byte[fs.Length];
fs.Read(buffByte, 0, (int)fs.Length);
fs.Close();
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into EIIS_EVIDENCE_IMGS(");
strSql.Append("EVIDENCE_NO,EVIDENCE_IMG,ORGANIZATION_ID,DELETE_FLAG,CREATE_USER,CREATE_DATETIME,CURRENT_IMG,TRANSFER_ID)");
strSql.Append(" values (");
strSql.Append("@EVIDENCE_NO,@EVIDENCE_IMG,@ORGANIZATION_ID,@DELETE_FLAG,@CREATE_USER,@CREATE_DATETIME,@CURRENT_IMG,@TRANSFER_ID)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@EVIDENCE_NO", SqlDbType.VarChar,50),
new SqlParameter("@EVIDENCE_IMG", SqlDbType.Image),
new SqlParameter("@ORGANIZATION_ID", SqlDbType.Int,4),
new SqlParameter("@DELETE_FLAG", SqlDbType.Char,1),
new SqlParameter("@CREATE_USER", SqlDbType.VarChar,30),
new SqlParameter("@CREATE_DATETIME", SqlDbType.DateTime),
new SqlParameter("@CURRENT_IMG", SqlDbType.Char,1),
new SqlParameter("@TRANSFER_ID",SqlDbType.Int)
};
parameters[0].Value = evi.EVIDENCE_NO;
parameters[1].Value = buffByte;
parameters[2].Value = GlobalClass.user.ORGANIZATION_ID;
parameters[3].Value = Convert.ToInt32(DeleteEnum.UnDelete).ToString();
parameters[4].Value = GlobalClass.user.USER_NO;
parameters[5].Value = DateTime.Now;
parameters[6].Value = Convert.ToInt32(YesOrNo.Yes).ToString();
parameters[7].Value = eviTransfer.TRANSFER_ID;
DataBase.SetBusinessConnection(GlobalClass.busiConnStr);
object obj = DataBase.GetScalar(strSql.ToString(), parameters);
}
修改后代码:
OpenFileDialog oFd = new OpenFileDialog();
if (oFd.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image = Image.FromFile(oFd.FileName);
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
FileStream fs = new FileStream(oFd.FileName, FileMode.Open, FileAccess.Read);
byte[] buffByte = new byte[fs.Length];
fs.Read(buffByte, 0, (int)fs.Length);
fs.Close();
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into EIIS_EVIDENCE_IMGS(");
strSql.Append("EVIDENCE_NO,EVIDENCE_IMG,ORGANIZATION_ID,DELETE_FLAG,CREATE_USER,CREATE_DATETIME,CURRENT_IMG,TRANSFER_ID)");
strSql.Append(" values (");
strSql.Append("@EVIDENCE_NO,@EVIDENCE_IMG,@ORGANIZATION_ID,@DELETE_FLAG,@CREATE_USER,@CREATE_DATETIME,@CURRENT_IMG,@TRANSFER_ID)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@EVIDENCE_NO", SqlDbType.VarChar,50),
new SqlParameter("@EVIDENCE_IMG", SqlDbType.Image,16),
new SqlParameter("@ORGANIZATION_ID", SqlDbType.Int,4),
new SqlParameter("@DELETE_FLAG", SqlDbType.Char,1),
new SqlParameter("@CREATE_USER", SqlDbType.VarChar,30),
new SqlParameter("@CREATE_DATETIME", SqlDbType.DateTime),
new SqlParameter("@CURRENT_IMG", SqlDbType.Char,1),
new SqlParameter("@TRANSFER_ID",SqlDbType.Int)
};
parameters[0].Value = evi.EVIDENCE_NO;
parameters[1].Value = buffByte;
parameters[2].Value = GlobalClass.user.ORGANIZATION_ID;
parameters[3].Value = Convert.ToInt32(DeleteEnum.UnDelete).ToString();
parameters[4].Value = GlobalClass.user.USER_NO;
parameters[5].Value = DateTime.Now;
parameters[6].Value = Convert.ToInt32(YesOrNo.Yes).ToString();
parameters[7].Value = eviTransfer.TRANSFER_ID;
DataBase.SetBusinessConnection(GlobalClass.busiConnStr);
object obj = DataBase.GetScalar(strSql.ToString(), parameters);
}
就是在定义参数SqlParameter[] parameters 是把new SqlParameter("@EVIDENCE_IMG", SqlDbType.Image),写成了new SqlParameter("@EVIDENCE_IMG", SqlDbType.Image,16),
晕到要死。真是低级。