复杂的数据绑定# 一.C# 结构体类型
struct Student{
public uintid;(32位无符号整数类型)
public string name;
}
枚举类型
enum Season{
Spring,
Summer,
Autumn,
Winter
};
一维数组
int[] array = {20,30,40,50}
int[] array = new int[4] {20,30,40,50}
多维数组
int[,]array={{1,2,3},{4,5,6}} //二维数组
array[0,1]==2
控制台输入输出
Console.Write()//输出
Console.WriteLine()//输出并在结尾+一个换行符
Console.ReadLine()//输入
类型转换
int number=Convert.ToInt32(year)//将字符串类型转换位int类型
异常处理机制
try{
程序代码块;
}
catch(Exception e){
异常处理代码块;
}
catch(Exception e){
异常处理代码块;
}
finally{
无论是否发生异常,均要执行的代码块;
}
类
类的定义:
public class Animals{
string _name=name;
int _age=age;
}
class Animals{
private string _name=name;
private int _age=age;
}
类的构造函数:
class animals{
private string _name;
private int _age;
}
//animals类有参的构造函数
public animals(string name,int age){
string_name=name;
int_age=age;
}
//animals类无参的构造函数
public animals{
string _name="sheep"
int _age=5;
}
类的初始化:
Animals a1=new Animals();
Animals a2=new Animals("sheep",5)
继承:
[类修饰符] class 类名:[父类名]{
[成员修饰符] 成员变量;
[成员修饰符] 成员函数;
}
//父类
class animals{
private string _name;
private int _age;
}
//父类构造函数
public animals(string name,int age){
string _name=name;
int _age=age;
}
//子类
class sheep:animals{
public Sheep(String str,int time)base(str,time) //当父类的构造函数没有参数时,可以省略base
}
多态
多态体现
同一方法接受不同的参数,产生不同的状态或结果
public void train(Dog dog){
//训练小狗站立、排队、做算术的代码
}
public void train(Monkey mokey){
//训练小猴敬礼、翻跟头、骑自行车的代码
}
子类重写父类方法
virtual声明的方法可以被子类重写
子类用override对继承父类的方法进行重写
//父类
class animals{
public virtual void train(string motion){
Console.WriteLine("小动物做"+motion);
}
}
//子类
class dog:animals{
public override void train(string motion){
Console.WriteLine("小🐕做"+motion);
}
}
一.访问数据库
链接数据库 SqlConnection
- 声明一个Connection对象
- 为对象属性设置一个初值,这个初值为登录数据库的信息
- 利用构造函数创建数据库对象
- 获取配置文件的数据库连接信息
- 打开数据库连接
//声明数据库对象
public SqlConnection{
string connectionString;
//设置数据库初值
public class ConString{
public static string ConnectionString=" Data Source=.; Initial Catalog=数据库名称 ;User ID=sa ; Password= ;"
}
//创建数据库对象
SqlConnection connection = new SqlConnection(ConString.ConnectionString);
//打开数据库连接
connection.Open();
connection对象是数据库的对象,包含了数据库的相关信息
二.获取数据
SqlCommand
- 创建Command对象
- 设置属性值
//创建Command对象
SqlCommand myCommand = new myCommand();
//设定Command命令操作的数据库对象
myCommand.Connection=connection
//设定Command命令操作的数据库表
myCommand.CommandText="Select * from DataTable;"
SqlDataReader 数据阅读器变量
- DataReader对象的作用是从数据库中检索只读、只进的数据
- 只进数据:指记录的接收是顺序进行且不可后退的
- 可以使用Read方法将查询返回的数据读出
- DataReader对象可以提高应用程序性能,因为它只要数据可用就立即检索数据,默认一次在内存中取一行,减少系统开销
DataReader对象的创建
SqlDataReader 数据阅读器变量名=Command变量.ExecuteReader();
SqlDataReader myReader=myCommand.ExecuteReader();
Command和DataReader对象配合使用,可以实现数据库的读写和输出,先创建Command对象的实例,再用Command对象的实例调用ExecuteReader()方法为DataReader对象赋值
三.填充数据集
数据集DataSet可以实现数据库的离线操作,其保存了数据库或数据库的部分数据
通过DataAdapter对象向DataSet中或者数据库表中填充数据
DataAdapter对象
与Command对象类似,但DataAdapter对象主要用于数据集(DataSet)与数据源(数据库)之间的数据交互的桥梁,负责填充和更新数据集。
常用属性:
- SelectCommand:从数据源中检索数据
- InsertCommand:把从DataSet中插入的记录写入数据源
- UpdateCommand:把从DataSet中更新的记录写入数据源
- DeleteCommand:从数据源中删除数据
DataAdapter对象的使用
//声明
SqlDataAdapter dataAdapter = new dataAdapter();
//定义
dataAdapter.SelectCommand="select * from MobilephoneInfo";
//填充数据集DataSet
dataAdapter.Fill(dataset.MobilephoneInfo);
DataSet对象
DataSet对象从数据源中获取数据以后就断开了与数据库源之间的连接,完成各项操作以后还可以把DataSet对象中的数据送回数据源
DataSet dataSet=new DataSet();
SqldataAdapter dataAdapter= new SqldataAdapter();
dataAdapter.SelectCommand='Select.......'
dataAdapter.Fill(dataSet)
数据绑定
简单的数据绑定
<strong>网络算术计算器</strong><br/>
第一个数值<asp:TextBox ID="TextBoxl" runat=""server"></asp:TextBox>
✖第二个数值asp:TextBoX ID="TextBox2" runat-"server"></asp:TextBox>
<asp:Button ID="Button!" nunat="server" Text="计算" onclick="Buttonl_Click"/>cbor>
<form>
第一个操作数为:<%#oper1%>
第二个操作数为:<%#oper2%>
计算结果为:<%oper1*oper2%>
</form>
//事件处理程序
protected int oper1;
protected int oper2;
protected void Buttonl Click(object sender, EventArgs e){
operl =System.Convert.ToInt32(TextBox1.Text);
oper2 =System.Convert.ToInt32(TextBox2.Text);
Page.DataBind();//数据源中的数据绑定到页面上
}
复杂的数据绑定
//设计Web页面
<asp:Label ID="Label1" runat="server" Text="绑定复杂数据源"></asp:Label>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="请选择您喜欢的手机"></asp:Label>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Label ID="Label3" runat="server"></asp:Label>
//绑定事件源
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList array = new ArrayList();
array.Add("iphone");
array.Add("xiaomi");
array.Add("huawei");
DropDownList1.DataSource = array;
DropDownList1.DataBind();
}
}
//绑定处理程序
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label3.Text = "您喜欢的手机是:"+DropDownList1.SelectedValue;
}
LINQ技术
查询数据库
- 创建数据库对象
- 查询代码
DataClassesDataContext data = new DataClassesDataContext();
var mob = from m in data.mobilephoneInfo
select m;
插入数据库
- 创建数据库对象
- 创建插入信息
- 将信息插入
DataClassesDataContext data = new DataClassesDataContext();
MobilephoneInfo m = new MobilephoneInfo();
m.ID='10009'
m.Name=''
da.MobliephoneInfo.InsertSubmit(m)
da.submitChanges();
修改数据库
- 创建数据库对象
- 选择数据库对象的数据库表进行查询
- 将查询结果保存
- 对查询结果修改
- 提交至数据库
DataClassesDataContext data = new DataClassesDataContext();
var M=from m in data.mobilephoneInfo() select m;
foreach(mobilephoneInfo m in M)
m.ID='10009'
m.Name=''
...
}
da.submitchanges();
删除数据库
- 创建数据库对象
- 选择数据库对象的数据表进行查询
- 将查询结果保存
- 对查询结果进行删除并提交数据库
DataClassesDataContext data = new DataClassesDataContext();
var M=from m in data.mobilephoneInfo() select m;
da.MobliephoneInfo.DeleteALLonSubmit(m)
da.submitChanges();