ASP.Net文件上传到SQL Server 2008数据库 [英] ASP.Net file upload to sql server 2008 Database

查看:75
本文介绍了ASP.Net文件上传到SQL Server 2008数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要一个示例应用程序来将文件上传到SQL satabase.
我正在使用asp.net c#.net和sql server.

我在这样的实用程序类中遇到错误.
请谁能帮我.

Hi All,

I need a sample application for File Upload to SQL satabase.
I am using asp.net c#.net and sql server.

I am getting an error in Utilities class like this.
Please can anyone help me.

Server Error in '/' Application.
Incorrect syntax near '@Document'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '@Document'.

Source Error:

Line 68:                 cmd.Parameters["@Size"].Value = size;
Line 69:                 cmd.Parameters["@Document"].Value = fileData;
Line 70:                 cmd.ExecuteNonQuery();
Line 71: 
Line 72:                 connection.Close();


Source File: C:\Users\CBS\documents\visual studio 2010\Projects\Mcases\Mcases\Utilities.cs    Line: 70

Stack Trace:




这是Utility.cs类




Here is utilities.cs class

public static void SaveFile(string name, string contentType, int size, byte[] fileData)
       {
           using (SqlConnection connection = new SqlConnection())
           {
               OpenConnection(connection);
               SqlCommand cmd = new SqlCommand();
               cmd.Connection = connection;
               cmd.CommandTimeout = 0;

               string commandText = "INSERT INTO Document VALUES(@FileName, @ContentType,@Size,@Document)";
               commandText = commandText + "@Document";
               cmd.CommandText = commandText;
               cmd.CommandType = CommandType.Text;

               cmd.Parameters.Add("@FileName", SqlDbType.VarChar);
               cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50);
               cmd.Parameters.Add("@size", SqlDbType.Int);
               cmd.Parameters.Add("@Document", SqlDbType.VarBinary);

               cmd.Parameters["@FileName"].Value = name;
               cmd.Parameters["@ContentType"].Value = contentType;
               cmd.Parameters["@Size"].Value = size;
               cmd.Parameters["@Document"].Value = fileData;
               cmd.ExecuteNonQuery();

               connection.Close();
           }
       }



这是我的按钮单击代码,用于将文件保存在数据库中



And this is my button click code to save file in Database

protected void btnUpload_Click(object sender, EventArgs e)
       {
           HttpFileCollection files = Request.Files;
           foreach (string fileTagName in files)
           {
               HttpPostedFile file = Request.Files[fileTagName];
               if (file.ContentLength > 0)
               {
                   // Due to the limit of the max for a int type, the largest file can be
                   // uploaded is 2147483647, which is very large anyway.
                   int size = file.ContentLength;
                   string name = file.FileName;
                   int position = name.LastIndexOf("\\");
                   name = name.Substring(position + 1);
                   string contentType = file.ContentType;
                   byte[] fileData = new byte[size];
                   file.InputStream.Read(fileData, 0, size);

                   Utilities.SaveFile(name, contentType, size, fileData);
               }
           }
           }

推荐答案

查看您的代码:
Look at your code:
string commandText = "INSERT INTO Document VALUES(@FileName, @ContentType,@Size,@Document)";
commandText = commandText + "@Document";


您最终得到了SQL Command字符串:


You end up with SQL Command string:

INSERT INTO Document VALUES(@FileName, @ContentType,@Size,@Document)@Document

这显然是错误的!
取出这两行中的第二行...

Which is clearly wrong!
Take out the second of these two lines...


尝试:
cmd.Parameters.Add("@Document", SqlDbType.Image);


代替


instead of

cmd.Parameters.Add("@Document", SqlDbType.VarBinary);



详细信息在这里:
MSDN:SqlParameter.SqlDbType属性 [ MSDN:SqlDbType枚举 [



Details here:
MSDN: SqlParameter.SqlDbType Property [^]
MSDN: SqlDbType Enumeration[^]


这篇关于ASP.Net文件上传到SQL Server 2008数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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