C# ado.net 操作存储过程(二)
调用存储过程
sql
1 IF OBJECT_ID('RegionInsert') IS NULL 2 EXEC (' 3 -- 4 -- Procedure which inserts a region record and returns the key 5 -- 6 CREATE PROCEDURE RegionInsert(@RegionDescription NCHAR(50), 7 @RegionID INTEGER OUTPUT)AS 8 SET NOCOUNT OFF; 9 10 SELECT @RegionID = MAX ( RegionID ) + 1 11 FROM Region ; 12 13 INSERT INTO Region(RegionID, RegionDescription) 14 VALUES(@RegionID, @RegionDescription);') 15 16 IF OBJECT_ID('RegionUpdate') IS NULL 17 EXEC (' 18 -- 19 -- Procedure to update the description of a region 20 -- 21 CREATE PROCEDURE RegionUpdate(@RegionID INTEGER, 22 @RegionDescription NCHAR(50))AS 23 SET NOCOUNT OFF; 24 25 UPDATE Region 26 SET RegionDescription = @RegionDescription 27 WHERE RegionID = @RegionID;') 28 29 IF OBJECT_ID('RegionDelete') IS NULL 30 EXEC (' 31 -- 32 -- Procedure to delete a region 33 -- 34 CREATE PROCEDURE RegionDelete (@RegionID INTEGER) AS 35 SET NOCOUNT OFF; 36 37 DELETE FROM Region 38 WHERE RegionID = @RegionID;');
代码
1 class Program 2 { 3 private static string constr = "server=.;database=northwnd;integrated security=sspi"; 4 static void Main(string[] args) 5 { 6 using (SqlConnection con=new SqlConnection(constr)) 7 { 8 con.Open(); 9 InitialiseDatabase(con); 10 // Generate the update command 11 SqlCommand updateCommand = GenerateUpdateCommand(con); 12 13 // Generate the delete command 14 SqlCommand deleteCommand = GenerateDeleteCommand(con); 15 16 // And the insert command 17 SqlCommand insertCommand = GenerateInsertCommand(con); 18 19 DumpRegions(con, "Regions prior to any stored procedure calls"); 20 21 insertCommand.Parameters["@RegionDescription"].Value = "South West"; 22 23 // Then execute the command 24 insertCommand.ExecuteNonQuery(); 25 26 // And then get the value returned from the stored proc 27 int newRegionID = (int)insertCommand.Parameters["@RegionID"].Value; 28 29 DumpRegions(con, "Regions after inserting 'South West'"); 30 31 updateCommand.Parameters[0].Value = newRegionID; 32 updateCommand.Parameters[1].Value = "South Western England"; 33 updateCommand.ExecuteNonQuery(); 34 35 DumpRegions(con, "Regions after updating 'South West' to 'South Western England'"); 36 37 // Delete the newly created record 38 deleteCommand.Parameters["@RegionID"].Value = newRegionID; 39 deleteCommand.ExecuteNonQuery(); 40 41 DumpRegions(con, "Regions after deleting 'South Western England'"); 42 43 con.Close(); 44 } 45 } 46 47 private static void InitialiseDatabase(SqlConnection conn) 48 { 49 SqlCommand cmd = new SqlCommand(Resource1.CreateSprocs, conn); 50 cmd.ExecuteNonQuery(); 51 } 52 53 private static SqlCommand GenerateUpdateCommand(SqlConnection con) 54 { 55 SqlCommand cmd = new SqlCommand("RegionUpdate", con); 56 57 cmd.CommandType = System.Data.CommandType.StoredProcedure; 58 cmd.Parameters.Add(new SqlParameter("@RegionID", SqlDbType.Int, 0, "RegionID")); 59 cmd.Parameters.Add(new SqlParameter("@RegionDescription", SqlDbType.NChar, 50, "RegionDescription")); 60 cmd.UpdatedRowSource = UpdateRowSource.None; 61 return cmd; 62 } 63 private static SqlCommand GenerateInsertCommand(SqlConnection conn) 64 { 65 SqlCommand aCommand = new SqlCommand("RegionInsert", conn); 66 67 aCommand.CommandType = CommandType.StoredProcedure; 68 aCommand.Parameters.Add(new SqlParameter("@RegionDescription", SqlDbType.NChar, 50, "RegionDescription")); 69 aCommand.Parameters.Add(new SqlParameter("@RegionID", SqlDbType.Int, 0, ParameterDirection.Output, 70 false, 0, 0, "RegionID", DataRowVersion.Default, null)); 71 aCommand.UpdatedRowSource = UpdateRowSource.OutputParameters; 72 73 return aCommand; 74 } 75 private static SqlCommand GenerateDeleteCommand(SqlConnection conn) 76 { 77 SqlCommand aCommand = new SqlCommand("RegionDelete", conn); 78 79 aCommand.CommandType = CommandType.StoredProcedure; 80 aCommand.Parameters.Add(new SqlParameter("@RegionID", SqlDbType.Int, 0, "RegionID")); 81 aCommand.UpdatedRowSource = UpdateRowSource.None; 82 83 return aCommand; 84 } 85 private static void DumpRegions(SqlConnection conn, string message) 86 { 87 SqlCommand aCommand = new SqlCommand("SELECT RegionID , RegionDescription From Region", conn); 88 89 // Note the use of CommandBehaviour.KeyInfo. 90 // If this is not set, the default seems to be CommandBehavior.CloseConnection, 91 // which is an odd default if there ever was one. Oh well. 92 SqlDataReader aReader = aCommand.ExecuteReader(CommandBehavior.KeyInfo); 93 94 Console.WriteLine(message); 95 96 while (aReader.Read()) 97 { 98 Console.WriteLine(" {0,-20} {1,-40}", aReader[0], aReader[1]); 99 } 100 101 aReader.Close(); 102 } 103 104 }
鹰击长空,鱼翔浅底