我的sqlCe数据库中有一个名为“dic”的表,它是一个字典数据库。它有两列名为english,bangla。 我有一个文本框和一个搜索按钮。通过使用此按钮和文本框,我可以搜索单词,匹配英文列和输入文本。现在我想显示与英文列匹配的TOP 5结果并将文本输入到列表框中。在列表框中,第一行将显示“english”列的数据,第二行将包含搜索结果中“bangla”列的数据。因此列表框将包含总计5 * 2 = 10行。 我尝试了一些非常初始但却出错:
condrokotha_newContext db = null;
// Constructor
public MainPage()
{
InitializeComponent();
db = new condrokotha_newContext(condrokotha_newContext.ConnectionString);
//db.Dispose();
db.CreateIfNotExists();
db.LogDebug = true;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (db!=null)
{
var contacts = (from m in db.Dics where m.English.Contains(@"abandon") select m.English);
List<string> listBoxItems = new List<string>();
string s;
try{
foreach (var a in contacts)
{
s = a;
listBoxItems.Add(s);
}
}
catch (Exception ex){ MessageBox.Show(ex.ToString()); }
listBox1.ItemsSource = listBoxItems;
}
}
它有一个例外“Unsoecified error”。
我怎样才能做我想要的事情????
N:B:我在下面给出了使用数据库的代码。
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18010
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.IO;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Data.Linq.Mapping;
using Microsoft.Phone.Data.Linq;
public class DebugWriter : TextWriter
{
private const int DefaultBufferSize = 256;
private System.Text.StringBuilder _buffer;
public DebugWriter()
{
BufferSize = 256;
_buffer = new System.Text.StringBuilder(BufferSize);
}
public int BufferSize
{
get;
private set;
}
public override System.Text.Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
#region StreamWriter Overrides
public override void Write(char value)
{
_buffer.Append(value);
if (_buffer.Length >= BufferSize)
Flush();
}
public override void WriteLine(string value)
{
Flush();
using(var reader = new StringReader(value))
{
string line;
while( null != (line = reader.ReadLine()))
System.Diagnostics.Debug.WriteLine(line);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
Flush();
}
public override void Flush()
{
if (_buffer.Length > 0)
{
System.Diagnostics.Debug.WriteLine(_buffer);
_buffer.Clear();
}
}
#endregion
}
public partial class condrokotha_newContext : System.Data.Linq.DataContext
{
public bool CreateIfNotExists()
{
bool created = false;
using (var db = new condrokotha_newContext(condrokotha_newContext.ConnectionString))
{
if (!db.DatabaseExists())
{
string[] names = this.GetType().Assembly.GetManifestResourceNames();
string name = names.Where(n => n.EndsWith(FileName)).FirstOrDefault();
if (name != null)
{
using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
{
if (resourceStream != null)
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(FileName, FileMode.Create, myIsolatedStorage))
{
using (BinaryWriter writer = new BinaryWriter(fileStream))
{
long length = resourceStream.Length;
byte[] buffer = new byte[32];
int readCount = 0;
using (BinaryReader reader = new BinaryReader(resourceStream))
{
// read file in chunks in order to reduce memory consumption and increase performance
while (readCount < length)
{
int actual = reader.Read(buffer, 0, buffer.Length);
readCount += actual;
writer.Write(buffer, 0, actual);
}
}
}
}
}
created = true;
}
else
{
db.CreateDatabase();
created = true;
}
}
}
else
{
db.CreateDatabase();
created = true;
}
}
}
return created;
}
public bool LogDebug
{
set
{
if (value)
{
this.Log = new DebugWriter();
}
}
}
public static string ConnectionString = "Data Source=isostore:/condrokotha_new.sdf";
public static string ConnectionStringReadOnly = "Data Source=appdata:/condrokotha_new.sdf;File Mode=Read Only;";
public static string FileName = "condrokotha_new.sdf";
public condrokotha_newContext(string connectionString) : base(connectionString)
{
OnCreated();
}
#region Extensibility Method Definitions
partial void OnCreated();
#endregion
public System.Data.Linq.Table<Dic> Dics
{
get
{
return this.GetTable<Dic>();
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dic")]
public partial class Dic
{
private string _English;
private string _Bangla;
public Dic()
{
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="english", Storage="_English", DbType="NVarChar(1000)")]
public string English
{
get
{
return this._English;
}
set
{
if ((this._English != value))
{
this._English = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="bangla", Storage="_Bangla", DbType="NText", UpdateCheck=UpdateCheck.Never)]
public string Bangla
{
get
{
return this._Bangla;
}
set
{
if ((this._Bangla != value))
{
this._Bangla = value;
}
}
}
}
#pragma warning restore 1591