//-------------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 = "";
}
}
我不知道哪个有问题。 insert语句或select语句。当我插入它不会给出任何异常但是当我尝试选择它时显示异常并且图片在图片框中显得模糊。我做错了什么?请帮忙。谢谢。
答案 0 :(得分:1)
虽然有更简单的方法可以完成某些步骤,例如
byte[] PictureBytes = File.ReadAllBytes(txtPictureURL.Text);
@Khan的评论也应该受到关注。
您的任何一种方法似乎都没有导致复制的任何降级。 我怀疑PictureBox属性可能正在缩放或拉伸图像。
要解决此问题,请将PictureBox.SizeMode属性更改为AutoSize 如果图像大于PictureBox,那么您可以实现如下答案的滚动条:https://stackoverflow.com/a/4710193/2549384