目錄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("寫入數據成功"); } } } }