C#中插入null到SQL中的image类型
我将图片直接用二至制的方式,以image类型存在数据库中,在C#中,用insert语句插入时,如果有图片则正常执行,但没用图片时却报这个错,请问下,没有图片是不是null...
我将图片直接用二至制的方式,以image类型存在数据库中,在C#中,用insert语句插入时,如果有图片则正常执行,但没用图片时却报这个错,请问下,没有图片是不是null,为什么转了DBnull.value也不行?如何解决?望高手明示
展开
2个回答
展开全部
对,的确是null。保存空的byte就可以了。
public byte[] PhotoChangeTwo(string photoname)
{
byte[] byData = null;
////根据图片文件的路径使用文件流打开,并保存为byte[]
string imagepath = Path.Combine(Application.StartupPath, "Image\\" + photoname + ".jpg");
//FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法
if (File.Exists(imagepath))
{
FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法
byData = new byte[fs.Length];
fs.Read(byData, 0, byData.Length);
fs.Close();
// bytevalues = dbhelp.ByteChange(byData);
}
return byData;
}
这个返回值就是你保存到照片值。没有为空的byte[],有照片则为二进制。
public byte[] PhotoChangeTwo(string photoname)
{
byte[] byData = null;
////根据图片文件的路径使用文件流打开,并保存为byte[]
string imagepath = Path.Combine(Application.StartupPath, "Image\\" + photoname + ".jpg");
//FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法
if (File.Exists(imagepath))
{
FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法
byData = new byte[fs.Length];
fs.Read(byData, 0, byData.Length);
fs.Close();
// bytevalues = dbhelp.ByteChange(byData);
}
return byData;
}
这个返回值就是你保存到照片值。没有为空的byte[],有照片则为二进制。
展开全部
首先是关于dataGridView的绑定。代码见下
private void button_show_Click(object sender, EventArgs e)
{
string sqlText = "server=localhost;initial catalog=Test; integrated security=true";
string sqlstr="select * from tast";
SqlConnection conn=new SqlConnection (sqlText);
conn.Open();
SqlCommand comm = new SqlCommand(sqlstr, conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = comm;
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
下面是插入图像到数据库,代码如下:
private void button_connect_Click(object sender, EventArgs e)
{
string sqlText = "server=localhost;initial catalog=Test; integrated security=true";
string sqlstr = "insert into tast(photo) values(@image) ";
SqlConnection conn = new SqlConnection(sqlText);
conn.Open();
SqlCommand comm = new SqlCommand(sqlstr, conn);
comm.Parameters.Add("@image", SqlDbType.Image).Value = GetPhoto("DSC_6126.JPG");
comm.ExecuteNonQuery();
conn.Close();
}
public byte[] GetPhoto(string str1)
{
string str = str1;
FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read);
byte[]photo=new byte[file.Length];
file.Read(photo,0,photo.Length);
file.Close();
return photo;
}
下面就是读取图片了
private void button_showimg_Click(object sender, EventArgs e)
{
string sqlText = "server=localhost;initial catalog=Test; integrated security=true";
string sqlstr = "select *from tast where whf=1";
SqlConnection conn = new SqlConnection(sqlText);
conn.Open();
SqlCommand comm = new SqlCommand(sqlstr, conn);
SqlDataAdapter da=new SqlDataAdapter ();
da.SelectCommand=comm;
DataSet ds=new DataSet ();
da.Fill(ds);
MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0][1]);
Image i = Image.FromStream(ms,true);
pictureBox1.Image = i;
}
private void button_show_Click(object sender, EventArgs e)
{
string sqlText = "server=localhost;initial catalog=Test; integrated security=true";
string sqlstr="select * from tast";
SqlConnection conn=new SqlConnection (sqlText);
conn.Open();
SqlCommand comm = new SqlCommand(sqlstr, conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = comm;
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
下面是插入图像到数据库,代码如下:
private void button_connect_Click(object sender, EventArgs e)
{
string sqlText = "server=localhost;initial catalog=Test; integrated security=true";
string sqlstr = "insert into tast(photo) values(@image) ";
SqlConnection conn = new SqlConnection(sqlText);
conn.Open();
SqlCommand comm = new SqlCommand(sqlstr, conn);
comm.Parameters.Add("@image", SqlDbType.Image).Value = GetPhoto("DSC_6126.JPG");
comm.ExecuteNonQuery();
conn.Close();
}
public byte[] GetPhoto(string str1)
{
string str = str1;
FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read);
byte[]photo=new byte[file.Length];
file.Read(photo,0,photo.Length);
file.Close();
return photo;
}
下面就是读取图片了
private void button_showimg_Click(object sender, EventArgs e)
{
string sqlText = "server=localhost;initial catalog=Test; integrated security=true";
string sqlstr = "select *from tast where whf=1";
SqlConnection conn = new SqlConnection(sqlText);
conn.Open();
SqlCommand comm = new SqlCommand(sqlstr, conn);
SqlDataAdapter da=new SqlDataAdapter ();
da.SelectCommand=comm;
DataSet ds=new DataSet ();
da.Fill(ds);
MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0][1]);
Image i = Image.FromStream(ms,true);
pictureBox1.Image = i;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询