什么是存儲過程?
用某百科的話來說就是一堆為了完成某一功能或者某些功能的SQL語句的集合,而數據庫則會將這些存儲過程的方法存儲到數據庫中去。
優點:
1.可重用並且效率高:存儲過程經過一次編譯后不需要再次進行編譯,這就提高了效率,再過一次編譯的存儲過程可重復使用,無需在進行第二次編譯。
2.減少網絡流量:以前傳輸的往往是SQL語句,當使用存儲過程后使用的是參數,只需要傳遞參數即可。
3.安全性:在SqlServer中,可以使用Grant進行權限的分配不同的權限,也可以使用Revoke進行收回,還可以進行Deny拒絕。
如何使用存儲過程?
關於建立存儲,當然是在已經有的數據庫中建立。下面的是我建立的數據庫和表。
打開“可編程性”可以看到“存儲過程”,右鍵創建新的存儲過程即可:
--調用數據庫 use 數據庫名稱 go create proc 存儲過程名稱 --字段 as --關聯 go
具體代碼使用過程如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // 引入數據庫管理控件 using System.Data; using System.Data.SqlClient; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { // SQL語句插入操作 /* * // 多個參數操作 SqlConnection conn = new SqlConnection(); // 初始化一個SqlConnection()類 conn.ConnectionString = "server=服務器名稱;database=pc_test;uid=賬號;pwd=密碼"; // 連接字符串,格式(注意間隔要使用分號(";")來做分割):"server=數據庫名稱(據說可以使用"."來表示本地數據庫,沒試過);database=數據庫;uid=賬號;pwd=密碼"。注:如果使用的是"."進行登陸的話,可以將uid和pwd換成"integrated security=SSPI"即:"server=.;database=數據庫名;integrated security=SSPI"; conn.Open(); // 打開數據庫 string sql = "insert into student(name,age,sex) values(@tname,@tage,@tsex);"; // SQL命令 SqlCommand comm = new SqlCommand(sql, conn); // 初始化SqlCommand()類 SqlParameter[] pars = { new SqlParameter("@tname",SqlDbType.NChar,10), new SqlParameter("@tage",SqlDbType.Int,10), new SqlParameter("@tsex",SqlDbType.TinyInt,2) }; pars[0].Value = "namejr1"; pars[1].Value = 12; pars[2].Value = 1; comm.CommandType = CommandType.Text; // 這個表明是SQL命令 comm.Parameters.AddRange(pars); SqlDataAdapter sda = new SqlDataAdapter(comm); DataSet ds = new DataSet(); sda.Fill(ds, "student"); // 對這個表進行顯示,具體顯示方法下一個會給出來 conn.Close();*/ // /* * 單條SQL操作 SqlConnection conn = new SqlConnection(); conn.ConnectionString = "server=NAMEJR-PC;database=pc_test;uid=sa;pwd=123456"; // 連接字符串,格式(注意間隔要使用分號(";")來做分割):"server=數據庫名稱(據說可以使用"."來表示本地數據庫,沒試過);database=數據庫;uid=賬號;pwd=密碼"。注:如果使用的是"."進行登陸的話,可以將uid和pwd換成"integrated security=SSPI"即:"server=.;database=數據庫名;integrated security=SSPI"; conn.Open(); // 打開數據庫 string sql = "select name,age,sex from student where id=@Id;"; SqlCommand comm = new SqlCommand(sql, conn); SqlParameter par = comm.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int, 10)); par.Value = 1; comm.CommandType = CommandType.Text; // 這個表明是SQL命令 SqlDataAdapter sda = new SqlDataAdapter(comm); DataSet ds = new DataSet(); sda.Fill(ds, "student"); foreach(DataTable dt in ds.Tables) { foreach(DataRow dr in dt.Rows) { foreach(DataColumn dc in dt.Columns) { // dt.TableName:就是使用Fill()指定的表名。dc.ColumnName字段名,dr[dc]字段值 Console.WriteLine("{0}=>{1}=>{2}", dt.TableName, dc.ColumnName, dr[dc]); } } } conn.Close(); */ // /* * 快捷顯示 SqlConnection conn = new SqlConnection(); conn.ConnectionString = "server=NAMEJR-PC;database=pc_test;uid=sa;pwd=123456"; conn.Open(); string sql = "execute TSelect 1"; SqlCommand comm = new SqlCommand(sql, conn); SqlDataAdapter sda = new SqlDataAdapter(comm); DataSet ds = new DataSet(); sda.Fill(ds, "student"); // 顯示同上 */ } } }
Sqlserver中對存儲過程的一些操作:
exec sp_databases -- 查看數據庫
exec sp_tables -- 查看表
exec sp_columns 表名 -- 查看所填表名的列
exec sp_helpIndex 表名-- 查看對應表的索引
exec sp_helpConstraint 表名 -- 查看對應表的約束
exec sp_stored_procedures -- 查看全部的存儲過程
exec sp_helptext 存儲進程名 -- 查看對應的存儲進程結構
此外還有sp_rename(修改表/列/索引的名稱)、sp_renamedb(修改數據庫名稱)、sp_defaultdb(設置默認登錄數據庫)、sp_helpdb(獲取幫助信息)
帶輸入輸出函數的存儲過程:
sqlserver查詢記錄命令: use 數據庫名 declare @CourtName nvarchar(32) declare @totalRoomNum int declare @NumHousesComple int declare @NumHousesNotComple int declare @HCTO int declare @HCTON int exec 存儲過程名 617,82297,'2019-03-28',@CourtName output,@totalRoomNum output,@NumHousesComple output,@NumHousesNotComple output,@HCTO output,@HCTON output print @CourtName print @totalRoomNum print @NumHousesComple print @NumHousesNotComple print @HCTO print @HCTON
#region 執行帶輸入輸出的存儲過程 public static void IOSql(string proName, string Courtid, long _costtype, string _time, out string CourtName, out int totalRoomNum, out int NumHousesComple, out int NumHousesNotComple, out int HCTO, out int HCTON) { /* SqlConnection conn = new SqlConnection("server=服務器地址;uid=賬號;pwd=密碼;database=數據庫"); */ // 連接數據庫 using (SqlConnection conn = GetConnection()) { conn.Open(); // 打開數據庫 using(SqlCommand commd = conn.CreateCommand()){ commd.CommandType = CommandType.StoredProcedure; // 存儲過程方式 commd.CommandText = proName; // 存儲過程名稱(可以是表名什么的) // 構建數據類型 SqlParameter[] para=new SqlParameter[]{ new SqlParameter("@CourtId",SqlDbType.Int), new SqlParameter("@CostType",SqlDbType.Int), new SqlParameter("@time",SqlDbType.Date), new SqlParameter("@CourtName",SqlDbType.NVarChar,32), // 注意,使用nvarchar的時候記得寫長度 new SqlParameter("@totalRoomNum",SqlDbType.Int), new SqlParameter("@NumHousesComple",SqlDbType.Int), new SqlParameter("@NumHousesNotComple",SqlDbType.Int), new SqlParameter("@HCTO",SqlDbType.Int), new SqlParameter("@HCTON",SqlDbType.Int) }; para[0].Value = Courtid; para[1].Value = _costtype; para[2].Value = _time.ToString(); para[3].Direction = ParameterDirection.Output; para[4].Direction = ParameterDirection.Output; para[5].Direction = ParameterDirection.Output; para[6].Direction = ParameterDirection.Output; para[7].Direction = ParameterDirection.Output; para[8].Direction = ParameterDirection.Output; commd.Parameters.AddRange(para); commd.ExecuteNonQuery(); // 執行語句 // 單個數據查看 CourtName = Convert.ToString(commd.Parameters[3].Value); totalRoomNum = Convert.ToInt32(commd.Parameters[4].Value); NumHousesComple = Convert.ToInt32(commd.Parameters[5].Value); NumHousesNotComple = Convert.ToInt32(commd.Parameters[6].Value); HCTO = Convert.ToInt32(commd.Parameters[7].Value); HCTON = Convert.ToInt32(commd.Parameters[8].Value); } conn.Close(); } } #endregion
AddBy 2020-2-24
存儲函數的使用:
create function ControlLessHundred(@Num int) returns int as begin declare @ReNumber int; if(@Num>100) begin set @ReNumber=100 end else begin set @ReNumber=@Num end return @ReNumber end
查詢方式:
對於無參數的存戶函數在sqlserver中查詢只需要: exec ControlLessHundred() 對於有參數的查詢 select ControlLessHundred(100) 關於在存儲過程中的引用 ControlLessHundred(100) 如果存在'ControlLessHundred' 不是可以識別的 內置函數名稱 的報錯,那么請在前面加上dbo. 即: select dbo.ControlLessHundred(50)
EndBy 2020-2-24