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 = "";
            }
        }

我不知道这其中有什么问题。插入语句或选择语句。当我插入它时没有给出任何异常,但是当我尝试选择它时显示异常并且图片在图片框中显得模糊。我做错了什么?请帮忙。谢谢。

4

1 回答 1

1

虽然有更简单的方法可以完成您的某些步骤,例如
byte[] PictureBytes = File.ReadAllBytes(txtPictureURL.Text);
还应注意@Khan 的评论。

您的任何一种方法似乎都不会导致复制品质量下降。我怀疑 PictureBox 属性可能会缩放或拉伸图像。

要解决该问题,请将 PictureBox.SizeMode 属性更改为 AutoSize。
如果图像大于 PictureBox,那么您可以像这个答案一样实现滚动条: https ://stackoverflow.com/a/4710193/2549384

于 2015-12-31T02:51:42.603 回答