数据库简单知识
1. 数据类型
这部分转载 MySQL中char、varchar和nvarchar的区别_xiguyang的博客-CSDN博客_mysql nvarchar和varchar ,
数据类型,转载于 MySQL 数据类型 | 菜鸟教程 (runoob.com)
另外,可以了解下编码发展史 , 转载于 彻底弄懂 Unicode 编码_hezh1994的博客-CSDN博客_unicode编码;
做一些补充:(1) decimal默认整数部分加小数部分总共18位,小数占0位 ,decimal(m,n) 表示总共m 位数字,小数有n位,整数有m-n位,优先考虑小数部分
例如:decimal(5,3)插入16.3455, 最终为16.346,插入224.444;
(2) 例:Unicode字符数据:nvarchar(50) 表示汉字和英文都是50,非Unicode字符数据:varchar(50)汉字25,英文50
(3) image类型,储存图片为二进制数据,最多2GB。
(4) date:仅存储日期,从0001年1月1日 到 9999年12月31日
time:仅存储时间,精度为100纳秒
datetime2,从 1753 年 1 月 1 日 到 9999 年 12 月 31 日,精度为 100 纳秒。
2. 在Sql Server Management创建数据库和表格并添加数据
创建数据库:在“对象资源管理器”中右键单击服务器实例,然后选择“新建查询”,在查询窗口输入 T-SQL 代码,然后点击执行
1 USE master 2 GO 3 IF NOT EXISTS ( 4 SELECT name 5 FROM sys.databases 6 WHERE name = N'EmployeeManager' 7 ) 8 CREATE DATABASE [EmployeeManager] 9 GO
创建表格:在数据库下拉列表中,选择所需数据库,在查询窗口输入 T-SQL 代码,然后点击执行
1 USE [EmployeeManager] 2 -- Create a new table called 'Customers' in schema 'dbo' 3 -- Drop the table if it already exists 4 IF OBJECT_ID('dbo.PlanManage', 'U') IS NOT NULL 5 DROP TABLE dbo.PlanManage 6 GO 7 -- Create the table in the specified schema 8 CREATE TABLE dbo.PlanManage 9 ( 10 Id int not null primary key, 11 ExcetDate dateTime Not Null, 12 PlanContent text , 13 Level char NOT NULL, 14 CreateNo int Not Null, 15 ExcetNo int Not Null, 16 IsRemind bit not null, 17 RemindDate dateTime Not Null, 18 IsValid bit not null 19 ); 20 GO
在表中添加数据:
1 INSERT INTO dbo.PlanManage 2 ([Id],[ExcetDate],[PlanContent],[Level] ,[CreateNo] ,[ExcetNo],[IsRemind],[RemindDate],[IsValid]) 3 VALUES 4 ( 1,getdate(), '买烟', 'A', 100003,100005,N'1',getdate()-1,N'1') 5 GO
3. 连接数据库,将数据传至winform窗体(这里我还在数据库中建了一个Employee表,包含员工一些信息)

SqlServer数据库连接查询方法:
1 public bool ConnectionTest() 2 { 3 //创建连接对象 4 using (SqlConnection m_Conn = new SqlConnection(connectString)) 5 { 6 //mySqlConnection.ConnectionTimeout = 1;//设置连接超时的时间 7 try 8 { 9 //Open DataBase 10 //打开数据库 11 m_Conn.Open(); 12 if (m_Conn.State == ConnectionState.Open) 13 { 14 return true; 15 } 16 } 17 catch (Exception e) 18 { 19 MessageBox.Show(e.ToString()); 20 } 21 } 22 return false; 23 } 24 25 public DataTable ExecuteDataTable(string SQL) 26 { 27 DataTable dt = new DataTable(); 28 using (SqlConnection m_Conn = new SqlConnection(connectString)) 29 { 30 try 31 { 32 SqlDataAdapter da = new SqlDataAdapter(); 33 SqlCommand cmd = new SqlCommand(SQL, m_Conn); 34 da.SelectCommand = cmd; 35 da.Fill(dt); 36 } 37 catch (Exception e) 38 { 39 MessageBox.Show(e.ToString()); 40 } 41 } 42 return dt; 43 } 44 45 public DataTable ExecuteDataset(string SQL, params DbParameter[] ParaName) 46 { 47 DataSet ds = new DataSet(); 48 using (SqlConnection m_Conn = new SqlConnection(connectString)) 49 { 50 try 51 { 52 SqlDataAdapter da = new SqlDataAdapter(); 53 SqlCommand cmd = new SqlCommand(SQL, m_Conn); 54 if (ParaName != null) 55 { 56 cmd.Parameters.AddRange(ParaName); 57 } 58 da.SelectCommand = cmd; 59 da.Fill(ds); 60 } 61 catch (Exception e) 62 { 63 MessageBox.Show(e.ToString()); 64 } 65 } 66 DataTable dt = ds.Tables[0]; 67 return dt; 68 }
winform窗体中的方法:
1 private void Form1_Load(object sender, EventArgs e) 2 { 3 string connect = "Data Source=DESKTOP-92RUK0U; Initial Catalog = EmployeeManager; Integrated Security = True"; 4 mssql = new MssqlFactory(connect); 5 if (mssql.ConnectionTest()) 6 MessageBox.Show("连接成功"); 7 string sql = @"select EmployeeId,Name,Password,Position,LeaderId,IsManager from Employee"; 8 DataTable dt = mssql.ExecuteDataTable(sql); 9 TreeNode treeNode = new TreeNode(); 10 treeNode.Text = "小明"; 11 treeNode.Tag = 100001; 12 treeView1.Nodes.Add(treeNode); 13 BindTree(100001,dt,treeNode); 14 treeView1.ExpandAll();//展开treeView 15 treeView1.SelectedNode = treeView1.Nodes[0];//默认选中第一个节点 16 } 17 18 /// <summary> 19 /// 递归传回所有数据 20 /// </summary> 21 /// <param name="employeeId"></param> 22 /// <param name="dt"></param> 23 /// <param name="node"></param> 24 private void BindTree(int employeeId ,DataTable dt ,TreeNode node) 25 { 26 DataRow[] rows = dt.Select($"LeaderId={employeeId}"); 27 foreach (DataRow row in rows) 28 { 29 TreeNode childNode = new TreeNode(); 30 childNode.Text = row["Name"].ToString(); 31 int num = Convert.ToInt32(row["EmployeeId"]); 32 childNode.Tag = num; 33 node.Nodes.Add(childNode); 34 BindTree(num, dt, childNode); 35 } 36 } 37 38 private void Search() 39 { 40 DateTime excetTime = dateTimePicker2.Value; 41 int id = (int)treeView1.SelectedNode.Tag; 42 string sql = @"select [Id], [ExcetDate], [PlanContent], [Level], b.Name as CreateName, c.Name as ExcetName,(case IsRemind when 1 then '是' else '否' end) [IsRemind], [RemindDate],(case IsValid when 1 then '是' else '否' end)[IsValid] from PlanManage a 43 left join Employee b 44 on(a.CreateNo=b.EmployeeId ) 45 left join Employee c 46 on(a.ExcetNo=c.EmployeeId ) 47 where convert(varchar,ExcetDate ,111)=convert(varchar,@ExcetDate,111)"; 48 sql += "and (a.CreateNo = @id or a.ExcetNo = @id)"; 49 SqlParameter[] paras = 50 { 51 new SqlParameter ("@ExcetDate",excetTime), 52 new SqlParameter ("@id",id), 53 }; 54 this.dataGridView1.DataSource = mssql.ExecuteDataset(sql,paras); 55 }
查询效果:

以上为个人学习的案例,如有错误,请指正

浙公网安备 33010602011771号