请高手解释下 下面的代码。请高手加上注释,由于小弟新手 请每句代码都能加上注释。谢谢谢谢
OpenFileDialog open = new OpenFileDialog();
open.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";
if (open.ShowDialog() == DialogResult.OK)
{
this.txtSelectPath.Text = open.FileName;
picImg.ImageLocation = open.FileName;
FileStream file = new FileStream(this.txtSelectPath.Text, FileMode.Open, FileAccess.Read);
Byte[] bt = new Byte[file.Length];
BinaryReader br = new BinaryReader(file);
bt = br.ReadBytes(Convert.ToInt32(file.Length));
SqlConnection con = new SqlConnection("server=.;database=img;uid=sa;pwd=123");
con.Open();
string str = string.Format("insert into imgs values(@Image)");
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.Add("@id", SqlDbType.Int, 4);
cmd.Parameters.Add("@Image", SqlDbType.Image);
cmd.Parameters["@id"].Value = 1;
cmd.Parameters["@Image"].Value = bt;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("图片上传成功");
}
OpenFileDialog open = new OpenFileDialog();
open.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";
if (open.ShowDialog() == DialogResult.OK)
{
this.txtSelectPath.Text = open.FileName;
picImg.ImageLocation = open.FileName;
FileStream file = new FileStream(this.txtSelectPath.Text, FileMode.Open, FileAccess.Read);
Byte[] bt = new Byte[file.Length];
BinaryReader br = new BinaryReader(file);
bt = br.ReadBytes(Convert.ToInt32(file.Length));
SqlConnection con = new SqlConnection("server=.;database=img;uid=sa;pwd=123");
con.Open();
string str = string.Format("insert into imgs values(@Image)");
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.Add("@id", SqlDbType.Int, 4);
cmd.Parameters.Add("@Image", SqlDbType.Image);
cmd.Parameters["@id"].Value = 1;
cmd.Parameters["@Image"].Value = bt;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("图片上传成功");
}
byte[] bytesOriginal = new byte[Convert.ToInt32(stream.Length)];
stream.Read(bytesOriginal, 0, Convert.ToInt32(stream.Length));
stream.Flush();
stream.Dispose();
不需要匹配啊?又不在数据库中保存文件内容,就显然不需要匹配文件内容。而从数据库中读出文件路径是,你如果判断一下 if(file.Exists) 不就匹配了嘛!