WP8 本地数据库问题
本文关键字:问题 数据库 WP8 | 更新日期: 2023-09-27 17:57:14
我在Windows Phone 8应用程序中创建了本地数据库。我有 4 个字段。
userID - int
Username - string
FileName - string
FileByte - byte[]
我正在做的是尝试更新文件字节列。但是当我更新列时,我收到异常SQL Server does not handle comparison of NText, Text, Xml, or Image data types.
这是我的DataTable
[Table]
public class UserFilesDetailsTable : INotifyPropertyChanged, INotifyPropertyChanging
{
// Define ID: private field, public property, and database column.
private int _userID;
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int userID
{
get { return _userID; }
set
{
if (_userID != value)
{
NotifyPropertyChanging("userID");
_userID = value;
NotifyPropertyChanged("userID");
}
}
}
// Define item name: private field, public property, and database column.
private string _Username;
[Column(DbType = "NVarChar(100) NOT NULL", CanBeNull = false)]
public string Username
{
get { return _Username; }
set
{
if (_Username != value)
{
NotifyPropertyChanging("Username");
_Username = value;
NotifyPropertyChanged("Username");
}
}
}
// Define item name: private field, public property, and database column.
private string _Filename;
[Column(DbType = "NVarChar(100) NOT NULL", CanBeNull = false)]
public string Filename
{
get { return _Filename; }
set
{
if (_Filename != value)
{
NotifyPropertyChanging("Filename");
_Filename = value;
NotifyPropertyChanged("Filename");
}
}
}
// Define item name: private field, public property, and database column.
private byte[] _Filebytes;
[Column(DbType = "image")]
public byte[] Filebytes
{
get { return _Filebytes; }
set
{
if (_Filebytes != value)
{
NotifyPropertyChanging("Filebytes");
_Filebytes = value;
NotifyPropertyChanged("Filebytes");
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
// Used to notify that a property changed
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region INotifyPropertyChanging Members
public event PropertyChangingEventHandler PropertyChanging;
// Used to notify that a property is about to change
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
#endregion
}
这是我的更新查询
public void addFiles(int userID, string userName, string fileName, byte[] fileBytes)
{
try
{
if (!(databaseTablesDB.usersFileDetailsTable.Where(f => f.Filename == fileName).Any()))
{
databaseTablesDB.usersFileDetailsTable.InsertOnSubmit(new UserFilesDetailsTable { userID = userID, Username = userName, Filename = fileName, Filebytes = fileBytes });
// Save changes to the database.
databaseTablesDB.SubmitChanges();
}
else
{
var fileDetails = (from file in databaseTablesDB.usersFileDetailsTable where file.Filename == fileName && file.Username == userName select file).FirstOrDefault();
if (fileDetails != null)
{
fileDetails.Filebytes = fileBytes;
}
databaseTablesDB.SubmitChanges();
}
}
catch (Exception ex)
{
}
}
我不明白问题出在哪里。有人可以帮忙解决这个问题吗?
看看 SQL Server 不处理 NText、Text、XML 或 Image 数据类型的比较。它建议您将 FileByte 列的类型更改为 VARBINARY(MAX)。