数据库的基本使用(C#语言)
数据库的基本使用
@
目录
insert
格式: insert into 表名() values()
其中,values 是带s
**格式: i**nsert into T1(name,age) values('aaa',30)
T1是表名;
name,age 是T1表中的键名,则'aaa',30是分别对应的值
== >values中的顺序要和表名后的顺序一致。
select 的使用
格式: select Name,Id from T1
Name, Id 是表中的键名,多个键名之间使用逗号分开
T1 是表格
delete
"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 更新
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
Like模糊查询
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 的数据
Order 排序
按照一个字段排序 select * from T_Student order by Age 默认是升序排列
select * from T_Student order by Age, Height
如果Age一样,则再按照Height排序
下面是降序排列,降序Descending
select * from T_Student order by Age Desc
下面是如果Age一样,则再按照Height排序
select * from T_Student order by Age, Height DEsc
下面的语句是一个整体
select * from T_Student where Name like 'y%'
order by Age
where要在order by之前
GETDATA()
GETDATA(),sql 内置的函数
用来获取当前时间
其中,下面的as是在查询结果显示的时候,起别名
select Name as selectname, GETDATE() from Table_Student
聚合函数:MAX,MIN,AVG,SUM,COUNT
Max
查询Height、Age的最大值
select Max(Height) as maxid, Age as myAge from Table_Student
COUNT
下面是查询在Age > 100 中所有的行数
select COUTN(*) from Table_Student where Age >100
ADO.Net 平台的使用
简介:.Net中用来向数据库提交执行SQL语句的一堆类。
在C#中使用的格式
// 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()) // 创建数据库的操作指令信息
{
// 操作数据库的相关指令信息
}
}
insert 在 C# 的使用
一下是在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>
对应的界面如下
delete 的使用
对应的.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("写入数据成功");
}
}
}
}
转载请注明出处。https://www.cnblogs.com/jyfootprint/p/9457346.html