问题描述
以前我创建了一个如下的数据模型,并且我已经成功地将数据连接到 listView
到 public class student
的 xamlpages。
那么,如何在不使用public class school
和listView
的情况下,从只输入一次的PrimaryKey
连接数据库sql?
using sqlite;
using System;
namespace SchoolData.Models
{
public class Student
{
[PrimaryKey,AutoIncrement]
public int IdSis { get; set; }
public DateTime Date { get; set; }
public string PicStudent { get; set; }
}
public class School
{
public string SchoolName { get; set; } //without primary key or single entry
public string Address { get; set; }
public string Vision { get; set; }
}
}
XamlPage 代码概览:
<StackLayout>
<Label Text="School Name:"/>
<Entry Text="{Binding SchoolName}"/>
<Label Text="Address:"/>
<Entry Text="{Binding Address}"/>
<Label Text="Vision:"/>
<Entry Text="{Binding Vision}"/>
<Button Text="Save" Clicked="SaveClick"/> //save all entry
<Button Text="Clear" Clicked="ClearClick"/> //clear all entry
</StackLayout>
那么上面的C#write命令是如何创建和更新public class school
连接到XamlPage的。
谢谢。
解决方法
那么上面的C#write命令是如何创建、更新公共类学校连接到XamlPage的。
在这个问题中有很多学生有一个学校名称。所以我只想取学校的名字,学校数据只能填写和更新,
如果你想在sqlite表中插入学校,你可以看看下面的代码:
<StackLayout>
<Label Text="School Name:" />
<Entry x:Name="txtschool" />
<Label Text="Address:" />
<Entry x:Name="txtaddress" />
<Label Text="Vision:" />
<Entry x:Name="txtvision" />
<Button Clicked="SaveClick" Text="Save" />
<Button Clicked="ClearClick" Text="Clear" />
</StackLayout>
public partial class Page1 : ContentPage
{
public SQLiteConnection conn;
public Page1()
{
InitializeComponent();
conn = GetSQLiteConnection();
conn.CreateTable<School>();
}
public SQLiteConnection GetSQLiteConnection()
{
var fileName = "studentdatabase.db";
var documentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentPath,fileName);
var connection = new SQLiteConnection(path);
return connection;
}
private void SaveClick(object sender,EventArgs e)
{
School school = new School();
school.SchoolName = txtschool.Text.ToString();
school.Address = txtaddress.Text.ToString();
school.Vision = txtvision.Text.ToString();
var data = conn.Table<School>();
var d1 = data.Where(x => x.SchoolName == school.SchoolName).FirstOrDefault();
if (d1 == null)
{
conn.Insert(school);
Console.WriteLine("Sucessfully Added");
}
else
{
school.Id = d1.Id;
conn.Update(school);
Console.WriteLine("Already school name Exist,update");
}
txtaddress.Text = "";
txtschool.Text = "";
txtvision.Text = "";
}
private void ClearClick(object sender,EventArgs e)
{
txtaddress.Text = "";
txtschool.Text = "";
txtvision.Text = "";
}
}
对于你的学校模型,请添加主键,因为sqlite表需要一个主键。
public class School
{
[PrimaryKey,AutoIncrement]
public int Id { get; set; }
public string SchoolName { get; set; }
public string Address { get; set; }
public string Vision { get; set; }
}
,
@Cherry_Bu 在 mainpage.xaml.cs 上
using SQLite;
using System;
using System.IO;
using Xamarin.Forms;
namespace EditSekolah
{
public partial class MainPage : ContentPage
{
public SQLiteConnection conn;
private string path;
public MainPage()
{
InitializeComponent();
var connection = new SQLiteConnection(path);
}
private void SaveClick(object sender,EventArgs e)
{
School school = new School();
school.SchoolName = txtschool.Text.ToString();
school.Address = txtaddress.Text.ToString();
school.Vision = txtvision.Text.ToString();
var data = conn.Table<School>();
var d1 = data.Where(x => x.SchoolName == school.SchoolName).FirstOrDefault();
if (d1 == null)
{
conn.Insert(school);
Console.WriteLine("Sucessfully Added");
}
else
{
school.Id = d1.Id;
conn.Update(school);
Console.WriteLine("Already school name Exist,update");
}
txtaddress.Text = "";
txtschool.Text = "";
txtvision.Text = "";
}
private void ClearClick(object sender,EventArgs e)
{
txtaddress.Text = "";
txtschool.Text = "";
txtvision.Text = "";
}
}
public class School
{
[PrimaryKey,AutoIncrement]
public int Id { get; set; }
public string SchoolName { get; set; }
public string Address { get; set; }
public string Vision { get; set; }
}
}
像这样在 mainpage.xaml 上。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="EditSekolah.MainPage">
<StackLayout>
<Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
<Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
</Frame>
<StackLayout>
<Label Text="School Name:" />
<Entry x:Name="txtschool" />
<Label Text="Address:" />
<Entry x:Name="txtaddress" />
<Label Text="Vision:" />
<Entry x:Name="txtvision" />
<Button Clicked="SaveClick" Text="Save" />
<Button Clicked="ClearClick" Text="Clear" />
</StackLayout>
<Label Text="Start developing now" FontSize="Title" Padding="30,10,30,10"/>
<Label Text="Make changes to your XAML file and save to see your UI update in the running app with XAML Hot Reload. Give it a try!" FontSize="16" Padding="30,0"/>
<Label FontSize="16" Padding="30,24,0">
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="Learn more at "/>
<Span Text="https://aka.ms/xamarin-quickstart" FontAttributes="Bold"/>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
</ContentPage>
每次调试时,总是以错误告终。我不知道为什么会发生这个问题..