2017-03-04 72 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;