windows phone上怎么使用SQLite数据库
private void button1_Click(object sender, RoutedEventArgs e)
{
if (Conn ==null)
{
Conn = new SQLiteConnection(FileName);
Conn.Open();
this.textBlock1.Text = "open";
button1.IsEnabled = false;
this.button2.IsEnabled = true;
button3.IsEnabled = false ;
//创建表
SQLiteCommand cmd = Conn.CreateCommand("Create table UserInfo (id int primary key,name text,pwd text)");
//执行
int i = cmd.ExecuteNonQuery();
}
}
//插入数据
private void button2_Click(object sender, RoutedEventArgs e)
{
//Create table UserInfo (id int primary key,name text,zipcode numeric(7))
if (Conn!=null)
{
//插入数据
UserInfo user = new UserInfo(1, "lk", "kl");
SQLiteCommand cmd= Conn.CreateCommand(" Insert into UserInfo (id, name, pwd) values (@id,@name,@pwd)");
// cmd.CommandText = " Insert into UserInfo (id, name, pwd) values (@id,@name,@pwd)";
int ii = cmd.ExecuteNonQuery(user);
if (ii > 0)
{
textBlock1.Text = "succe";
}
else
{
textBlock1.Text = "error";
}
}
#region MyRegion
//try
//{
// int id = 1;
// string name ="name";
// string pwd ="pwd";//" + id +",\"" + name + "\"," + zipcode +"
// SQLiteCommand cmd = Conn.CreateCommand("insert into UserInfo values (" + id + ",\"" + name + "\"," + pwd + ")");
// int num1 = cmd.ExecuteNonQuery();
// if (num1 > 0)
// {
// MessageBox.Show("succes");
// }
// else
// {
// MessageBox.Show("error");
// }
//}
//catch (SQLiteException Ex)
//{
// textBlock1.Text = Ex.Message;
//}
////for (int i = 0; i < 10; i++)
////{
//// id++;
//// name = name + id;
//// pwd = pwd + id;
//// cmd= Conn.CreateCommand("insert into UserInfo values("+id+","+name+","+pwd+")");
//// num= cmd.ExecuteNonQuery();
////}
#endregion
this.button2.IsEnabled = false;
this.button3.IsEnabled = true;
}
//查询数据
private void button3_Click(object sender, RoutedEventArgs e)
{
if (Conn !=null)
{
SQLiteCommand cmd = Conn.CreateCommand("SELECT * FROM UserInfo");
//List<UserInfo> userInfo = new List<UserInfo>();
//userInfo = cmd.ExecuteQuery<UserInfo>();
//var lis = cmd.ExecuteQuery<UserInfo>();
//List <UserInfo >ltt= lis.ToList<UserInfo>();
//ltt.Add(user);
var li = cmd.ExecuteQuery<UserInfo>();
this.listBox1.ItemsSource = li.ToList<UserInfo>();
}
}
}
}
为存储数据
public class UserInfo
{
public UserInfo()
{
}
public UserInfo(int _id, string _name, string _pwd)
{
this.id1 = _id;
this.name1 = _name;
this.pwd1 = _pwd;
}
int id1;
string name1;
public string name
{
get { return name1; }
set { name1 = value; }
}
string pwd1;
public string pwd
{
get { return pwd1; }
set
{
pwd1 = value;
}
}
public int id
{
get { return id1; }
set { id1 = value; }
}
}
前台UI:
<Button Content="btnCon" Height="72" HorizontalAlignment="Left" Margin="8,18,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
<Button Content="btnSE" Height="72" HorizontalAlignment="Left" Margin="271,18,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
<Button Content="btnCle" Height="72" HorizontalAlignment="Left" Margin="8,119,0,0" Name="button3" VerticalAlignment="Top" Width="160" Click="button3_Click" />
<TextBlock Height="26" HorizontalAlignment="Left" Margin="271,140,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="109" />
<ListBox Height="291" HorizontalAlignment="Left" Margin="24,283,0,0" Name="listBox1" VerticalAlignment="Top" Width="407">
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Vertical" >
<TextBlock Text="地址"></TextBlock>
<TextBlock Text="{Binding id}"></TextBlock>
<TextBlock Text="名字"></TextBlock>
<TextBlock Text="{Binding name}"></TextBlock>
<TextBlock Text="密码"></TextBlock>
<TextBlock Text="{Binding pwd}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

浙公网安备 33010602011771号