wp8本地数据库问题

时间:2014-04-08 10:38:50

标签: c# database windows-phone-8

我在windows phone 8 app中创建了本地数据库。我有4个字段。

userID - int
Username - string 
FileName - string 
FileByte - byte[]

我正在尝试更新FileByte列。但是当我更新列时,我得到异常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)
        { 

        }
    }

我不知道问题出在哪里。有人可以帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

看看SQL Server does not handle comparison of NText, Text, Xml, or Image data types。它建议您将FileByte列的类型更改为VARBINARY(MAX)。

相关问题