2012-04-24 39 views
2
//-------------SELECT STATEMENT--------- 
private void comboBoxLogin_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (comboBoxLogin.SelectedIndex.ToString() != string.Empty) 
       splitContainer1.Panel2.Enabled = true; 
      if (comboBoxLogin.SelectedItem.ToString() == "Create New User") 
       groupBoxNewUser.Visible = true; 
      else groupBoxNewUser.Visible = false; 
      if(comboBoxLogin.SelectedItem.ToString()!="Create New User"){ 

       string DBConnection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Timesheet.mdf;Integrated Security=True;User Instance=True"; 
       sqlConnection = new SqlConnection(DBConnection); 

       try { 
        sqlConnection.Open(); 
        SqlCommand sqlCommand = sqlConnection.CreateCommand(); 
        sqlCommand.CommandType = System.Data.CommandType.Text; 
        sqlCommand.CommandText = "SELECT firstname, lastname, NDFuserID, picture FROM UserRegistration WHERE NDFuserID='" +comboBoxLogin.SelectedItem + "'"; 
        SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); 

        if(sqlDataReader.Read()) 
        { 
         labelUserDetails.Text = sqlDataReader["firstname"].ToString() + " " + sqlDataReader["lastname"].ToString(); 
         byte[] pictureByteReader = (byte[])sqlDataReader["picture"]; 
         MemoryStream ms = new MemoryStream(pictureByteReader); 
         Image picture = Image.FromStream(ms); 
         pictureBoxUserDetails.Image = picture; 
        } 

        comboBoxItems.Refresh(); 
       } 
       catch(Exception ex){ 
        MessageBox.Show(ex.ToString()); 
       } 
       finally{ 
       sqlConnection.Close(); 
       } 
      } 
     } 
//--------------------INSERT STATEMENT------------------------- 

private void btnCreateNewUser_Click(object sender, EventArgs e) 
     { 
      string DBConnection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Timesheet.mdf;Integrated Security=True;User Instance=True"; 
      sqlConnection = new SqlConnection(DBConnection); 

      try 
      { 
       //--Insert statement for a picture----------------------- 
       FileInfo fileImage = new FileInfo(txtPictureURL.Text); 
       var fileLength = fileImage.Length; 
       byte[] picutreByte = new byte[Convert.ToInt32(fileLength)]; 
       FileStream fileStreams = new FileStream(txtPictureURL.Text, FileMode.Open, FileAccess.Read, FileShare.Read); 
       int readByte = fileStreams.Read(picutreByte, 0, Convert.ToInt32(fileLength)); 
       fileStreams.Close(); 

       sqlConnection.Open(); 
       SqlCommand sqlCommand = sqlConnection.CreateCommand(); 
       sqlCommand.CommandType = System.Data.CommandType.Text; 
       sqlCommand.CommandText = "INSERT INTO UserRegistration(firstname, lastname, NDFuserID, phone, picture) VALUES(@firstname, @lastname, @NDFuserID, @phone, @picture)"; 

       sqlCommand.Parameters.Add("@firstname", SqlDbType.NVarChar, 50); 
       sqlCommand.Parameters.Add("@lastname", SqlDbType.NVarChar, 50); 
       sqlCommand.Parameters.Add("@NDFuserID", SqlDbType.NChar, 10); 
       sqlCommand.Parameters.Add("@phone", SqlDbType.NVarChar); 
       sqlCommand.Parameters.Add("@picture", SqlDbType.Image); 

       sqlCommand.Parameters["@firstname"].Value = txtFirstName.Text; 
       sqlCommand.Parameters["@lastname"].Value = txtLastname.Text; 
       sqlCommand.Parameters["@NDFuserID"].Value = "NDF-" +txtUserID.Text; 
       sqlCommand.Parameters["@phone"].Value = maskedtxtPhone.Text; 
       sqlCommand.Parameters["@picture"].Value = picutreByte; 
       sqlCommand.ExecuteNonQuery(); 
      } 

      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 
      finally 
      { 
       sqlConnection.Close(); 
       txtFirstName.Text = ""; 
       txtLastname.Text = ""; 
       txtPictureURL.Text = ""; 
       txtUserID.Text = ""; 
       maskedtxtPhone.Text = ""; 
      } 
     } 

我不知道這是哪個問題。插入語句或選擇語句。當我插入它並沒有給出任何例外,但是當我嘗試選擇它時,顯示一個異常並且圖片在圖片框中顯得模糊。我做錯了什麼?請幫忙。謝謝。選擇語句後圖片顯示模糊

+0

爲什麼要將fileLength轉換爲Int32?你可能會截斷它。 – Khan 2012-04-24 14:17:41

+0

存儲的圖像和圖片框的大小(以像素爲單位)是多少? – 2012-04-24 14:18:37

回答

1

雖然有更簡單的方法來完成你的一些步驟,比如
byte[] PictureBytes = File.ReadAllBytes(txtPictureURL.Text);
同樣來自@Khan註釋應該得到重視。

您的兩種方法都不會導致複製中的任何降級。 我懷疑PictureBox屬性可能會縮放或拉伸圖像。

要解決該問題,請將PictureBox.SizeMode屬性更改爲AutoSize。
如果圖像比圖片框大,那麼你可以實現這樣的滾動條回答:https://stackoverflow.com/a/4710193/2549384