我正在为Windows手机创建一个应用程序,我需要为我使用SQLite创建一个数据库连接。 在此链接中,我执行了查询:http://www.codeguru.com/csharp/.net/wp7/using-sqlite-in-your-windows-phone-application.htm
这是代码:
private void btncreate_Click(object sender, RoutedEventArgs e)
{
if (MySqlLiteDB == null)
{
MySqlLiteDB = new SQLiteConnection("MyTestDB");
MySqlLiteDB.Open();
MessageBox.Show("Connection opened Successfully!!!");
}
}
private void btnpopulate_Click(object sender, RoutedEventArgs e)
{
SQLiteCommand cmd=MySqlLiteDB.CreateCommand("Create table student(id int primary key,name text,zipcode numeric(7))");
int i = cmd.ExecuteNonQuery();
int id = 0;
for (int j = 0; j < 20; j++)
{
id++;
string name = "Name" + id;
int zipcode = 98000 + id;
cmd.CommandText = "Insert into student(id,name,zipcode) values(" + id + ",\"" + name + "\"," + zipcode + ")";
i = cmd.ExecuteNonQuery();
}
MessageBox.Show("Insert successful");
}
private void btnclear_Click(object sender, RoutedEventArgs e)
{
SQLiteCommand cmd = MySqlLiteDB.CreateCommand("drop table student");
int i = cmd.ExecuteNonQuery();
MessageBox.Show("Data Cleared successfully");
}
private void btnclose_Click(object sender, RoutedEventArgs e)
{
if (MySqlLiteDB != null)
{
MySqlLiteDB.Dispose();
MySqlLiteDB = null;
MessageBox.Show("Connection closed");
}
}
但现在我需要从数据库表中检索数据并使用select语句将其绑定到网格我该怎么做? 我想在网格中显示表数据是否有任何方法将数据绑定到网格。
答案 0 :(得分:1)
在代码中添加以下函数和类
public class student
{
public int id {get;set;}
public string name {get;set;}
public long zipcode {get;set;}
}
SelectFromStudent()
{
string query="select id,name,zipcode from student";
List<student> studentList = SelectFromTable<student>(query);
}
List<T> SelectFromTable<T>(String statement) where T : new()
{
SQLiteCommand cmd = MySqlLiteDB.CreateCommand(statement);
var lst = cmd.ExecuteQuery<T>();
return lst.ToList<T>();
}
您无法在Windows Phone应用程序中使用网格绑定数据。还有另一个控件来做ListBox这样的操作。你可以编辑ListBox的Itemtemplate。 ListBox有itemsource属性。