C#mysql使用Byte []将文件插入数据库

时间:2012-12-18 19:04:09

标签: c# mysql

我正在AsyncUpload集合中执行一个简单的foreach循环到Byte []并尝试将其插入到数据库中。

 foreach (UploadedFile file in AsyncUpload1.UploadedFiles)
        {

            string[] splitFileExt = file.FileName.Split('.');
            List<string> CommandArgs = new List<string>();
            CommandArgs.Add("IID|" + Request.QueryString["InstructorID"]);
            CommandArgs.Add("FName|" + file.FileName);
            CommandArgs.Add("FTitle|" + file.FileName);
            CommandArgs.Add("FType|" + file.ContentType);
            CommandArgs.Add("FExtension|" + splitFileExt[1]);
            CommandArgs.Add("FSize|" + file.ContentLength.ToString());
            byte[] b = StaticControlCreationClass.ReadToEnd(file.InputStream);
            MySQLProcessing.MySQLProcessor.MySQL_UploadFile(b, "fileupload", CommandArgs, mysqlCon);

        }



public static void MySQL_UploadFile(byte[] value, string MYSQLCommand, List<string> CommandArgs, MySqlConnection con)
    {
        MySqlCommand cmd = new MySqlCommand(MYSQLCommand, con);
        cmd.Parameters.AddWithValue("FContent", value);
        foreach (string args in CommandArgs)
        {
            string[] splitArgs = args.Split('|');
            cmd.Parameters.AddWithValue(splitArgs[0], splitArgs[1].Trim());
        }
        cmd.ExecuteNonQuery();

    }

然后这是存储过程

DROP PROCEDURE IF EXISTS fileupload;

CREATE PROCEDURE fileupload`(
   IID           int,
   TValue        varchar(250),
   FName         varchar(250),
   FType         varchar(15),
   FSize         varchar(45),
   FContent      longblob,
   FExtension    varchar(10))
   BEGIN
      SET @IID = IID;
      SET @TValue = TValue;
      SET @FName = FName;
      SET @FType = FType;
      SET @FSize = FSize;
      SET @FExtension = FExtension;
      SET @FContent = FContent;

      INSERT INTO tblfiles(rID,
                                     Title,
                                     File_Name,
                                     File_Type,
                                     File_Size,
                                     File_Content,
                                     File_Extension)
      VALUES (@IID,
              @TValue,
              @FName,
              @FType,
              @FSize,
              @FContent,
              @FExtension);
   END

我不断获得You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fileupload' at line 1 @ cmd.ExecuteNonQuery();

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我错过了分类器,说它是一个存储过程

cmd.CommandType = CommandType.StoredProcedure;