c# winform 程序 如何以字节流方式上传文件到数据库

usernamezero 2010-03-07 06:14:36
如题
...全文
1057 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
usernamezero 2010-03-07
  • 打赏
  • 举报
回复
lzsho622 谢谢
lzsh0622 2010-03-07
  • 打赏
  • 举报
回复

byte[] fieldValue = File.ReadAllBytes("C:\\ls.bmp");

using (SqlConnection sqlcon = new SqlConnection("SQL连接字符串"))
{
string cmdtxt = "UPDATE 数据表名 SET 列名 =@fieldValue WHERE 主键 = 1 ";
SqlCommand sqlcmd = new SqlCommand(cmdtxt, sqlcon);
sqlcmd.Parameters.Add("@fieldValue", SqlDbType.Image).Value = fieldValue;
try
{
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message); return false;
}
}

usernamezero 2010-03-07
  • 打赏
  • 举报
回复
有哪个大哥帮忙解决一下
fuheng 2010-03-07
  • 打赏
  • 举报
回复
顶一下,我来学习了,O(∩_∩)O~
BATTLERxANGE 2010-03-07
  • 打赏
  • 举报
回复
这样的设计不太好!建议将文件存放到硬盘的某个地方,然后数据库中存放文件的物理路径。
usernamezero 2010-03-07
  • 打赏
  • 举报
回复
楼上的哥哥 看看把这句的postArray 换成什么东西
string strSql = "Update PrintModel set filecontent='" + postArray + "',FileName='" + FileName + "' where ID='" + PrintModelID + "'";
wuyq11 2010-03-07
  • 打赏
  • 举报
回复
FileStream filestream = new FileStream(Application.StartupPath + "a.txt", FileMode.Open, FileAccess.Read);
BinaryReader filerd = new BinaryReader(filestream,Encoding .Default );
byte[] filebyte = new byte[filestream.Length];
filerd.Read(filebyte, 0, (int)filestream.Length);
filerd.Close();
filestream.Close();
usernamezero 2010-03-07
  • 打赏
  • 举报
回复
上边是我找的,这样传进去写入的postArray 不对啊不全  还有谁做过帮下忙撒
usernamezero 2010-03-07
  • 打赏
  • 举报
回复
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
FilePath = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
FileName = System.IO.Path.GetFileName(openFileDialog1.FileName);
//如果要路径加文件名
strPathName = openFileDialog1.FileName;
}
//将文件添加到FileStream类中
FileStream fs = new FileStream(strPathName, FileMode.Open, FileAccess.Read);
//通过FileStream实例化BinaryReader类
BinaryReader r = new BinaryReader(fs);
//通过BinaryReader类对象的ReadBytes()方法将FileStream类对象转化为二进制数组
byte[] postArray = r.ReadBytes((int)fs.Length);
if (fs.CanRead)
{
int FileSize = Convert.ToInt32(fs.Length);
string strSql = "Update PrintModel set filecontent='" + postArray + "',FileName='" + FileName + "' where ID='" + PrintModelID + "'";
IRolaDataAccess db = RolaDataAccessFactory.Create();
db.ExecuteNonQuery(strSql);
dataGridView1.CurrentRow.Cells["filename"].Value = FileName;
MessageBox.Show("上传成功", "提示");
}
xray2005 2010-03-07
  • 打赏
  • 举报
回复
把数据库字段设置成 大对象 类型。

然后,把文件使用 Stream对象读到一个byte[] 里面,然后执行,insert table(xx) values(byte数组)就是了。

比如:
FileInfo fi=new FileInfo(filePath);
FileStream fileStream=fi.OpenRead();
int length=(int)fileStream.Length;
byte[] fileData=new byte[length];
fileStream.Read(fileData,0,length);

//Sql
OleDbCommand command = new OleDbCommand ("INSERT INTO t1 (filename,fileData)" + "VALUES (@filename,@fileData)", myConnection);
//add para 1
System.Data.OleDb.OleDbParameter parafileName = new OleDbParameter("@filename", System.Data.OleDb.OleDbType.VarChar,50);
parafileName.Value ="it.jpg";
command.Parameters.Add(parafileName);

//add para2
System.Data.OleDb.OleDbParameter paramPersonImage = new OleDbParameter("@fileData", System.Data.OleDb.OleDbType.Binary);
paramPersonImage.Value = fileData;
command.Parameters.Add(paramPersonImage);

//Excute
this.OpenDB();
command.ExecuteNonQuery();
myConnection.Close();

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧