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