呼叫儲存過程
阿新 • • 發佈:2021-12-10
1. 返回單一記錄集的儲存過程
儲存過程Categoriestest1
CREATE PROCEDURE Categoriestest1 AS select * from Categories GO
SqlConnection conn = new SqlConnection(ConStr); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "Categoriestest1"; // 指定執行語句為儲存過程 cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter dp = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); dp.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind();
2. 沒有輸入輸出的儲存過程
儲存過程Categoriestest2
CREATE PROCEDURE Categoriestest2 AS insert into dbo.Categories (CategoryName,[Description],[Picture]) values ('test1','test1',null) GO
SqlConnection conn = new SqlConnection(ConStr); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn ; cmd.CommandText = "Categoriestest2"; cmd.CommandType = CommandType.StoredProcedure; conn.Open(); Label1.Text = cmd.ExecuteNonQuery().ToString(); conn.Close();
3. 有返回值的儲存過程
儲存過程 Categoriestest3
CREATE PROCEDURE Categoriestest3 AS insert into dbo.Categories (CategoryName,[Description],[Picture]) values ('test1','test1',null) return @@rowcount GO
SqlConnection conn = new SqlConnection(ConStr); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "Categoriestest3"; cmd.CommandType = CommandType.StoredProcedure; // 建立引數 IDataParameter[] parameters = { new SqlParameter("rval", SqlDbType.Int,4) }; // 將引數型別設定為 返回值型別 parameters[0].Direction = ParameterDirection.ReturnValue; // 新增引數 cmd.Parameters.Add(parameters[0]); conn.Open(); // 執行儲存過程並返回影響的行數 Label1.Text = cmd.ExecuteNonQuery().ToString(); conn.Close(); // 顯示影響的行數和返回值 Label1.Text += "-" + parameters[0].Value.ToString();
4. 有輸入引數和輸出引數的儲存過程
儲存過程Categoriestest4
CREATE PROCEDURE Categoriestest4 @id int output, @CategoryName nvarchar(15) AS insert into dbo.Categories (CategoryName,[Description],[Picture]) values (@CategoryName,'test1',null) set @id = @@IDENTITY GO
SqlConnection conn = new SqlConnection(ConStr); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "Categoriestest4"; cmd.CommandType = CommandType.StoredProcedure; // 建立引數 IDataParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Int,4) , new SqlParameter("@CategoryName", SqlDbType.NVarChar,15) , }; // 設定引數型別 parameters[0].Direction = ParameterDirection.Output; // 設定為輸出引數 parameters[1].Value = "testCategoryName"; // 新增引數 cmd.Parameters.Add(parameters[0]); cmd.Parameters.Add(parameters[1]); conn.Open(); // 執行儲存過程並返回影響的行數 Label1.Text = cmd.ExecuteNonQuery().ToString(); conn.Close(); // 顯示影響的行數和輸出引數 Label1.Text += "-" + parameters[0].Value.ToString();
5. 同時具有返回值、輸入引數、輸出引數的儲存過程
儲存過程 Categoriestest5
CREATE PROCEDURE Categoriestest5 @id int output, @CategoryName nvarchar(15) AS insert into dbo.Categories (CategoryName,[Description],[Picture]) values (@CategoryName,'test1',null) set @id = @@IDENTITY return @@rowcount GO
SqlConnection conn = new SqlConnection(ConStr); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "Categoriestest5"; cmd.CommandType = CommandType.StoredProcedure; // 建立引數 IDataParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Int,4) , new SqlParameter("@CategoryName", SqlDbType.NVarChar,15) , new SqlParameter("rval", SqlDbType.Int,4) }; // 設定引數型別 parameters[0].Direction = ParameterDirection.Output; // 設定為輸出引數 parameters[1].Value = "testCategoryName"; // 給輸入引數賦值 parameters[2].Direction = ParameterDirection.ReturnValue; // 設定為返回值 // 新增引數 cmd.Parameters.Add(parameters[0]); cmd.Parameters.Add(parameters[1]); cmd.Parameters.Add(parameters[2]); conn.Open(); // 執行儲存過程並返回影響的行數 Label1.Text = cmd.ExecuteNonQuery().ToString(); conn.Close(); // 顯示影響的行數,輸出引數和返回值 Label1.Text += "-" + parameters[0].Value.ToString() + "-" + parameters[2].Value.ToString();
6. 同時返回引數和記錄集的儲存過程
儲存過程Categoriestest6
CREATE PROCEDURE Categoriestest6 @id int output, @CategoryName nvarchar(15) AS insert into dbo.Categories (CategoryName,[Description],[Picture]) values (@CategoryName,'test1',null) set @id = @@IDENTITY select * from Categories return @@rowcount GO
SqlConnection conn = new SqlConnection(ConStr); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "Categoriestest6"; cmd.CommandType = CommandType.StoredProcedure; // 建立引數 IDataParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Int,4) , new SqlParameter("@CategoryName", SqlDbType.NVarChar,15) , new SqlParameter("rval", SqlDbType.Int,4) // 返回值 }; // 設定引數型別 parameters[0].Direction = ParameterDirection.Output; // 設定為輸出引數 parameters[1].Value = "testCategoryName"; // 給輸入引數賦值 parameters[2].Direction = ParameterDirection.ReturnValue; // 設定為返回值 // 新增引數 cmd.Parameters.Add(parameters[0]); cmd.Parameters.Add(parameters[1]); cmd.Parameters.Add(parameters[2]); SqlDataAdapter dp = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); dp.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); Label1.Text = string.Empty; // 顯示輸出引數和返回值 Label1.Text += parameters[0].Value.ToString() + "-" + parameters[2].Value.ToString();
7. 返回多個記錄集的儲存過程
儲存過程Categoriestest7
CREATE PROCEDURE Categoriestest7 AS select * from Categories select * from Categories GO
SqlConnection conn = new SqlConnection(ConStr); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "Categoriestest7"; cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter dp = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); dp.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); GridView2.DataSource = ds.Tables[1]; GridView2.DataBind();