取得文件流及相关信息后存入数据库中HttpPostedFile upFile = up_file.PostedFile;
int iFileLength = upFile.ContentLength;
Byte[] FileByteArray = new Byte[iFileLength];
Stream StreamObject = upFile.InputStream;
StreamObject.Read(FileByteArray, 0, iFileLength);
SqlConnection conn = new SqlConnection("server=yy;uid=sa;pwd=;database=pany");
string sql = "insert into t_imgs (imgData, type, description, imgSize) values "
+ "(@Image, @ContentType, @ImageDescription, @ImgSize)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Image", SqlDbType.Binary, iFileLength).Value = FileByteArray;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = upFile.ContentType;
cmd.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDesc.Text;
cmd.Parameters.Add("@ImgSize", SqlDbType.BigInt, 8).Value = upFile.ContentLength;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
int iFileLength = upFile.ContentLength;
Byte[] FileByteArray = new Byte[iFileLength];
Stream StreamObject = upFile.InputStream;
StreamObject.Read(FileByteArray, 0, iFileLength);
SqlConnection conn = new SqlConnection("server=yy;uid=sa;pwd=;database=pany");
string sql = "insert into t_imgs (imgData, type, description, imgSize) values "
+ "(@Image, @ContentType, @ImageDescription, @ImgSize)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Image", SqlDbType.Binary, iFileLength).Value = FileByteArray;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = upFile.ContentType;
cmd.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDesc.Text;
cmd.Parameters.Add("@ImgSize", SqlDbType.BigInt, 8).Value = upFile.ContentLength;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
OleDbCommand Cmd = Conn.CreateCommand();
Cmd.CommandText = "Insert Into t_Goods_Image(Goods_ID, Goods_Image) Values (?, ?)";
OleDbParameter ParamID = Cmd.Parameters.Add(new OleDbParameter("Goods_ID", System.Data.OleDb.OleDbType.VarWChar, 50, "Goods_ID"));
OleDbParameter ParamImage = Cmd.Parameters.Add(new OleDbParameter("Goods_Image", System.Data.OleDb.OleDbType.VarBinary, 0, "Goods_Image"));
ParamID.Value = GoodsID.ToString();
try
{
FileStream ImageStream = File.OpenRead(lblPicPath.Text);
byte[] bytStream = new byte[ImageStream.Length];
long lngRead = ImageStream.Length;
lngRead = ImageStream.Read(bytStream, 0, (int)lngRead);
ParamImage.Value = bytStream;
}
catch(FileNotFoundException Ex)
{
MessageBox.Show("未找到指定图像文件!");
Console.Write(Ex.Message);
}
catch(FileLoadException Ex)
{
MessageBox.Show("无法读取图像文件!");
Console.Write(Ex.Message);
}
DBConnection.OpenConnection(Conn);
long lngAffected = Cmd.ExecuteNonQuery();
Conn.Close();