Je suis en train d'enregistrer un fichier dans SQL Server DB. (Il ne murmure pas quel type de fichier) J'utilise pour le champ fileContent avec le type d'image qui autorise NULL. Lorsque j'ai exécuté Command.ExecuteNonQuery(), un message d'erreur s'affiche: "Les données chaîne ou binaires sont tronquées. \ R \ nL'instruction a été interrompue." Ci-dessous vous pouvez voir mon code:Problème avec le fichier de stockage dans SQL Server
CREATE TABLE [NewsContent]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [nvarchar](15) NOT NULL,
[Extension] [nvarchar](5) NOT NULL,
[Content] [image] NULL
)
protected void btnUploadFile_Click(object sender, EventArgs e)
{
if (fileUpload.HasFile)
{
try
{
Int32 fileLength = fileUpload.PostedFile.ContentLength;
String fileType = fileUpload.PostedFile.ContentType;
Stream fileStream = fileUpload.PostedFile.InputStream;
String fileName = fileUpload.PostedFile.FileName;
byte[] fileContent = new byte[fileLength];
fileStream.Read(fileContent, 0, fileLength);
int status = Utils.DBWorker.UploadFile(fileName, fileType, fileContent);
if (status == -1)
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured";
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
public static int UploadFile(String fileName, String fileType, byte[] fileContent)
{
try
{
string insertSql = "INSERT INTO [NewsContent] (FileName, Extension, Content) VALUES (@FileName, @Extension, @Content)";
Command.CommandText = insertSql;
Command.Parameters.Clear();
Command.Parameters.Add(new SqlParameter("@FileName", SqlDbType.NVarChar, 100));
Command.Parameters.Add(new SqlParameter("@Extension", SqlDbType.NVarChar, 50));
Command.Parameters.Add(new SqlParameter("@Content", SqlDbType.VarBinary));
Command.Parameters["@FileName"].Value = fileName;
Command.Parameters["@Extension"].Value = fileType;
Command.Parameters["@Content"].Value = fileContent;
return Command.ExecuteNonQuery();
}
catch (Exception es)
{
return -1;
}
}
Quelqu'un pourrait-il me aider?
Eh bien, cela ressemble à une limite de colonne. Veuillez publier votre SQL CREATE TABLE afin que nous puissions voir comment les colonnes sont configurées. –