中显示
如何使用Xamarin Android Web服务在数据库SQL Server中保存视频,并在ListView
由于以下原因,我强烈建议不要将视频存储在数据库中:
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
}
}