11 个解决方案
#1
看看吧:
private static void InsertDB(string file)
{
FileInfo finfo = new FileInfo(file); //绝对路径
if (finfo.Exists)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "Insert into Categories(CategoryName,Picture) values('test',@Content)";
cmd.Parameters.Add("@Content", SqlDbType.Image, (int)finfo.Length, "Image字段名"); //注意,此处参数Size为写入的字节数
//读取文件内容,写入byte数组
byte[] content = new byte[finfo.Length];
FileStream stream = finfo.OpenRead();
stream.Read(content, 0, content.Length);
stream.Close();
cmd.Parameters["@Content"].Value = content; //为参数赋值
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
}
#2
ms_sql使用varbinary字段,varbinary最多只能8000字节,如果你要保存的二进制数据超过了8000字节,需要用text/ntext/image数据类型,
Oracle使用Blob字段,
参数化赋值就可以。
Oracle使用Blob字段,
参数化赋值就可以。
#3
谢谢 我回家试试先
#4
已经说完了
#5
这个好象对了
#6
public static byte[] f_imageToByte(string path,string iszip)
{
var str=HttpContext.Current.Server.MapPath(path);
var stream = new MemoryStream();
if (!File.Exists(str))
{
return null;
}
var bitmap = Bitmap.FromFile(str);
bitmap.Save(stream,ImageFormat.Jpeg);
var bts=stream.ToArray();
stream.Close();
stream.Dispose();
if (iszip=="1")
{
//str = Convert.ToBase64String(bts);
bts = NickLee.Common.CompressData.Compress<byte[]>(bts);
}
return bts;
}
public static void f_byteToImage(byte[] bts, string iszip,string path)
{
var str=string.Empty;
if (iszip == "1")
{
bts = NickLee.Common.CompressData.UnCompressObject<byte[]>(bts);
//bts = Convert.FromBase64String(str);
}
var ms = new MemoryStream(bts,false);
var objBitmap = new Bitmap(ms);
objBitmap.Save(path, ImageFormat.Jpeg);
ms.Close();
ms.Dispose();
}
{
var str=HttpContext.Current.Server.MapPath(path);
var stream = new MemoryStream();
if (!File.Exists(str))
{
return null;
}
var bitmap = Bitmap.FromFile(str);
bitmap.Save(stream,ImageFormat.Jpeg);
var bts=stream.ToArray();
stream.Close();
stream.Dispose();
if (iszip=="1")
{
//str = Convert.ToBase64String(bts);
bts = NickLee.Common.CompressData.Compress<byte[]>(bts);
}
return bts;
}
public static void f_byteToImage(byte[] bts, string iszip,string path)
{
var str=string.Empty;
if (iszip == "1")
{
bts = NickLee.Common.CompressData.UnCompressObject<byte[]>(bts);
//bts = Convert.FromBase64String(str);
}
var ms = new MemoryStream(bts,false);
var objBitmap = new Bitmap(ms);
objBitmap.Save(path, ImageFormat.Jpeg);
ms.Close();
ms.Dispose();
}
#7
if (FileUpload1.HasFile)
{
string fileName = FileUpload1.FileName;
int contentLength = FileUpload1.PostedFile.ContentLength;
string mime = FileUpload1.PostedFile.ContentType;
byte[] data = FileUpload1.FileBytes;
string sql = "insert into FileList(FileName,FileData,FileLength)values(@FileName,@FileData,@FileLength)";
SqlParameter[] parameters = new SqlParameter[4];
parameters[0] = new SqlParameter("@FileName", SqlDbType.NVarChar, 50);
parameters[0].Value = fileName;
parameters[1] = new SqlParameter("@FileData", SqlDbType.Image, int.MaxValue);
parameters[1].Value = data;
parameters[2] = new SqlParameter("@FileLength", SqlDbType.Int, 4);
parameters[2].Value = contentLength;
if (SqlDbHelper.ExecuteNonQuery(sql, CommandType.Text, parameters) > 0)
{
Response.Write("<script language='javascript'>alert('上传成功!');</script>");
}
}
或
using(System.IO.FileStream stream = new System.IO.FileStreamfile,System.IO.FileMode.Open,System.IO.FileAccess.Read)
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
string strName = System.IO.Path.GetFileNameWithoutExtension(file);
SqlCommand cmd = new SqlCommand("Insert into Photo(name,photo) values(@name,@image)", sqlConn);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = strName;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
cmd.ExecuteNonQuery();
}
#8
using(System.IO.FileStream stream = new System.IO.FileStreamfile,System.IO.FileMode.Open,System.IO.FileAccess.Read)
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
string strName = System.IO.Path.GetFileNameWithoutExtension(file);
SqlCommand cmd = new SqlCommand("Insert into Photo(name,photo) values(@name,@image)", sqlConn);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = strName;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
cmd.ExecuteNonQuery();
}
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
string strName = System.IO.Path.GetFileNameWithoutExtension(file);
SqlCommand cmd = new SqlCommand("Insert into Photo(name,photo) values(@name,@image)", sqlConn);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = strName;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
cmd.ExecuteNonQuery();
}
#9
参数化就可以了
还有一个问题,那个字段最好单独出来一张表
跟主表关联,
要不然,打开主表会死人的
还有一个问题,那个字段最好单独出来一张表
跟主表关联,
要不然,打开主表会死人的
#10
好多回复哦 谢谢你们了 我一个一个试试
#11
如果不转二进制呢?
#1
看看吧:
private static void InsertDB(string file)
{
FileInfo finfo = new FileInfo(file); //绝对路径
if (finfo.Exists)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "Insert into Categories(CategoryName,Picture) values('test',@Content)";
cmd.Parameters.Add("@Content", SqlDbType.Image, (int)finfo.Length, "Image字段名"); //注意,此处参数Size为写入的字节数
//读取文件内容,写入byte数组
byte[] content = new byte[finfo.Length];
FileStream stream = finfo.OpenRead();
stream.Read(content, 0, content.Length);
stream.Close();
cmd.Parameters["@Content"].Value = content; //为参数赋值
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
}
#2
ms_sql使用varbinary字段,varbinary最多只能8000字节,如果你要保存的二进制数据超过了8000字节,需要用text/ntext/image数据类型,
Oracle使用Blob字段,
参数化赋值就可以。
Oracle使用Blob字段,
参数化赋值就可以。
#3
谢谢 我回家试试先
#4
已经说完了
#5
这个好象对了
#6
public static byte[] f_imageToByte(string path,string iszip)
{
var str=HttpContext.Current.Server.MapPath(path);
var stream = new MemoryStream();
if (!File.Exists(str))
{
return null;
}
var bitmap = Bitmap.FromFile(str);
bitmap.Save(stream,ImageFormat.Jpeg);
var bts=stream.ToArray();
stream.Close();
stream.Dispose();
if (iszip=="1")
{
//str = Convert.ToBase64String(bts);
bts = NickLee.Common.CompressData.Compress<byte[]>(bts);
}
return bts;
}
public static void f_byteToImage(byte[] bts, string iszip,string path)
{
var str=string.Empty;
if (iszip == "1")
{
bts = NickLee.Common.CompressData.UnCompressObject<byte[]>(bts);
//bts = Convert.FromBase64String(str);
}
var ms = new MemoryStream(bts,false);
var objBitmap = new Bitmap(ms);
objBitmap.Save(path, ImageFormat.Jpeg);
ms.Close();
ms.Dispose();
}
{
var str=HttpContext.Current.Server.MapPath(path);
var stream = new MemoryStream();
if (!File.Exists(str))
{
return null;
}
var bitmap = Bitmap.FromFile(str);
bitmap.Save(stream,ImageFormat.Jpeg);
var bts=stream.ToArray();
stream.Close();
stream.Dispose();
if (iszip=="1")
{
//str = Convert.ToBase64String(bts);
bts = NickLee.Common.CompressData.Compress<byte[]>(bts);
}
return bts;
}
public static void f_byteToImage(byte[] bts, string iszip,string path)
{
var str=string.Empty;
if (iszip == "1")
{
bts = NickLee.Common.CompressData.UnCompressObject<byte[]>(bts);
//bts = Convert.FromBase64String(str);
}
var ms = new MemoryStream(bts,false);
var objBitmap = new Bitmap(ms);
objBitmap.Save(path, ImageFormat.Jpeg);
ms.Close();
ms.Dispose();
}
#7
if (FileUpload1.HasFile)
{
string fileName = FileUpload1.FileName;
int contentLength = FileUpload1.PostedFile.ContentLength;
string mime = FileUpload1.PostedFile.ContentType;
byte[] data = FileUpload1.FileBytes;
string sql = "insert into FileList(FileName,FileData,FileLength)values(@FileName,@FileData,@FileLength)";
SqlParameter[] parameters = new SqlParameter[4];
parameters[0] = new SqlParameter("@FileName", SqlDbType.NVarChar, 50);
parameters[0].Value = fileName;
parameters[1] = new SqlParameter("@FileData", SqlDbType.Image, int.MaxValue);
parameters[1].Value = data;
parameters[2] = new SqlParameter("@FileLength", SqlDbType.Int, 4);
parameters[2].Value = contentLength;
if (SqlDbHelper.ExecuteNonQuery(sql, CommandType.Text, parameters) > 0)
{
Response.Write("<script language='javascript'>alert('上传成功!');</script>");
}
}
或
using(System.IO.FileStream stream = new System.IO.FileStreamfile,System.IO.FileMode.Open,System.IO.FileAccess.Read)
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
string strName = System.IO.Path.GetFileNameWithoutExtension(file);
SqlCommand cmd = new SqlCommand("Insert into Photo(name,photo) values(@name,@image)", sqlConn);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = strName;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
cmd.ExecuteNonQuery();
}
#8
using(System.IO.FileStream stream = new System.IO.FileStreamfile,System.IO.FileMode.Open,System.IO.FileAccess.Read)
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
string strName = System.IO.Path.GetFileNameWithoutExtension(file);
SqlCommand cmd = new SqlCommand("Insert into Photo(name,photo) values(@name,@image)", sqlConn);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = strName;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
cmd.ExecuteNonQuery();
}
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
string strName = System.IO.Path.GetFileNameWithoutExtension(file);
SqlCommand cmd = new SqlCommand("Insert into Photo(name,photo) values(@name,@image)", sqlConn);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = strName;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
cmd.ExecuteNonQuery();
}
#9
参数化就可以了
还有一个问题,那个字段最好单独出来一张表
跟主表关联,
要不然,打开主表会死人的
还有一个问题,那个字段最好单独出来一张表
跟主表关联,
要不然,打开主表会死人的
#10
好多回复哦 谢谢你们了 我一个一个试试
#11
如果不转二进制呢?