private void LoadDataSourceCommandsFromFile(string configFileName) 
		{
			Hashtable commands = new Hashtable();

			XmlTextReader xr = new XmlTextReader(configFileName);

			xr.WhitespaceHandling = WhitespaceHandling.None;
			xr.MoveToContent();

			DataCommand currentDataCommand = null;
			IDbCommand currentDbCommand = null;
			bool isCommandTextCurrentNode = false;
				
			while(xr.Read())
			{
				switch(xr.NodeType) 
				{
					case XmlNodeType.Element:
					switch(xr.Name) 
					{
						case "dataCommand":
							//instantiate the command
							string commandName = xr.GetAttribute("name");

							currentDbCommand = 
								(IDbCommand)Activator.CreateInstance(_provider.CommandObjectType);

							string strCommandType = xr.GetAttribute("type");
							CommandType commandType = 
								(CommandType)Enum.Parse(typeof(CommandType), strCommandType , true);
							currentDbCommand.CommandType = commandType;

							currentDataCommand = new DataCommand(commandName, currentDbCommand, _dataSource);
							commands.Add(commandName, currentDataCommand);
							break;

						case "commandText":
							isCommandTextCurrentNode = true;
							break;

						case "param":
							IDbDataParameter param = currentDbCommand.CreateParameter();
							param.ParameterName = xr.GetAttribute("name");
							string strParamType = xr.GetAttribute("type");
							_provider.ParameterDbTypeProperty.SetValue(
								param, Enum.Parse(_provider.ParameterDbType, strParamType, true), null);
							string size = xr.GetAttribute("size");
							if(size != null)
								param.Size = Int32.Parse(xr.GetAttribute("size"));
							param.Direction = 
								(ParameterDirection)Enum.Parse(typeof(ParameterDirection), xr.GetAttribute("direction"));
							currentDbCommand.Parameters.Add(param);
							break;
					}
						break;

					case XmlNodeType.Text:
						if(isCommandTextCurrentNode) 
						{
							currentDbCommand.CommandText = xr.Value;
						}
						break;

					case XmlNodeType.EndElement:
					switch(xr.Name) 
					{
						case "commandText":
							isCommandTextCurrentNode = false;
							break;
					}
						break;
				}
			}
			xr.Close();
			
			foreach(DictionaryEntry entry in commands)
			{
				_commands[entry.Key] = entry.Value;
			}
		}
Пример #2
0
        private DataCommand CreateCommand(CommandType commandType, string commandText)
        {
            //TODO: decide whether to delegate or do it here

            //IDbCommand dbCmd = (IDbCommand)Activator.CreateInstance(_provider.CommandObjectType);
            IDbCommand dbCmd = (IDbCommand)((ICloneable)_templateCommand).Clone();
            dbCmd.CommandText = commandText;
            dbCmd.CommandType = commandType;

            DataCommand cmd = new DataCommand("", dbCmd, this);

            return cmd;
        }