2017-03-04 71 views
-1

我在使用文本框和图片框更新数据库中的记录时遇到问题。当我尝试更新时,它给了我错误:无法找到路径的一部分。更新Winforms应用程序中的SQL记录(语言:C#)

下面是编码:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.Sql; 
using System.Data.SqlClient; 
using System.IO; 

namespace CO6009DissertationV5 
{ 
public partial class frmViewCatManagement : Form 
{ 
    SqlConnection con = new SqlConnection("Data Source = SQL2016; Initial Catalog = db; Persist Security Info=True;User ID = user_db; Password=#####"); 
    SqlCommand com; 
    string imgLoc = ""; 


    public frmViewCatManagement() 
    { 
     InitializeComponent(); 
    } 

    private void frmViewCarManagement_Load(object sender, EventArgs e) 
    { 
     //DataGridViewImageColumn dgvCarCat = new DataGridViewImageColumn(); 
     //dgvCarCat.HeaderText = "Image"; 
     //dgvCarCat.ImageLayout = DataGridViewImageCellLayout.Stretch; 
     //dataGridView1.Col 
    } 

    // 
    // Buttons 
    // 
    private void btnUpdate_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      string imgPath = txtPath.Text; 
      FileStream fs; 
      fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read); 
      byte[] picbyte = new byte[fs.Length]; 
      fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length)); 
      fs.Close(); 
      con.Open(); 
      string qry = "UPDATE tbl_catManagement SET (catclass, carmake, cardescription, carimage) VALUES ('" + txtCatClass.Text + "', '" + txtCarMake.Text + "','" + txtCarDesc.Text + "', @img)"; 
      //catclass='" + txtCatClass + "', carmake='" + txtCarMake.Text + "', cardescription='" + txtCarDesc.Text + "', carimagePath'" + txtPath.Text + "', @img)"; 
      SqlParameter imgParameter = new SqlParameter(); 
      imgParameter.SqlDbType = SqlDbType.Image; 
      imgParameter.ParameterName = "img"; 
      imgParameter.Value = picbyte; 
      SqlCommand cmd = new SqlCommand(qry, con); 
      cmd.Parameters.Add(imgParameter); 
      cmd.ExecuteNonQuery(); 
      MessageBox.Show("Data Updated"); 
      cmd.Dispose(); 
      con.Close(); 
     } 

     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

     finally 
     { 
      if (con.State == ConnectionState.Open) 
      { 
       con.Close(); 
      } 
     } 

    } 

    private void btnFindData_Click(object sender, EventArgs e) 
    { 
     //string sqlQuery = "SELECT catname,catdescription,carimage FROM tbl_manageCat WHERE catid= '" + txtCatID.Text + "'"; 
     //cmd = new SqlCommand(sqlQuery, connection); 
     //SqlDataReader DataRead = cmd.ExecuteReader(); 
     //DataRead.Read(); 

     //if (DataRead.HasRows) 
     //{ 
     // txtCarClass.Text = DataRead[0].ToString(); 
     // txtCarName.Text = DataRead[1].ToString(); 
     // txtCatDesc.Text = DataRead[2].ToString(); 
     // byte[] images = ((byte[])DataRead[1]); 

     // if (images == null) 
     // { 
     //  picBoxCar.Image = null; 
     // } 

     // else 
     // { 
     //  MemoryStream mstream = new MemoryStream(images); 
     //  picBoxCar.Image = Image.FromStream(mstream); 
     // } 
     //} 

     //else 
     //{ 
     // MessageBox.Show("This Data is not available"); 
     //} 

     //connection.Close(); 

     //dataCarCategory.ColumnCount = 3; 
     //dataCarCategory.Columns[0].Name = "catid"; 
     //dataCarCategory.Columns[1].Name = "catname"; 
     //dataCarCategory.Columns[2].Name = "catdescription"; 

     //DataGridViewImageColumn img = new DataGridViewImageColumn(); 
     //Image image = Image.FromFile("Image Path"); 
     //img.Image = image; 
     //dataCarCategory.Columns.Add(img); 
     //img.HeaderText = "carimage"; 
     //img.Name = "img"; 
    } 

    private void btnViewData_Click(object sender, EventArgs e) 
    { 
     //try 
     //{ 
     //if (txtCatID.Text == "") 
     //{ 
     // MessageBox.Show("Please Enter Category ID"); 
     // return; 
     //} 

     //SqlCommand cmd = new SqlCommand("SELECT carimage FROM tbl_catManagement WHERE catid=" + txtCatID.Text, con); 

     ////SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_catManagement catid= " + txtCatID.Text + "catclass= " + txtCarClass, con); 
     //SqlDataAdapter da = new SqlDataAdapter(cmd); 
     //DataSet ds = new DataSet(); 
     //da.Fill(ds); 

     //if(ds.Tables[0].Rows.Count > 0) 
     //{ 
     // //MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0].["carimage"]); 

     // MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["carimage"]); 
     // picBoxCar2.Image = new Bitmap(ms); 

     // panel4.Visible = true; 
     // panel5.Visible = false; 
     //} 

     //else 
     //{ 
     // MessageBox.Show("Image not found"); 
     //} 
     //} 

     //catch (Exception ex) 
     //{ 
     // MessageBox.Show(ex.Message); 
     //} 

     //finally 
     //{ 
     // if (con.State == ConnectionState.Open) 
     // { 
     //  con.Close(); 
     // } 
     //} 

     con.Open(); 
     string sqlQry = "SELECT catclass, carmake, cardescription, carimagePath, carimage FROM tbl_catManagement WHERE catid= '" + txtCatID.Text + "'"; 
     com = new SqlCommand(sqlQry, con); 
     SqlDataReader sdr = com.ExecuteReader(); 
     sdr.Read(); 

     if (sdr.HasRows) 
     { 
      txtCatClass.Text = sdr[0].ToString(); 
      txtCarMake.Text = sdr[1].ToString(); 
      txtCarDesc.Text = sdr[2].ToString(); 
      txtPath.Text = sdr[3].ToString(); 
      byte[] img = (byte[])sdr[4]; 
      panel4.Visible = true; 
      panel5.Visible = false; 
      panel6.Visible = false; 
      panel2.Visible = true; 


      if (img == null) 
      { 
       picBoxCar2.Image = null; 
      } 

      else 

      { 
       MemoryStream mstream = new MemoryStream(img); 
       picBoxCar2.Image = Image.FromStream(mstream); 
      } 
     } 

     else 
     { 
      MessageBox.Show("This data is not available"); 
     } 

     con.Close(); 
    } 

    private void btnViewAllData_Click(object sender, EventArgs e) 
    { 
     frmDGVCatManagement frmdvgCM = new frmDGVCatManagement(); 
     frmdvgCM.Show(); 
    } 

    private void btnFindCategory_Click(object sender, EventArgs e) 
    { 
     panel4.Visible = false; 
     panel5.Visible = true; 
     panel2.Visible = false; 
     panel6.Visible = false; 
    } 

    private void btnBrowse_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog ofd = new OpenFileDialog(); 
     ofd.Filter = "png files(*.png)|*.png|jpg files(*.jpg)|*.jpg|All files(*.*)|*.*"; 
     if (ofd.ShowDialog() == DialogResult.OK) 
     { 
      txtPath.Text = ofd.FileName; 
      picBoxCar2.Image = new Bitmap(ofd.FileName); 
     } 
    } 
} 
    } 

任何帮助,将不胜感激

+1

代码中的哪一行出现错误?你可以分享它给出的样本路径值错误/ –

+0

它不会在线上发生错误。但是,当我尝试单击更新按钮时,它会显示错误消息:无法找到路径的一部分。 – AdrianAndrews

+0

您正在看到消息,因为您已经使用了try catch块,并且在发生异常时,它将捕获块和messagebox作为异常消息。您需要删除try catch或调试代码并查看发生异常的行并转到catch块。正如在答案中提到的Sujit看起来像您在代码中使用的文件路径无效。 –

回答

0

“找不到路径的一部分”是指文件的位置是不正确?你可以验证该文件是否存在于该位置?有时当你只通过文件名引用文件时,可能会出现这个问题?

建议您在获取任何路径错误时始终使用FULL PATH,以便在您的情况下尝试在下面的行中添加一个类似“C:\ myPics \ mypic.jpg”的路径。 string imgPath = txtPath.Text;