目录sql
数据库的基本使用
@数据库
格式: insert into 表名() values()
其中,values 是带sexpress
**格式: i**nsert into T1(name,age) values('aaa',30) T1是表名; name,age 是T1表中的键名,则'aaa',30是分别对应的值 == >values中的顺序要和表名后的顺序一致。
格式: select Name,Id from T1 Name, Id 是表中的键名,多个键名之间使用逗号分开 T1 是表格
"delete from Table_Student where Name ='aaa' "; delete from T_Student where Name='yzk' 从 T_Student表中删除Name字段等于yzk的数据 delete from T_Student 删除全部数据 delete from T_Student where Age>100 or Name='yzk' 或者 delete from T_Student where Age>100 and Height>150 而且
update T_Student set Age=Age+1 update T_Student set Age=Age+1,AiHao='吃饭' update T_Student set AiHao='散步' where Age>100 select * from T_Student where Age<30
select * from T_Student where Name like 'y%' 这个是查询以y开头的数据 select * from T_Student where Name like 'y%k' 这个是查询以y开头, k为结尾的数据 select * from T_Student where Name like '%a%' 查询含有名字中含有a 的数据
按照一个字段排序 select * from T_Student order by Age 默认是升序排列ide
select * from T_Student order by Age, Height
若是Age同样,则再按照Height排序函数
下面是降序排列,降序Descendingspa
select * from T_Student order by Age Desc
下面是若是Age同样,则再按照Height排序.net
select * from T_Student order by Age, Height DEsc
下面的语句是一个总体3d
select * from T_Student where Name like 'y%' order by Age
where要在order by以前code
GETDATA(),sql 内置的函数
用来获取当前时间
其中,下面的as是在查询结果显示的时候,起别名orm
select Name as selectname, GETDATE() from Table_Student
查询Height、Age的最大值 select Max(Height) as maxid, Age as myAge from Table_Student
下面是查询在Age > 100 中全部的行数 select COUTN(*) from Table_Student where Age >100
简介:.Net中用来向数据库提交执行SQL语句的一堆类。
// SqlConnection 继承的父类 SqlConnection 实如今对自动回收接口的继承 // public abstract class DbConnection : Component, IDbConnection, IDisposable /* SqlConnection 实例化创建新的链接 * Data Source 表示 IP,若是是本机通讯的话,只须要写 . 就能够了 * Initial Catalog 对应的数据库中将要操做的表格 * User ID 表示数据库登陆的帐户名称,sa表示的是管理员帐户名称 * Password 表示数据库登陆的密码 * */ // 如数据库创建链接 using (SqlConnection conn = new SqlConnection ("Data Source = 192.168.43.102; Initial Catalog = MyTest; User ID = sa; Password = 123")) { conn.Open(); // 打开数据库 /* * conn.CreateCommand 建立操做命令 * cmd.CommandText 后面接的是sql语句 * cmd.ExecuteNonQuery(); 实施查询 * */ using (SqlCommand cmd = conn.CreateCommand()) // 建立数据库的操做指令信息 { // 操做数据库的相关指令信息 } }
一下是在wpf 中的Button按钮对应的控制方法
,cs 文件内容以下:
/// <summary> /// 向数据库写入信息 /// Insert语句的使用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_Click(object sender, RoutedEventArgs e) { // SqlConnection 继承的父类 SqlConnection 实如今对自动回收接口的继承 // public abstract class DbConnection : Component, IDbConnection, IDisposable /* SqlConnection 实例化创建新的链接 * Data Source 表示 IP,若是是本机通讯的话,只须要写 . 就能够了 * Initial Catalog 对应的数据库中将要操做的表格 * User ID 表示数据库登陆的帐户名称,sa表示的是管理员帐户名称 * Password 表示数据库登陆的密码 * */ using (SqlConnection conn = new SqlConnection ("Data Source = 192.168.43.102; Initial Catalog = MyTest; User ID = sa; Password = 123")) { conn.Open(); // 打开数据库的链接 /* * conn.CreateCommand 建立操做命令 * cmd.CommandText 后面接的是sql语句 * cmd.ExecuteNonQuery(); 实施查询 * */ using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "Insert into Table_Student(Name, Age, Height) Values ('Vs插入数据', 12, 12.1212)"; cmd.ExecuteNonQuery(); } MessageBox.Show("写入数据成功"); } }
对应的
MainWindow.xaml文件以下
<Window x:Class="ShuJuKu15_ADONet_JiChu.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ShuJuKu15_ADONet_JiChu" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <Button x:Name="button" Content="写入数据" HorizontalAlignment="Left" Margin="230,92,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/> </Grid> </Window>
对应的界面以下
对应的.cs文件
/// <summary> /// 数据库删除信息 /// delete语句的使用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_delete_Click(object sender, RoutedEventArgs e) { // SqlConnection 继承的父类 SqlConnection 实如今对自动回收接口的继承 // public abstract class DbConnection : Component, IDbConnection, IDisposable using (SqlConnection conn = new SqlConnection ("Data Source = .; Initial Catalog = MyTest; User ID = sa; Password = 123")) //("Data Source = 192.168.43.102; Initial Catalog = MyTest; User ID = sa; Password = 123")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "delete from Table_Student where Name ='aaa' "; cmd.ExecuteNonQuery(); } MessageBox.Show("删除数据成功"); } }
对应的.wpf文件
<Window x:Class="ShuJuKu15_ADONet_JiChu.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ShuJuKu15_ADONet_JiChu" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <Button x:Name="btn_delete" Content="删除数据" HorizontalAlignment="Left" Margin="230,120,0,0" VerticalAlignment="Top" Width="75" Click="btn_delete_Click" /> </Grid> </Window>
对应的界面
其余的操做同理。最后,融合在一块儿后, 造成的界面以下:
对应的.wpf 文件以下
<Window x:Class="ShuJuKu15_ADONet_JiChu.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ShuJuKu15_ADONet_JiChu" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <Button x:Name="button" Content="写入数据" HorizontalAlignment="Left" Margin="230,92,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/> <Button x:Name="btn_delete" Content="删除数据" HorizontalAlignment="Left" Margin="230,120,0,0" VerticalAlignment="Top" Width="75" Click="btn_delete_Click" /> <Button x:Name="btn_update" Content="更新数据" HorizontalAlignment="Left" Margin="230,150,0,0" VerticalAlignment="Top" Width="75" Click="btn_update_Click" /> <Button x:Name="btn_queryAllLine" Content="查询总行数" HorizontalAlignment="Left" Margin="363,92,0,0" VerticalAlignment="Top" Width="75" Click="btn_queryAllLine_Click"/> <Button x:Name="btn_writeAndReturnId" Content="写入数据并方式对应的ID" HorizontalAlignment="Left" Margin="320,120,0,0" VerticalAlignment="Top" Width="150" Click="btn_writeAndReturnId_Click"/> </Grid> </Window>
对应的.cs文件以下
using System; using System.Collections.Generic; using System.Data.SqlClient; // 数据库空间引入 using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ShuJuKu15_ADONet_JiChu { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } /// <summary> /// 向数据库写入信息 /// Insert语句的使用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_Click(object sender, RoutedEventArgs e) { // SqlConnection 继承的父类 SqlConnection 实如今对自动回收接口的继承 // public abstract class DbConnection : Component, IDbConnection, IDisposable /* SqlConnection 实例化创建新的链接 * Data Source 表示 IP,若是是本机通讯的话,只须要写 . 就能够了 * Initial Catalog 对应的数据库中将要操做的表格 * User ID 表示数据库登陆的帐户名称,sa表示的是管理员帐户名称 * Password 表示数据库登陆的密码 * */ using (SqlConnection conn = new SqlConnection ("Data Source = 192.168.43.102; Initial Catalog = MyTest; User ID = sa; Password = 123")) { conn.Open(); // 打开数据库的链接 /* * conn.CreateCommand 建立操做命令 * cmd.CommandText 后面接的是sql语句 * cmd.ExecuteNonQuery(); 实施查询 * */ using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "Insert into Table_Student(Name, Age, Height) Values ('Vs插入数据', 12, 12.1212)"; cmd.ExecuteNonQuery(); } MessageBox.Show("写入数据成功"); } } /// <summary> /// 数据库删除信息 /// delete语句的使用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_delete_Click(object sender, RoutedEventArgs e) { // SqlConnection 继承的父类 SqlConnection 实如今对自动回收接口的继承 // public abstract class DbConnection : Component, IDbConnection, IDisposable using (SqlConnection conn = new SqlConnection ("Data Source = .; Initial Catalog = MyTest; User ID = sa; Password = 123")) //("Data Source = 192.168.43.102; Initial Catalog = MyTest; User ID = sa; Password = 123")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "delete from Table_Student where Name ='aaa' "; cmd.ExecuteNonQuery(); } MessageBox.Show("删除数据成功"); } } /// <summary> /// 数据库更新信息 /// update 语句的使用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_update_Click(object sender, RoutedEventArgs e) { using (SqlConnection conn = new SqlConnection ("Data Source = 192.168.43.102; Initial Catalog = MyTest; User ID = sa; Password = 123")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "update Table_Student set Age = 2 where Name ='Vs插入数据' "; cmd.ExecuteNonQuery(); } MessageBox.Show("更新数据成功"); } } /// <summary> /// 数据库查询信息 /// select 语句的应用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_queryAllLine_Click(object sender, RoutedEventArgs e) { using (SqlConnection conn = new SqlConnection ("Data Source = 192.168.43.102; Initial Catalog = MyTest; User ID = sa; Password = 123")) { int tmp = 0; conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select count(*) from Table_Student "; int i = (int)cmd.ExecuteScalar(); //ExecuteScalar通常用来执行有且只有一行一列返回值的SQL语句 tmp = i; /* //下面这段是查询数据库表格中是否有'aaa'并返回aaa cmd.CommandText = "select 'aaa'"; //ExecuteScalar通常用来执行有且只有一行一列返回值的SQL语句 string s = (string)cmd.ExecuteScalar(); MessageBox.Show(s); */ } MessageBox.Show("总共有【" + tmp + "】条数据"); } } /// <summary> /// 写入数据库中的表格并获取ID /// insert 与 select ,及关键词 @@identity , output 的使用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_writeAndReturnId_Click(object sender, RoutedEventArgs e) { // SqlConnection 继承的父类 SqlConnection 实如今对自动回收接口的继承 // public abstract class DbConnection : Component, IDbConnection, IDisposable using (SqlConnection conn = new SqlConnection ("Data Source = 192.168.43.102; Initial Catalog = MyTest; User ID = sa; Password = 123")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "insert into Table_Student(Name, Age) values('bbb', 111)"; cmd.CommandText = "select Max(ID) as maxid Table_Student "; string s = (string)cmd.ExecuteScalar(); MessageBox.Show(s); //得到自字段的值 // @@ 是什么意思 // 这段注释一样是写入数据库中的表格并获取ID, 可是不是很好,由于这是,若是下写入数据后,若是有新的数据 //忽然插进来写入新的数据,这个时候获取的ID就不是原来的ID , 而是新的ID,这就不对了 cmd.CommandText = "insert into Table_Student(Name,Age) values('aaa',123) ;select @@identity"; decimal i = (decimal)cmd.ExecuteScalar();// ////写入数据并返回该数据对应的id值 //cmd.CommandText = "insert into Table_Student(Name,Age) output inserted.Id values('aaa',123) "; //long i = (long)cmd.ExecuteScalar();//数据库bigint→C#平台long MessageBox.Show(i.ToString()); } MessageBox.Show("写入数据成功"); } } } }