读取的时候出现参数无效 求大神帮忙啊 网上的代码全试了 都不行
byte[] imagebytes = null;
int bh = 0;
string sc = this.listView1.SelectedItems[0].Text;
SqlConnection sq = new SqlConnection();
sq.ConnectionString = "server=.;database=PhoneBook;uid=sa;pwd=2300700;";
sq.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = sq;
cmd.CommandText = "select * from 联系人 where 用户名='" + label2.Text.Trim() + "' and 姓名='" + sc.Trim() + "'";
SqlDataReader sdr = cmd.ExecuteReader();
string[] zb = new string[20];
Form3 t = new Form3(label2.Text, bh);
while (sdr.Read())
{
t.textBox3.Text = sdr["工作单位"].ToString();
t.textBox2.Text = sdr["联系电话"].ToString();
t.textBox5.Text = sdr["电子邮箱"].ToString();
t.textBox4.Text = sdr["家庭住址"].ToString();
bh = Convert.ToInt32(sdr["编号"]);
imagebytes = (byte[])sdr["照片"];
}
//MessageBox.Show(imagebytes.Length.ToString());
t.textBox1.Text = sc;
MemoryStream ms = new MemoryStream(imagebytes);
Image image = Image.FromStream(ms, true);
t.pictureBox1.Image= image;
剩下的代码就不贴了 提示 Image image = Image.FromStream(ms, true); 参数无效
确认一下imagebytes是否有内容,原数据库的图片是否正确保存。
public void tupian()
{
openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fullpath = openFileDialog1.FileName;//文件路径
FileStream fs = new FileStream(fullpath, FileMode.Open);
imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
MessageBox.Show(imagebytes.Length.ToString());
pictureBox1.ImageLocation = fullpath;
br.Close();
fs.Close();
}
}
这是打开图片的操作
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sq = new SqlConnection("server=.;database=PhoneBook;uid=sa;pwd=2300700;");
sq.Open();
SqlCommand cmd = new SqlCommand("insert into 联系人(用户名,姓名,联系电话,工作单位,家庭住址,电子邮箱,组别,照片) values('" + a + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + comboBox1.Text + "','" + imagebytes + "')", sq);
cmd.Parameters.Add("@fullpath", SqlDbType.Image);
cmd.Parameters["@fullpath"].Value = imagebytes;
if (cmd.ExecuteNonQuery() > 0)
MessageBox.Show("添加成功");
else
MessageBox.Show("添加失败");
sq.Close();
}
这是保存到数据库的操作
哪里有问题啊?