断开式绑定ComboBox关键命令
绑定数据
组合框对象.DataSource=数据集.Tables[表名称];
设置显示的内容源
组合框对象.DisplayMember=数据表的列名
设置提交的内容源
组合框对象.ValueMember=数据表的列名
获取选中的值
组合框对象.SelectedValue
获取选中的文本
组合框对象.Text
DataSet对象的命令
实例化一个仓库DataSet对象
DataSet ds对象=new DataSet();
实例化一个小货车DataAdapter对象
SqlDataAdpter da对象=new SqlDataAdpter(sql对象,连接对象);
小货车对象卸货到仓库中
da对象.Fill(ds对象,表名称)//表名称为自己起对的名字
仓库中的表新建一个行
DataRow 行对象=ds对象.Tables[表名称].NewRow()
给行对象的单元格赋值
通过列索引给值
行对象[索引]=值;
往仓库的表中添加行
ds对象.Tables[表名称].Rows.InsertAt(行对象,索引);//把行插入在索引对应的行集合中
**增删改对象SqlCommandBuilder **
获得对象
SqlCommandBuilder scb对象=new SqlCommandBuilder (da对象)
更新的方法
da对象.Update (ds对象,数据表名称)
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 断开式数据连接
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//连接对象
String connStr = "Data Source=User-2020WAFNWR;Initial Catalog=yyy;Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
//实例化一个DataSet对象,仓库对象
DataSet ds = new DataSet();
Console.WriteLine(ds);
Console.WriteLine("目前仓库中有{0}张表", ds.Tables.Count);//ds.Tables.Count表示仓库中标的总数
String sql = "select * from acc;";
//实例化一个小车对象
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
Console.WriteLine(da);
//让小车卸货
da.Fill(ds,"one");
//cb控件.数据源=仓库.表集合[表名称]
cbCountry.DataSource = ds.Tables[0];
//绑定数据,让dataset仓库中某个表的值与控件绑定
cbCountry.DisplayMember = "name";
cbCountry.ValueMember = "id";
Console.ReadLine();
}
private void cbCountry_SelectedIndexChanged(object sender, EventArgs e)
{
//拿到当前选中的项目文本
//控件对象.选中的文本
MessageBox.Show(cbCountry.SelectedValue.ToString());
}
}
}