我数据库里有个表的一列tupian是存image的。现在在datagridview里显示了除图片外的其他列。怎么在我选定了某行时。我把一张图片插进这一行的图片列。我用下面代码插入图片: Bitmap p2 = new Bitmap(pictureBox1.Image, 112, 114);
             MemoryStream ms = new MemoryStream();
            p2.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] data=ms.ToArray();
            ms.Close();
            string str1 = "insert into show(tupian) values(@tupian)";
            SqlConnection conn = new SqlConnection(sql);
            SqlCommand da = new SqlCommand(str1,conn);
            da.Parameters.Add(new SqlParameter("@tupian",SqlDbType.Binary));
            da.Parameters["@tupian"].Value = data;
            conn.Open();
            da.ExecuteNonQuery();
            conn.Close();可是总是在新建的一行插入的,不是在我选中的datagridview里的哪一行。请问该怎么做才可以把图片插入到我选中的那一行里。。

解决方案 »

  1.   

    sql语句有问题不是insert用update
      

  2.   

    update show set tupian=@tupian where 主键=主键值
      

  3.   

    insert是在新行中插入,update是更新指定行
      

  4.   

    你用的是insert,当然是新建一行了,你要用update!
    update show set tupian=@tupian where ....
    where条件看你的表结构了,可以取到当前行的某个信息去查询
    dataGridView1.CurrentRow.Cells["列名"].Value 
      

  5.   


    string str1 = "update show set tupian= @tupian where Id=@Id";
    //更新原来的行用update
      

  6.   

    主键值 = dataGridView1.CurrentRow.Cells["主键"].Value 
      

  7.   

    要用update
    sql语句更新insert是插入
      

  8.   

    这是我的代码。麻烦谁看下,运行没提示错误,可是表里面的tupian还是null。咋回事呢namespace tongxun
    {
        public partial class main : Form
        {
            public main()
            {
                InitializeComponent();
            }       static String Database = System.IO.Directory.GetCurrentDirectory() + "\\Stu21.mdf";
           string sql = "Data Source=.\\SQLExpress;Integrated Security=SSPI;AttachDBFilename=" + Database + ";User Instance=true";
            string s1;
            private void main_Load(object sender, EventArgs e)
            {
                // TODO: 这行代码将数据加载到表“stu21DataSet3.show”中。您可以根据需要移动或移除它。
                this.showTableAdapter1.Fill(this.stu21DataSet3.show);
                // TODO: 这行代码将数据加载到表“stu21DataSet.show”中。您可以根据需要移动或移除它。
                this.showTableAdapter.Fill(this.stu21DataSet.show);
                // TODO: 这行代码将数据加载到表“stu21DataSet.show”中。您可以根据需要移动或移除它。
              //  this.showTableAdapter.Fill(this.stu21DataSet.show);        }        private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
            {
                int k = dataGridView1.CurrentRow.Index;
                comboBox1.Text = dataGridView1.Rows[k].Cells[0].Value.ToString();
                comboBox2.Text = dataGridView1.Rows[k].Cells[1].Value.ToString();
                textBox1.Text = dataGridView1.Rows[k].Cells[2].Value.ToString();
                textBox2.Text = dataGridView1.Rows[k].Cells[3].Value.ToString();
                textBox3.Text = dataGridView1.Rows[k].Cells[4].Value.ToString();
                textBox4.Text = dataGridView1.Rows[k].Cells[5].Value.ToString();
                textBox5.Text = dataGridView1.Rows[k].Cells[6].Value.ToString();
                textBox6.Text = dataGridView1.Rows[k].Cells[7].Value.ToString();
                textBox7.Text = dataGridView1.Rows[k].Cells[8].Value.ToString();
                textBox8.Text = dataGridView1.Rows[k].Cells[9].Value.ToString();
                textBox9.Text = dataGridView1.Rows[k].Cells[10].Value.ToString();
                textBox10.Text = dataGridView1.Rows[k].Cells[11].Value.ToString();
                s1 = dataGridView1.CurrentRow.Cells[0].Value.ToString(); 
              
            }        private void button1_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    Bitmap bits = null;
                    bits = new Bitmap(openFileDialog1.FileName);
                    pictureBox1.Image = bits;
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                Bitmap p2 = new Bitmap(pictureBox1.Image, 112, 114);
                 MemoryStream ms = new MemoryStream();
                p2.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] data=ms.ToArray();
                ms.Close();
                string str1 = "update show set tupian= @tupian where Sno='s1'";            SqlConnection conn = new SqlConnection(sql);
                SqlCommand da = new SqlCommand(str1,conn);
                da.Parameters.Add(new SqlParameter("@tupian",SqlDbType.Binary));
                da.Parameters["@tupian"].Value = data;
                conn.Open();
                da.ExecuteNonQuery();
                conn.Close();
                if (this.stu21DataSet3.HasChanges(0))
                    this.showTableAdapter1.Update(this.stu21DataSet3);
                       }
        }
    }
      

  9.   

    //Binary最大容量8k,用Image
    new SqlParameter("@tupian",SqlDbType.Image,int.MaxValue);//最好把 byte[] data=ms.ToArray();中data的数组大小弹出来看下
    //用来判断是数据获取的问题还是数据更新的问题?
      

  10.   

    也可以
    主键值 = dataGridView1.SelectedRows[0].Cells["主键"].Value