或者从其中删除?

解决方案 »

  1.   

    //存入图片
    private void Button1_Click(object sender, System.EventArgs e)
    {SqlConnection con = new SqlConnection("Server=ss;uid=sa;pwd=;database=northwind");
    SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
    SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
    DataSet ds = new DataSet("MyImages");
    da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    FileStream fs = new FileStream(@"d:\a.jpg", FileMode.OpenOrCreate, FileAccess.Read);
    byte[] MyData= new byte[fs.Length];
    fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
    fs.Close();
    da.Fill(ds,"MyImages");
    DataRow myRow=ds.Tables["MyImages"].NewRow();
    myRow["Description"] = "This would be description text";
    myRow["imgField"] = MyData;
    ds.Tables["MyImages"].Rows.Add(myRow);
    da.Update(ds, "MyImages");
    con.Close();}//取出图片
    private void Button2_Click(object sender, System.EventArgs e)
    {
    SqlConnection con = new SqlConnection("Server=ss;uid=sa;pwd=;database=northwind");
    SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages",con);
    SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
    DataSet ds = new DataSet("MyImages");
    byte[] MyData= new byte[0];
    da.Fill(ds,"MyImages");
    DataRow myRow=ds.Tables["MyImages"].Rows[0];
    MyData=(byte[])myRow["imgField"];
    int ArraySize = new int();
    ArraySize = MyData.GetUpperBound(0); 
    FileStream fs = new FileStream(@"d:\b.jpg", 
    FileMode.OpenOrCreate, FileAccess.Write);
    fs.Write(MyData, 0,ArraySize);
    fs.Close();
    }
      

  2.   

    共有二個文件,其中uploadfile.aspx源碼如下
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="UploadFile.aspx.vb" Inherits="Upload.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <asp:Label id="Message" runat="server">選擇文件和文件名字:</asp:Label>
    <form id="Form1" method="post" runat="server">
    <b>文件名字:</b><input id="MyFileName" type="text" runat="server">
    <p/>
    <b>文件:</b><input id="MyFile" type="file" runat="server">
    <br>
    <br>
    <input type="submit" value="開始上傳" onserverclick="UploadBtn_Click" runat="server" id="Submit1"
    name="Submit1">
    </form>
    </body>
    </HTML>
      

  3.   

    uploadfile.aspx.vb源碼如下
    Imports System.IO
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Byte
    Public Class WebForm1
        Inherits System.Web.UI.Page#Region " Web Form Designer Generated Code "    'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()    End Sub
        Protected WithEvents Message As System.Web.UI.WebControls.Label
        Protected WithEvents MyFileName As System.Web.UI.HtmlControls.HtmlInputText
        Protected WithEvents MyFile As System.Web.UI.HtmlControls.HtmlInputFile
        Protected WithEvents Submit1 As System.Web.UI.HtmlControls.HtmlInputButton    'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub#End Region    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
        End Sub    Sub UploadBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim fileDataStream As Stream = MyFile.PostedFile.InputStream
            Dim fileLength As Int32 = MyFile.PostedFile.ContentLength
            Dim fileData(fileLength) As Byte
            fileDataStream.Read(fileData, 0, fileLength)
            Dim fileTitle As String = MyFileName.Value
            Dim fileType As String = MyFile.PostedFile.ContentType
            Dim connection As New SqlConnection("Server=192.168.203.176;uid=kye;pwd=kye;database=OA")
            Dim command As New SqlCommand("INSERT INTO TestFiles (MyFileName,MyFile,FileType)" & _
                    "VALUES (@MyFileName,@MyFile,@FileType)", connection)
            Dim paramTitle As New SqlParameter("@MyFileName", SqlDbType.VarChar, 35)
            paramTitle.Value = fileTitle
            command.Parameters.Add(paramTitle)        Dim paramData As New SqlParameter("@MyFile", SqlDbType.Image)
            paramData.Value = fileData
            command.Parameters.Add(paramData)        Dim paramType As New SqlParameter("@FileType", SqlDbType.VarChar, 50)
            paramType.Value = fileType
            command.Parameters.Add(paramType)        connection.Open()
            command.ExecuteNonQuery()
            connection.Close()        Message.Text = "你的文件已經成功上載!"
            MyFileName.Value = ""
        End Sub
    End Class
      

  4.   

    數據庫結構如下
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestFiles]')
     and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[TestFiles]
    GOCREATE TABLE [dbo].[TestFiles] (
    [id] [int] IDENTITY (1, 1) NOT NULL ,
    [MyFileName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
    [FileType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
    [MyFile] [image] NOT NULL 
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO
    以上摘至孟憲會之精彩世界
      

  5.   

    Select * From MyImages
    取所有数据到dataset 会不会造成占用内存太多??