【发布时间】:2015-12-04 06:44:04
【问题描述】:
我正在尝试使用 asp.net (c#) 创建一个应用程序,其中用户将使用文件上传控件上传单个文件(任何数据格式 doc、docx、pdf)。
然后我必须将文件的内容保存在用户选择的数据库中。数据库的结构是:
Field DataType
id int
title nvarchar(MAX)
body varbinary(MAX)
bodyoverview nvarchar(MAX)
postedon date
文件的内容将保存在body属性中。我也有以下代码:
protected async void Button_Upload_Click(object sender, EventArgs e)
{
StringBuilder stringBuilder = new StringBuilder();
if(FileUpload1.HasFile)
{
try
{
string FileContent = String.Empty;
using(StreamReader inputStreamReader=new StreamReader(FileUpload1.PostedFile.InputStream))
{
FileContent = await inputStreamReader.ReadToEndAsync();
foreach (char c in FileContent.ToCharArray())
{
stringBuilder.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
}
}
SqlConnection sqlConnection = new SqlConnection("Data Source=(localdb)\\ProjectsV12;Initial Catalog=Blog;Integrated Security=True");
string query = "insert into blogthumbnails values(@blogtitle,@body,@bodyoverview,@allowcomments,@commentscount,@postedon)";
SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
sqlCommand.Parameters.AddWithValue("@blogtitle", TextBox_Title_Of_The_Blog.Text);
sqlCommand.Parameters.AddWithValue("@body", stringBuilder);
sqlCommand.Parameters.AddWithValue("@postedon", DateTime.Now.Date);
await sqlConnection.OpenAsync();
try
{
int i = sqlCommand.ExecuteNonQuery();
if(i>0)
{
Label_File_Upload_Status.Text = "File Uploaded";
}
else
{
Label_File_Upload_Status.Text = "Please try again";
}
}
catch(SqlException sqlException)
{
Label_File_Upload_Status.Text = sqlException.Message.ToString();
}
}
catch(Exception FileUploadException)
{
Label_File_Upload_Status.Text = "Error in uploading file. Please try again. Error: "+FileUploadException.Message;
}
}
else
{
Label_File_Upload_Status.Text = "Please select a file to upload";
}
}
但我得到了错误:
不存在从对象类型
System.Text.StringBuilder到已知托管提供程序本机类型的映射。
【问题讨论】:
标签: c# sql asp.net sql-server