xml文件到mysql数据库

时间:2017-04-07 09:46:46

标签: c# mysql xml

我正在将3D对象转换为一个xml文件,将其写入硬盘驱动器,然后从硬盘驱动器读取它并作为blob上传到Mysql数据库。然后我从我的硬盘驱动器中删除创建的xml.file。是否有可能这样做(创建xml文件/上传到数据库),以便我不必首先将其写入驱动器,然后读取它,然后删除它?如果我能以某种方式创建xml文件并直接传递它而无需将其保存在驱动器上。有什么想法吗?

这里我将xml文件写入drive(EDITED):

 public Stream WriteXml(List<object> gridEntities, string fileName)
        {
            XDocument doc = new XDocument();
            XElement root = new XElement("ViewportLayout");
            XElement xEntities = new XElement("Entities");

            xEntities.Add(...);
            root.Add(xEntities);
            doc.Add(root);
            //var path = string.Format("C:\\Users\\NP\\Desktop\\Saves\\{0}", fileName);
            //doc.Save(path);

            Stream stream = new MemoryStream();  // Create a stream
            doc.Save(stream);      // Save XDocument into the stream
            stream.Position = 0;   // Rewind the stream ready to read from it elsewhere
            return stream;
        }

我将它上传到数据库:

  public static void SaveGridXmlFileToDataBase(Stream stream, string projectName, string filePath, string gridName, string gridGuid)
    {
        if (OpenConnection() == true)
        {
            byte[] file;

            using (stream)
            {
                using (var reader = new BinaryReader(stream))
                {
                    file = reader.ReadBytes((int)stream.Length);
                }
            }
            string project = string.Concat(projectName, "_grids");

            string query = String.Format("INSERT INTO {0} SET name=@name, guid=@guid, xml=@File;", project);
            using (var sqlWrite = new MySqlCommand(query, connection))
            {
                sqlWrite.Parameters.Add("@name", MySqlDbType.VarChar).Value = gridName;
                sqlWrite.Parameters.Add("@guid", MySqlDbType.VarChar).Value = gridGuid;
                sqlWrite.Parameters.Add("@File", MySqlDbType.LongBlob).Value = file;
                sqlWrite.ExecuteNonQuery();
            }

            CloseConnection();
        }
    }

0 个答案:

没有答案