Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
rtutus
Aged Yak Warrior
522 Posts |
Posted - 2006-06-28 : 13:59:34
|
This is the code in MSDN article: Dynamically Bind Your Data Layer to Stored Procedures and SQL Commands Using .NET Metadata and Reflection: http://msdn.microsoft.com/msdnmag/issues/02/08/NETReflection/My 3 questions pls are:1/ What classes out of these 4 classes do I need to implement creating paameters and command automatically2/ What doesmoduleId in the last class refer to. When I cal the last class, what do I put in the in the moduleId param3/ where is the name of the stored proc in al this scenario if my stored proc s "MyStoredProc" where do I put this name.Here is the code:using System;using System.Reflection;using System.Data;using System.Data.SqlClient;sealed class PortalDatabase{ public static DataSet GetAnnouncements( SqlConnection connection, int moduleId) { return null; }}sealed class ReflectionSample{ static void Main() { MethodInfo[] methods = typeof(PortalDatabase).GetMethods( BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (MethodInfo method in methods) { Console.WriteLine("{0}.{1} [{2}]", method.DeclaringType.Name, method.Name, method.ReturnType); foreach (ParameterInfo paramInfo in method.GetParameters()) { Console.WriteLine("\t{0} {1}", paramInfo.ParameterType, paramInfo.Name); } } }}Figure 5 Generating a SqlCommand from a Methodsealed class SqlCommandGenerator{ private SqlCommandGenerator() {} public static SqlCommand GenerateCommand(SqlConnection connection, MethodInfo method, object[] values) { SqlCommand command = new SqlCommand(method.Name, connection); command.CommandType = CommandType.StoredProcedure; ParameterInfo[] parameters = method.GetParameters(); for (int i = 1; i < parameters.Length; i++) { SqlParameter sqlParameter = new SqlParameter(); sqlParameter.ParameterName = "@" + parametersIdea.Name; sqlParameter.Value = valuesIdea; command.Parameters.Add(sqlParameter); } return command; }}public static DataSet GetAnnouncements(SqlConnection connection, int moduleId){ MethodInfo methodInfo = typeof(PortalDatabase).GetMethod("GetAnnouncements", new Type[] { typeof(SqlConnection), typeof(int) }); SqlCommand command = SqlCommandGenerator.GenerateCommand(connection, methodInfo, new object[] { moduleId }); DataSet dataSet = new DataSet(); SqlDataAdapter dataAdapter = new SqlDataAdapter(command); dataAdapter.Fill(dataSet); return dataSet;}Thanks a lot for your explanantions |
|
|
|
|
|
|