***********************C#连接数据库7个步骤:增删改*******************
1.导包
using System.Data.SqlClient;
2.准备连接字符串
String conStr = "Data Source=localhost;Initial Catalog=myschool;Integrated Security=True";
3.准备连接对象
SqlConnection con = new SqlConnection(conStr);
4.打开连接
con.Open();
5.准备sql语句
String sql = "增删改的语句";
6.准备执行sql语句的对象
SqlCommand com = new SqlCommand(sql,con);
7.执行sql语句
com.ExecuteNonQuery();
8.关闭连接
con.Close();
***********************C#连接数据库7个步骤:查询*******************
1.导包
using System.Data.SqlClient;
2.准备连接字符串
String conStr = "Data Source=localhost;Initial Catalog=myschool;Integrated Security=True";
3.准备连接对象
SqlConnection con = new SqlConnection(conStr);
4.打开连接
con.Open();
5.准备sql语句
String sql = "查询的语句";
6.准备执行sql语句的对象
SqlCommand com = new SqlCommand(sql,con);
7.执行sql语句
SqlDataReader r = com.ExecuteReader();
8.读取数据
while (r.Read()) {
int id = (int)r["结果集字段名"];
String name = (String)r["name"];
}
9.关闭连接
con.Close();
***********************C#连接数据库6个步骤:临时数据库*******************
1.导包
using System.Data.SqlClient;
2.准备连接字符串
String conStr = "Data Source=localhost;Initial Catalog=myschool;Integrated Security=True";
3.准备连接对象
SqlConnection con = new SqlConnection(conStr);
4.准备sql语句
String sql = "查询语句";
5.准备货车
SqlDataAdapter da = new SqlDataAdapter(sql,con);
6.准备临时数据库(仓库)
DataSet ds = new DataSet();
7.执行sql语句 并把结果 保存到临时数据库中
da.Fill(ds,"students");
------------------------结果集视图
//结果集 视图
//1.临时数据库的表 ds.Tables["students"]
//2.控件上的 this.下拉框.DataSource this.DGV.DataSource
//3.C#中的 DataView dv = new DataView(ds.Tables["students"]);
DataView dv = new DataView(ds.Tables["students"]);
dv.RowFilter = "name like '%张%'";//where后面的条件
dv.Sort = "age";//order by 排序后面的内容(按年龄升序排序)
------------------------临时数据库得到数据后 显示到下拉框、DataGridView
this.下拉框.DataSource = ds.Tables["students"];
this.DGV.DataSource = ds.Tables["students"];
------------------------通过修改临时数据库 去 改数据库的内容
SqlCommandBuilder builder = new SqlCommandBuilder(da);//传adapter进去
da.Update(ds,"students");//把ds 的 students表 映射到数据库
//设置默认启动窗口(主窗口)
Program类中 设置窗口名字
------------------------------------窗体属性-----------------------------
//设置窗口打开默认在屏幕中央
StartPosition:CenterScreen
//设置窗口不能最大化
MaximizeBox:False
//设置窗口不能缩放(拖大拖小)
FormBorderStyle:FixedSingle
//设置窗口默认 最大化
WindowState
//给窗口加 图标
Icon:选一个 .ico的图片格式文件
//给窗口加一个背景图片
BackgroundImage:选一个图片文件
------------------------------------控件名-----------------------------
Label:文本内容
TextBox:文本框
ComboBox:下拉框
RadioButton:单选框
CheckBox:多选框
DateTimePicker:日期框
Button:按钮
GroupBox:分组框
ImageList:存图片的控件
MenuStrip:菜单栏
ToolStrip:工具栏
ListView: 大图标模式 小图标模式 列表详情模式
DataGridView:专门做 增删改查 的控件(不支持多表修改,必须绑定主键列)
TreeView:树状菜单
------------------------------------控件属性----------------------------
-------Label---------文本
Font:设置字体大小和样式
BackColor:背景颜色(要透明:WEB的第一项)
-------TextBox-------文本框
Multiline:设置是否可以多行
PasswordChar:* 设置密码显示的符号
ReadOnly: 设置只读
-------ComboBox------下拉框
DropDownStyle:DropDownList 设置下拉框不能输入内容
-------ToolStrip-----工具栏
DisplayStyle:ImageAndText 设置工具 可以显示图片和文字(默认只显示图片)
------------------------------------控件方法----------------------------
-------TextBox-------文本框
this.文本框.Text : 可以 存取 文本框中内容
-------ComboBox------下拉框
this.下拉框.Text : 取 下拉框的内容
this.下拉框.SelectIndex = 0;设置下拉框选中第一项
-------RadioButton---单选框
this.单选框.Select() : 选中这个单选框
this.单选框.Checked() : 得到或设置这个单选框是否被选中
-------DateTimePicker--日期框
this.日期框.Value.toString("yyyy-MM-dd"): 拿到日期框中的内容并显示成这样的格式
-------ListView的方法------
给ListView添加内容(显示数据):
1.设置ListView为列表模式
-----------this.listView1.View = View.Details;
2.添加列
-----------this.listView1.Columns.Add("编号");
-----------this.listView1.Columns.Add("姓名");
3.创建项 和 子项(项:首列内容 子项是后面的内容)
-----------ListViewItem lv = new ListViewItem("001");
-----------lv.SubItems.Add("张三");
4.把创建的项目 添加到ListView中去
-----------this.listView1.Items.Add(lv);
得到ListView选中的内容:
this.listView1.SelectedItems[0].SubItems[0].Text;-----得到选中那行的第一列的内容
-------DataGridView的方法---------------
显示数据:
1.窗口中 创建列
2.绑定列的显示数据库字段 列的属性 DataPropertyName:查询的结果集的字段名字
3.数据库拿到数据:临时数据库
String conStr = "Data Source=localhost;Initial Catalog=myschool;Integrated Security=True";
//2.连接对象
SqlConnection con = new SqlConnection(conStr);
//3.sql语句
String sql = "select * from student";
//4.SqlDataAdapter对象
SqlDataAdapter adpter = new SqlDataAdapter(sql,con);
//5.准备临时仓库
DataSet ds = new DataSet();
//6.把数据库复制到 临时仓库
adpter.Fill(ds,"students");
//7.把临时仓库的数据 赋给 DGV控件上
this.dataGridView1.DataSource = ds.Tables["students"];