.net如何把视频存储到数据库

seakang 2003-05-31 05:49:05
.net如何把视频存储到数据库,并可以通过播放器播放显示
...全文
81 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
CMIC 2003-05-31
  • 打赏
  • 举报
回复
同意使用image
shuker 2003-05-31
  • 打赏
  • 举报
回复
用image类型的字段好了
Cnapollo 2003-05-31
  • 打赏
  • 举报
回复
给个例子参考吧
1.传文件到数据库页面.cs
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
BindData();
}
}
public void myInsert()
{

Int32 intFileLength;
string strFileType;
string strFileName;
Stream stmFile;
strFileName="";
strFileName=FileBox1.PostedFile.FileName;
intFileLength=FileBox1.PostedFile.ContentLength;
//文件类型,显示页面判断文件类型时用到(可识别为视频文件用mediaplay播出
strFileType=FileBox1.PostedFile.ContentType;
//strFileType="dd";
//从页面控件file中取以字节流读出
stmFile=FileBox1.PostedFile.InputStream;
byte[] ByteFile=new byte[intFileLength];
stmFile.Read(ByteFile,0,intFileLength);
/*如直接读取指定路径文件可参考这里
FileStream fs = new FileStream("aa.doc", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] DocByte = br.ReadBytes((int)fs.Length);
fs.Close();
*/

SqlConnection myCon=new SqlConnection("server=(local);database=test;uid=sa;pwd=");
string strCom="INSERT INTO FileByField Values(@Name,@FileData,@FileType,@FileLength)";
SqlCommand myCom=new SqlCommand(strCom,myCon);
myCom.Parameters.Add("@Name",SqlDbType.Char,255);
myCom.Parameters.Add("@FileData",SqlDbType.Image);
myCom.Parameters.Add("@FileType",SqlDbType.Char,255);
myCom.Parameters.Add("@FileLength",SqlDbType.BigInt);

myCom.Parameters["@Name"].Value=strFileName.Trim();
myCom.Parameters["@FileData"].Value=ByteFile;
myCom.Parameters["@FileType"].Value=strFileType;
myCom.Parameters["@FileLength"].Value=intFileLength;


myCon.Open();
try
{

myCom.ExecuteNonQuery();

}
catch(SqlException e)
{
if(e.Number==2627)
Response.Write("PrimaryKey Error");
else
Response.Write("Other Error");
}
myCon.Close();
BindData();
}
private void BindData( )
{
SqlConnection myCon=new SqlConnection("server=(local);database=test;uid=sa;pwd=");
string strCom="Select PKID,Name,FileType,FileLength From FileByField";
SqlCommand myDap=new SqlCommand(strCom,myCon);
myDap.CommandType=CommandType.Text;
myDap.Connection.Open();
SqlDataReader drd=myDap.ExecuteReader();

dgdTest.DataSource=drd;
dgdTest.DataBind();
//int intPKID=Int32.Parse(myDst.Tables[0].DefaultView[0].Row["PKID"].ToString());

myCon.Close();


}
页面元素:
<td><asp:datagrid id="dgdTest" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="PKID" HeaderText="PKID"></asp:BoundColumn>
<asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="FileType" HeaderText="ContentType"></asp:BoundColumn>
<asp:HyperLinkColumn DataNavigateUrlField="PKID" DataNavigateUrlFormatString="GetDocByDB.aspx?PKID={0}" DataTextField="Name" HeaderText="File"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="FileLength" HeaderText="FileLength"></asp:BoundColumn>
</Columns>
</asp:datagrid></td>
2.显示页面GetDocByDB.aspx.cs
public string strTitle;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
View();
}
private void View()
{
SqlConnection myCon=new SqlConnection("server=guohongwei;database=test;uid=sa;pwd=");
string strCom="Select * From FileByField where PKID='"+ Int32.Parse(Request["PKID"].ToString()) +"'";
SqlDataAdapter myDap=new SqlDataAdapter(strCom,myCon);
DataSet myDst=new DataSet();
myDap.Fill(myDst,"FileByField");

strTitle=myDst.Tables[0].DefaultView[0].Row["Name"].ToString().Trim();
string[] strsTitle=strTitle.Split('\\');
strTitle=strsTitle[strsTitle.Length-1].TrimEnd();
Response.Clear();
Response.ContentType=myDst.Tables[0].DefaultView[0].Row["FileType"].ToString();
Response.AddHeader("Content-Disposition", "inline;filename=\"" + strTitle + "\";UrlEncode=true");
Response.AddHeader("Content-Length", myDst.Tables[0].DefaultView[0].Row["FileLength"].ToString());
Response.BinaryWrite((byte[])myDst.Tables[0].DefaultView[0].Row["FileData"]);
// Response.OutputStream.Write((byte[])myDst.Tables[0].DefaultView[0].Row["FileData"],0,Int32.Parse(myDst.Tables[0].DefaultView[0].Row["FileLength"].ToString()));
Response.End();

myCon.Close();
}

Cnapollo 2003-05-31
  • 打赏
  • 举报
回复
把电影保存为image字段(最大存2G的文件),以字节流形式写入,显示时用response.binarywrite(byte[])输出

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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