如何使用C#和SQL加载下一行单击事件



i有一个PictureBox,其中图像是从SQL数据库中检索的。我要做的是在单击下一个按钮的单击事件上加载下一个图像。

public void nextcloth(SqlConnection conn ,SqlCommand comd )
{
    try
    {
        string sql = "select productPIC from product where productType = 'Fabric'";
         if (conn.State != ConnectionState.Open)
         {
            conn.Open();
         }
        comd = new SqlCommand(sql, conn);
        SqlDataReader dr = comd.ExecuteReader()
        while (dr.HasRows)
        {
            byte[] img = (byte[])(dr[2]);
            if (img == null)
            {
                p.Image = null;
            }
            else
            {
                MemoryStream ms = new MemoryStream(img);
                p.Image = Image.FromStream(ms);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

按钮代码

private void button7_Click(object sender, EventArgs e)
{
    obj.nextcloth(con, cmd);
    pictureBox2.Image = obj.getpic();
    pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
    pictureBox2.BorderStyle = BorderStyle.Fixed3D;
}

您的表格应该具有PictureorderIndex字段。然后,您可以获取该productType的所有PictureorderIndex值,然后将它们存储在数组中。每当按钮是单击数组中的下一个项目,然后使用该数组中的值以获取产品表中的下一张图片。

公共类MainClass {

private int [] imagesIndexArray; 私有int imagecurrentIndex;

公共空隙mainmethod(...){ ImageCurrentIndex = 0; 字符串sql ="从productType ='fabric'的产品中选择pictureorderIndex'; //运行SQL并填充ImagesIndexArray。我知道您可以完成此部分。

}

public void nextCloth(SQLConnection Conn,sqlcommand comd) { 尝试 { 字符串sql ="从产品中选择productpic,其中productType ='babric''and ticleOrderIndex =" imagecurrentIndex.toString(); //您的其余代码...... //不要忘记在成功呼叫后增加索引

         imageCurrentIndex++;

最新更新