参数无效错误 [英] Parameter is not valid error

查看:71
本文介绍了参数无效错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//将图像保存到sql server 2008的代码r2 //////

byte [] img = null;

FileStream fs = new FileStream(imgLoc,FileMode.Open,FileAccess.Read);

BinaryReader br = new BinaryReader(fs);

img = br.ReadBytes((int)fs.Length);





string conn =data source = .;初始目录= RMSDB ; user = sa; password = ibs;;

SqlConnection con = new SqlConnection(conn);

con.Open();

string cmdd = String.Format(INSERT INTO Employees(Emp_Pic_ImageData)VALUES('{0}'),@ img);



SqlCommand cmd = new SqlCommand( cmdd,con);



cmd.Parameters.Add(new SqlParameter(@ img,img));

int tempss = cmd.ExecuteNonQuery();





//从sql server审查图像的代码//////////// < br $>




string sql = String.Format(从雇员那里选择Emp_Pic_ImageData,其中Emp_Id ='{0}',TxtBoxId.Text);

SqlCommand cmd = new SqlCommand(sql,con);

SqlDataReader reader = cmd.ExecuteReader();

reader.Read();

if(reader.HasRows)

{

byte [] img =(byte [])(reader [0]);

if(img == null)

{

PicboxEmployee.Image = null;

}

else

{

MemoryStream mstrm = new MemoryStream(img);

PicboxEmployee.Image = new System.Drawing.Bitmap(mstrm ); //参数错误无效。



}

}

else

{



MessageBox.Show(这不存在);



}

//code for saving image into sql server 2008 r2//////
byte[] img = null;
FileStream fs = new FileStream(imgLoc,FileMode.Open,FileAccess.Read);
BinaryReader br=new BinaryReader(fs);
img = br.ReadBytes((int)fs.Length);


string conn = "data source=.;Initial catalog=RMSDB;user=sa;password=ibs;";
SqlConnection con = new SqlConnection(conn);
con.Open();
string cmdd = String.Format("INSERT INTO Employees (Emp_Pic_ImageData) VALUES('{0}')", @img);

SqlCommand cmd = new SqlCommand(cmdd, con);

cmd.Parameters.Add(new SqlParameter("@img",img));
int tempss = cmd.ExecuteNonQuery();


//code for retrival of image from sql server////////////


string sql = String.Format("Select Emp_Pic_ImageData From Employees where Emp_Id='{0}'", TxtBoxId.Text);
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
byte[] img = (byte[])(reader[0]);
if (img == null)
{
PicboxEmployee.Image = null;
}
else
{
MemoryStream mstrm = new MemoryStream(img);
PicboxEmployee.Image = new System.Drawing.Bitmap(mstrm); //there is error of parameter is not valid.

}
}
else
{

MessageBox.Show("this not exists");

}

推荐答案

不,那不行。

它似乎可以,但它没有做你有什么想法。

见这里:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 解释问题和解决方案。
No, that won't work.
It may seem to, but it doesn't do what you think at all.
See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - explains the problem and solution.


这篇关于参数无效错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆