使用asp.net中的存储过程上传文件? [英] upload file using stored procedure in asp.net?

查看:82
本文介绍了使用asp.net中的存储过程上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



使用存储过程上传文件

设计:

Dear All,

upload file using stored procedure
design:

<form id="form1"  runat="server">
    <div>
    
        <table class="style1">
            <tr>
                <td>
                    Name</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server">
                </td>
            </tr>
            <tr>
                <td>
                    Upload File</td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                </td>
            </tr>
        </table>
    
    </div>
    </form>

code:




protected void Button1_Click(object sender, EventArgs e)
    {
        int userId = 0;

        string constr = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;

        using(SqlConnection con=new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SP_Uploadfile"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    string strdata = @"~\data\" + FileUpload1.FileName;
                    FileUpload1.PostedFile.SaveAs(Server.MapPath(strdata));

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@Uploaddata", FileUpload1.FileBytes);

                    cmd.Connection = con;
                    con.Open();
                    userId = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();

                }
            }
            string message = string.Empty;
            switch (userId)
            {
                default: message = "upload successfully";
                    break;
            }
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
        }
    }





此处上传文件中此代码数据未插入任何错误请回复我和任何示例请



添加存储过程



here uploading file in this code data not inserted any mistakes please reply me and any examples please

Stored Procedure added

USE [Place]
GO
 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
Create PROCEDURE [dbo].[SP_Uploadfile]
@Name VARCHAR(50),
@Uploaddata varchar(max)	

AS
BEGIN
INSERT INTO [Uploadfile]
([Name],
[Uploaddata])

VALUES
(@Name,
@Uploaddata)


SELECT SCOPE_IDENTITY() -- Id	

END

推荐答案



  1. 首先,用于存储文件字节的列 DataType 应为 varbinary( max)
  2. 接下来,在插入数据时,您应该使用以下代码来获取文件字节


  1. First of all, the column DataType for storing the File Bytes should be varbinary(max).
  2. Next, while inserting data, you should use the below code to get the File Bytes.
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);





参考 - 使用FileUpload控件将文件保存到SQL Server数据库 [ ^ ]获取完整示例。


Refer - Save Files to SQL Server Database using FileUpload Control[^] for a complete example.


这篇关于使用asp.net中的存储过程上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆