将图片保存到SQL2000数据库中

数据库脚本:
CREATE   TABLE   [ dbo ] . [ Pictures ]  (
    
[ kFileName ]   [ bigint ]   IDENTITY  ( 1 1 NOT   NULL  ,
    
[ Picture ]   [ image ]   NULL  ,
    
[ FileName ]   [ varchar ]  ( 250 ) COLLATE SQL_Latin1_General_CP1_CI_AS  NULL  
ON   [ PRIMARY ]  TEXTIMAGE_ON  [ PRIMARY ]
GO

CREATE   PROCEDURE   [ dbo ] . [ UploadFile ]
(
    
@Picture   image ,
    
@FileName   varchar ( 250 ),
    
@kFileName   bigint  output
)
AS
insert   into  Pictures(Picture, FileName)  values  ( @Picture , @FileName )
set   @kFileName   =   @@IDENTITY
GO
Demo:
<% @ Page language="c#" Codebehind="UploadImage.aspx.cs" AutoEventWireup="false" Inherits="ZKSTAT.UploadImage"  %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
< HTML >
    
< HEAD >
        
< title > UploadImage </ title >
        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
    
</ HEAD >
    
< body >
        
< form  id ="Form1"  method ="post"  runat ="server" >
            
< FONT  face ="宋体" >< INPUT  id ="File1"  type ="file"  name ="File1"  runat ="server" >
                
< asp:Button  id ="Button1"  runat ="server"  Text ="上传" ></ asp:Button ></ FONT >
        
</ form >
    
</ body >
</ HTML >

// ***********************************************************
// *公司:
// *作者:YK
// *模块:
// *功能:上传图片到数据库
// *创建日期:
// *修改日期:
// ***********************************************************
using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Data.SqlClient;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;
using  System.IO;
using  System.Configuration;
namespace  ZKSTAT
{
    
/// <summary>
    
/// UploadImage 的摘要说明。
    
/// </summary>

    public class UploadImage : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Button Button1;
        
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
        }


        
Web 窗体设计器生成的代码

        
private void Button1_Click(object sender, System.EventArgs e)
        
{
            
if(File1.Value!=""&&File1.PostedFile.ContentLength!=0)
            
{

                FileStream fs
=null;
                
try
                
{    
                    fs 
= new FileStream(File1.PostedFile.FileName,FileMode.Open);

                    FileInfo fi 
= new FileInfo(File1.PostedFile.FileName);
                    
long temp = fi.Length;
                    
int lung = Convert.ToInt32(temp);

                    
byte[] picture=new byte[lung];
                    fs.Read(picture,
0,lung);
                    fs.Close();

                    
long result = uploadFileToDatabase(picture,fi.Name);
                }

                
catch(Exception ex)
                
{
                    
throw ex;
                }

            }

        }


        
public static string ConnectionString()
        
{
            
return ConfigurationSettings.AppSettings["ConnectionString"].ToString();
        }


        
//上传图片到数据库
        private long uploadFileToDatabase(byte[] picture, string fileName)
        
{

            SqlConnection conn 
= null;
            SqlCommand cmd 
=null;
            SqlParameter kFileName 
=null;
            SqlParameter FileName 
=null;
            SqlParameter pic 
=null;
            
long result=-1
    
            
try
            
{

                conn 
= new SqlConnection(ConnectionString());
                cmd 
= new SqlCommand("UploadFile",conn);

                cmd.CommandType 
= System.Data.CommandType.StoredProcedure;

                kFileName 
= new SqlParameter("@kFileName"
                    System.Data.SqlDbType.BigInt,
8);
                kFileName.Direction 
= ParameterDirection.Output;

                pic 
= new SqlParameter("@picture",SqlDbType.Image); 
                pic.Value 
= picture; 
    
                FileName 
= new SqlParameter("@FileName",SqlDbType.VarChar,250);
                FileName.Value 
= fileName;

                cmd.Parameters.Add(pic);
                cmd.Parameters.Add(FileName);
                cmd.Parameters.Add(kFileName);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                result 
= (long)kFileName.Value;
                conn.Dispose();
                cmd.Dispose();

            }

            
catch
            
{
                result 
= -1;

            }


            
return result; 
        }

    }

}

你可能感兴趣的:(sql2000)