如何使用xamarin android web服务在数据库sql server中保存视频并在listview中显示

时间:2017-06-03 07:34:11

标签: c# sql sql-server web-services xamarin.android

如何使用xamarin android web服务在数据库sql server中保存视频并在listview中显示

1 个答案:

答案 0 :(得分:2)

由于以下原因,我强烈建议不要在您的数据库中存储视频:

1). Doing so would bloat your database size
2). Slow down the performance of the database
3). It opens your application to Denial of Service attacks (DDOS).

您应该期待将视频存储在文件专用存储空间中,例如:

1). Cloud Storage.
2). FTP storage.
3). Server's file system.
4). Etc...

如果您确实希望在数据库中存储视频以进行测试或基准测试,可以尝试以下代码:

 using System;
 using System.Data;
 using System.Data.SqlClient;

 namespace Portal.WebApp.Models
 {
     public class DbBlobSaver
     {
         #region Public Methods
         public void Execute(byte[] fileBytes)
         {
             using (var dbConnection = new SqlConnection("Data Source = .; Initial Catalog = BlobsDatabase; Integrated Security = SSPI"))
             {
                 var command = BuildCommand(dbConnection);
                 var blobParameter = BuildParameter(fileBytes);

                 command.Parameters.Add(blobParameter);

                 try
                 {
                     command.Connection.Open();
                     command.ExecuteNonQuery();
                 }
                 catch (Exception ex)
                 {
                     // Log errors here
                 }
                 finally
                 {
                     command.Connection.Close();
                 }
             }
         }
         #endregion

         #region Private Methods
         private SqlParameter BuildParameter(byte[] fileBytes)
         {
             var blobParameter = new SqlParameter("@BlobFile", SqlDbType.Binary);

             blobParameter.Value = fileBytes;

             return blobParameter;
         }

         private SqlCommand BuildCommand(SqlConnection connection)
         {
             var command = new SqlCommand();
             command.Connection = connection;
             command.CommandType = CommandType.Text;
             command.CommandText = "INSERT INTO BlobsTable(BlobFile)" + "VALUES (@BlobFile)";

             return command;
         }
         #endregion
     }
 }