pictureBox 控件 Buttob控件
参考代码(显示用户选择的图片,点击上传按钮之后就以二进制保存到数据库中):
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
{
System.Drawing.Bitmap objPic;
objPic = new System.Drawing.Bitmap(openFileDialog1.FileName.ToString().Trim());
pictureBox1.Image = new System.Drawing.Bitmap(objPic, 200, 200);
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string conString = "Data Source =.; Initial Catalog = ;User ID = ; Pwd = ";
SqlConnection connection = new SqlConnection(conString);
string sql = "insert c values (1,@TP)";
SqlCommand command = new SqlCommand(sql, connection);
string picturePath = openFileDialog1.FileName;
FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
Byte[] mybyte = new byte[fs.Length];
fs.Read(mybyte, 0, mybyte.Length);
fs.Close();
SqlParameter prm = new SqlParameter
("@TP", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
command.Parameters.Add(prm);
connection.Open();
int result = command.ExecuteNonQuery();
connection.Close();
if (result > 0) { MessageBox.Show("上传成功!", "提示");}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
来了来了