在sql数据库中保存图像如何?

时间:2016-05-30 12:01:52

标签: c# sql-server

C#代码:

   SqlCommand NewUser = new SqlCommand("INSERT INTO [User] Values (@username,@password,@name,@lastname,@location,@profesion,@email,@gender,@money,@pro,@xp,@lv,@m1,@m2,@m3,@m4,@m5,@d1,@d2,@d3,@d4,@d5,@im);", c);
        NewUser.Connection = c;
        NewUser.Parameters.AddWithValue("@username", txtuser.Text);
        NewUser.Parameters.AddWithValue("@password", txtpass.Text);
        NewUser.Parameters.AddWithValue("@name", txtFName.Text);
        NewUser.Parameters.AddWithValue("@lastname", txtLName.Text);
        NewUser.Parameters.AddWithValue("@location", ddlcountry.SelectedItem.Text);
        NewUser.Parameters.AddWithValue("@profesion", txtprofession.Text);
        NewUser.Parameters.AddWithValue("@email", txtemail.Text);
        NewUser.Parameters.AddWithValue("@gender", rbgendere.SelectedItem.Text);
        NewUser.Parameters.AddWithValue("@money", 0);
        NewUser.Parameters.AddWithValue("@pro", DBNull.Value);
        NewUser.Parameters.AddWithValue("@xp", 0);
        NewUser.Parameters.AddWithValue("@lv", 1);
        NewUser.Parameters.AddWithValue("@m1", 0);
        NewUser.Parameters.AddWithValue("@m2", 0);
        NewUser.Parameters.AddWithValue("@m3", 0);
        NewUser.Parameters.AddWithValue("@m4", 0);
        NewUser.Parameters.AddWithValue("@m5", 0);
        NewUser.Parameters.AddWithValue("@d1", 0);
        NewUser.Parameters.AddWithValue("@d2", 0);
        NewUser.Parameters.AddWithValue("@d3", 0);
        NewUser.Parameters.AddWithValue("@d4", 0);
        NewUser.Parameters.AddWithValue("@d5", 0);
        NewUser.Parameters.AddWithValue("@im", );
        Session["CurentUserid"] = txtuser.Text;
        c.Open();
        NewUser.ExecuteNonQuery();
        c.Close();
        Session["Conect"] = (bool)true;
        Response.Redirect("Finish Had Member.aspx", true);

表格代码:

   CREATE TABLE [dbo].[User] (
    [Username]  VARCHAR (100)   NOT NULL,
    [Pasword]   VARCHAR (100)   NOT NULL,
    [FName]     VARCHAR (MAX)   NOT NULL,
    [LName]     VARCHAR (MAX)   NOT NULL,
    [Location]  VARCHAR (MAX)   NOT NULL,
    [Profesion] VARCHAR (MAX)   NOT NULL,
    [email]     VARCHAR (MAX)   NOT NULL,
    [gender]    VARCHAR (MAX)   NOT NULL,
    [money]     INT             NOT NULL,
    [property]  VARCHAR (MAX)   NULL,
    [xp]        INT             NOT NULL,
    [level]     INT             NOT NULL,
    [mission1]  INT             NOT NULL,
    [mission2]  INT             NOT NULL,
    [mission3]  INT             NOT NULL,
    [mission4]  INT             NOT NULL,
    [mission5]  INT             NOT NULL,
    [did1]      INT             NOT NULL,
    [did2]      INT             NOT NULL,
    [did3]      INT             NOT NULL,
    [did4]      INT             NOT NULL,
    [did5]      INT             NOT NULL,
[image]     VARBINARY (MAX) NULL,
    PRIMARY KEY CLUSTERED ([Username] ASC)
);

我想将c#页面中的图像保存到数据库中,这是html:

<input runat="server" id="pho" type="file" name="file[]" multiple="" accept="image/*" />

我该怎么办? 我添加了所有插入代码plss loke如何保存图像?

我更新代码有完整的表

2 个答案:

答案 0 :(得分:0)

以下是将图像存储到sql server的示例代码:

SqlConnection conn = new SqlConnection(connectionString);

try
{
    int imageLength = uploadInput.PostedFile.ContentLength;
    byte[] picbyte = new byte[imageLength];
    uploadInput.PostedFile.InputStream.Read (picbyte, 0, imageLength);

    SqlCommand command = new SqlCommand("INSERT INTO ImageTable (ImageFile) VALUES (@Image)", conn);
    command.Parameters.Add("@Image", SqlDbType.Image);
    command.Parameters[0].Value = picbyte;

    conn.Open();
    command.ExecuteNonQuery();
    conn.Close();
}
finally
{
    if (conn.State != ConnectionState.Closed)
    {
        conn.Close();
    }
}

注意: uploadInput是一个文件输入控件,用于将图像文件上传到服务器。代码取自ASP.NET应用程序。

以下是图像类型列的插入脚本:

INSERT INTO ImageTable (ImageColumn)

SELECT ImageColumn FROM 
OPENROWSET(BULK N'C:\SampleImage.jpg', SINGLE_BLOB) 
AS ImageSource(ImageColumn);

编辑:要了解有关此主题的更多信息,请参阅here

答案 1 :(得分:0)

Try this..

     Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);

SqlCommand NewUser = new SqlCommand("INSERT INTO [User] Values (@username,@password,@name,@lastname,@location,@profesion,@email,@gender,@money,@pro,@xp,@lv,@m1,@m2,@m3,@m4,@m5,@d1,@d2,@d3,@d4,@d5,@im);", c);
        NewUser.Connection = c;
        NewUser.Parameters.AddWithValue("@username", txtuser.Text);
        NewUser.Parameters.AddWithValue("@password", txtpass.Text);
        NewUser.Parameters.AddWithValue("@name", txtFName.Text);
        NewUser.Parameters.AddWithValue("@lastname", txtLName.Text);
        NewUser.Parameters.AddWithValue("@location", ddlcountry.SelectedItem.Text);
        NewUser.Parameters.AddWithValue("@profesion", txtprofession.Text);
        NewUser.Parameters.AddWithValue("@email", txtemail.Text);
        NewUser.Parameters.AddWithValue("@gender", rbgendere.SelectedItem.Text);
        NewUser.Parameters.AddWithValue("@money", 0);
        NewUser.Parameters.AddWithValue("@pro", DBNull.Value);
        NewUser.Parameters.AddWithValue("@xp", 0);
        NewUser.Parameters.AddWithValue("@lv", 1);
        NewUser.Parameters.AddWithValue("@m1", 0);
        NewUser.Parameters.AddWithValue("@m2", 0);
        NewUser.Parameters.AddWithValue("@m3", 0);
        NewUser.Parameters.AddWithValue("@m4", 0);
        NewUser.Parameters.AddWithValue("@m5", 0);
        NewUser.Parameters.AddWithValue("@d1", 0);
        NewUser.Parameters.AddWithValue("@d2", 0);
        NewUser.Parameters.AddWithValue("@d3", 0);
        NewUser.Parameters.AddWithValue("@d4", 0);
        NewUser.Parameters.AddWithValue("@d5", 0);
        NewUser.Parameters.AddWithValue("@im", bytes);
        Session["CurentUserid"] = txtuser.Text;
        c.Open();
        NewUser.ExecuteNonQuery();
        c.Close();
        Session["Conect"] = (bool)true;
        Response.Redirect("Finish Had Member.aspx", true);

**

  编辑:好的,我在我的系统上试过了。下面的代码插入图像   二进制格式成功。

**

SQL表

CREATE TABLE [dbo].[Image_table](   

    [Id] [int] IDENTITY(1,1) NOT NULL,
    [image]     VARBINARY (MAX) NULL
    )



protected void Button1_Click(object sender, EventArgs e)
    {
        Stream fs = FileUpload.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);

         string consString = ConfigurationManager.ConnectionStrings["connectionKey"].ConnectionString;
         SqlConnection con = new SqlConnection(consString);

            try
            { 
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "insert into [dbo].[Image_table]  values (@img)";
                cmd.Parameters.Add("@img", SqlDbType.Image).Value = bytes;
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

            }
            catch (Exception ex)
            {
                throw ex;
            }

    }