/// <summary>
 /// Retrieves parameter information from the stored procedure specified in the VistaDBCommand and populates the Parameters collection of the specified VistaDBCommand object. Now do nothing.
 /// </summary>
 /// <param name="command">The VistaDBCommand referencing the stored procedure from which the parameter information is to be derived. The derived parameters are added to the Parameters collection of the VistaDBCommand.</param>
 public static void DeriveParameters(VistaDBCommand command)
 {
     if (command.parameters.Count != 0)
     {
         command.parameters = new VistaDBParameterCollection();
     }
 }
 /// <summary>
 /// Overloaded. Releases the resources used by the component.
 /// </summary>
 /// <param name="disposing"></param>
 protected override void Dispose(bool disposing)
 {
     selectCommand = null;
     insertCommand = null;
     updateCommand = null;
     deleteCommand = null;
 }
        private void UnregisterDataAdapter()
        {
            dataAdapter.RowUpdating -= new VistaDBRowUpdatingEventHandler(OnRowUpdating);

            dataAdapter   = null;
            insertCommand = null;
            updateCommand = null;
            deleteCommand = null;
        }
Exemple #4
0
        /// <summary>
        /// Creates and returns a VistaDBCommand object associated with the VistaDBConnection.
        /// </summary>
        public VistaDBCommand CreateCommand()
        {
            //' Return a new instance of a command object.
            VistaDBCommand comm = new VistaDBCommand();

            comm.Connection = this;

            return(comm);
        }
        /// <summary>
        /// Overloaded constructor.
        /// </summary>
        public VistaDBCommandBuilder(VistaDBDataAdapter adapter)
        {
            VistaDBErrorMsgs.SetErrorFunc();

            dataAdapter   = null;
            insertCommand = null;
            updateCommand = null;
            deleteCommand = null;

            RegisterDataAdapter(adapter);
        }
        /// <summary>
        /// Overloaded constructor.
        /// </summary>
        public VistaDBCommandBuilder()
        {
            VistaDBErrorMsgs.SetErrorFunc();

            //'This call is required by the Component Designer.
            InitializeComponent();

            dataAdapter   = null;
            insertCommand = null;
            updateCommand = null;
            deleteCommand = null;
        }
        private void RetrieveParameterValues(VistaDBCommand command, DataRow row)
        {
            object o;

            foreach (VistaDBParameter parameter1 in command.Parameters)
            {
                o = row[parameter1.SourceColumn, parameter1.SourceVersion];
                if (o is System.DBNull)
                {
                    parameter1.Value = null;
                }
                else
                {
                    parameter1.Value = o;
                }
            }
        }
        private void RefreshSchema()
        {
            string            commandText, tableName;
            int               pos, i, k, textLen;
            string            text, primaryKey, s, s2;
            VistaDBType       fieldType;
            VistaDBParameter  parameter;
            VistaDBDataReader reader;
            bool              needClose;
            string            columnName;

            if (dataAdapter.SelectCommand == null)
            {
                return;
            }

            //'Getting table name
            commandText = dataAdapter.SelectCommand.CommandText.ToUpper();

            tableName = "";
            pos       = commandText.IndexOf("FROM");
            if (pos > 0)
            {
                pos    += 5;
                textLen = commandText.Length;

                for (i = pos; i < textLen; i++)
                {
                    if (commandText[i] != ' ')
                    {
                        for (k = i; k < textLen; k++)
                        {
                            if (commandText[k] == ' ' || commandText[k] == ';')
                            {
                                break;
                            }
                        }

                        tableName = commandText.Substring(i, k - i);
                        break;
                    }
                }

                if (tableName == "")
                {
                    return;
                }
            }
            else
            {
                return;
            }

            //Create data reader
            if (dataAdapter.SelectCommand.Connection.State != ConnectionState.Open)
            {
                needClose = true;
                dataAdapter.SelectCommand.Connection.Open();
            }
            else
            {
                needClose = false;
            }

            try
            {
                reader = dataAdapter.SelectCommand.ExecuteReader(CommandBehavior.KeyInfo);

                try
                {
                    //Creating where expression
                    primaryKey = "";
                    for (i = 0; i < reader.Columns.Count; i++)
                    {
                        if (reader.Columns[i].Unique)
                        {
                            if (primaryKey != "")
                            {
                                primaryKey += " and ";
                            }

                            columnName = reader.Columns[i].Name;
                            if (reader.Columns[i].ReservedWord)
                            {
                                columnName = "[" + columnName + "]";
                            }

                            primaryKey += columnName + " = @" + reader.Columns[i].Name;
                        }
                    }

                    if (primaryKey == "")
                    {
                        throw new VistaDBException(VistaDBErrorCodes.TableDoesNotHaveUniqueColumn);
                    }

                    //Creation of VistaDBCommand's

                    ///////////////////////////////////////////////////////
                    ///////////////////////Update Command//////////////////
                    ///////////////////////////////////////////////////////
                    text = "update " + tableName + " set ";

                    //Create set expression
                    s = "";

                    for (i = 0; i < reader.Columns.Count; i++)
                    {
                        fieldType = reader.Columns[i].VistaDBType;

                        if (!reader.Columns[i].Unique && !reader.Columns[i].Identity)
                        {
                            columnName = reader.Columns[i].Name;
                            if (reader.Columns[i].ReservedWord)
                            {
                                columnName = "[" + columnName + "]";
                            }

                            text = text + s + columnName + " = @" + reader.Columns[i].Name;
                            s    = ",";
                        }
                    }

                    //Create where expression
                    text = text + " where " + primaryKey;

                    updateCommand                = new VistaDBCommand(text);
                    updateCommand.Connection     = dataAdapter.SelectCommand.Connection;
                    updateCommand.CommandTimeout = dataAdapter.SelectCommand.CommandTimeout;
                    updateCommand.Transaction    = dataAdapter.SelectCommand.Transaction;

                    //Create parameters
                    for (i = 0; i < reader.Columns.Count; i++)
                    {
                        fieldType = reader.Columns[i].VistaDBType;

                        parameter = new VistaDBParameter("@" + reader.Columns[i].Name, fieldType, reader.Columns[i].Name);
                        parameter.SourceVersion = DataRowVersion.Current;
                        updateCommand.Parameters.Add(parameter);
                    }

                    /////////////////////////////////////////////////////////
                    //////////////////////Insert(Command)////////////////////
                    /////////////////////////////////////////////////////////
                    text = "insert into " + tableName + "(";

                    //Create into and values expression
                    s  = "";
                    s2 = " VALUES (";

                    for (i = 0; i < reader.Columns.Count; i++)
                    {
                        fieldType = reader.Columns[i].VistaDBType;

                        if (!reader.Columns[i].Identity)
                        {
                            columnName = reader.Columns[i].Name;
                            if (reader.Columns[i].ReservedWord)
                            {
                                columnName = "[" + columnName + "]";
                            }

                            text = text + s + columnName;
                            s2   = s2 + s + " @" + reader.Columns[i].Name;
                            s    = ",";
                        }
                    }

                    text = text + ")" + s2 + ")";

                    insertCommand = new VistaDBCommand(text,
                                                       (VistaDBConnection)(dataAdapter.SelectCommand.Connection));
                    insertCommand.Connection     = dataAdapter.SelectCommand.Connection;
                    insertCommand.CommandTimeout = dataAdapter.SelectCommand.CommandTimeout;
                    insertCommand.Transaction    = dataAdapter.SelectCommand.Transaction;

                    //'CREATE(PARAMETERS)
                    for (i = 0; i < reader.Columns.Count; i++)
                    {
                        fieldType = reader.Columns[i].VistaDBType;

                        if (!reader.Columns[i].Identity)
                        {
                            parameter = new VistaDBParameter("@" + reader.Columns[i].Name, fieldType, reader.Columns[i].Name);
                            parameter.SourceVersion = DataRowVersion.Current;
                            insertCommand.Parameters.Add(parameter);
                        }
                    }

                    // Delete Command
                    text          = "delete from " + tableName + " where " + primaryKey;
                    deleteCommand = new VistaDBCommand(text);

                    // Create parameters
                    for (i = 0; i < reader.Columns.Count; i++)
                    {
                        if (reader.Columns[i].Unique)
                        {
                            fieldType = reader.Columns[i].VistaDBType;
                            parameter = new VistaDBParameter("@" + reader.Columns[i].Name, fieldType, reader.Columns[i].Name);
                            parameter.SourceVersion = DataRowVersion.Original;
                            deleteCommand.Parameters.Add(parameter);
                        }
                    }

                    deleteCommand.Connection     = dataAdapter.SelectCommand.Connection;
                    deleteCommand.CommandTimeout = dataAdapter.SelectCommand.CommandTimeout;
                    deleteCommand.Transaction    = dataAdapter.SelectCommand.Transaction;
                }
                finally
                {
                    reader.Close();
                }
            }
            finally
            {
                if (needClose)
                {
                    dataAdapter.SelectCommand.Connection.Close();
                }
            }
        }
		/// <summary>
		/// Creates and returns a VistaDBCommand object associated with the VistaDBConnection.
		/// </summary>
		public VistaDBCommand CreateCommand()
		{
			//' Return a new instance of a command object.
			VistaDBCommand comm = new VistaDBCommand();
			comm.Connection     = this;

			return comm;
		}
		/// <summary>
		/// Retrieves parameter information from the stored procedure specified in the VistaDBCommand and populates the Parameters collection of the specified VistaDBCommand object. Now do nothing.
		/// </summary>
		/// <param name="command">The VistaDBCommand referencing the stored procedure from which the parameter information is to be derived. The derived parameters are added to the Parameters collection of the VistaDBCommand.</param>
		public static void DeriveParameters(VistaDBCommand command)
		{
			if( command.parameters.Count != 0 )
				command.parameters = new VistaDBParameterCollection();
		}
        /// <summary>
        /// Overloaded constructor.
        /// </summary>
        /// <param name="commText">V-SQL query text</param>
        /// <param name="connString">Connectionstring to a VistaDB database</param>
        public VistaDBDataAdapter(string commText, string connString)
        {
            VistaDBConnection conn = new VistaDBConnection(connString);

            selectCommand = new VistaDBCommand(commText, conn);
        }
		private void RetrieveParameterValues(VistaDBCommand command, DataRow row)
		{
			object o;

			foreach(VistaDBParameter parameter1 in command.Parameters)
			{
				o = row[parameter1.SourceColumn, parameter1.SourceVersion];
				if(o is System.DBNull)
				{
					parameter1.Value = null;
				}
				else
					parameter1.Value = o;
			}

		}
		private void UnregisterDataAdapter()
		{
			dataAdapter.RowUpdating -= new VistaDBRowUpdatingEventHandler(OnRowUpdating);

			dataAdapter = null;
			insertCommand = null;
			updateCommand = null;
			deleteCommand = null;
		}
		private void RefreshSchema()
		{
			string commandText, tableName;
			int pos, i, k, textLen;
			string text, primaryKey, s, s2;
			VistaDBType fieldType;
			VistaDBParameter parameter;
			VistaDBDataReader reader;
			bool needClose;
			string columnName;

			if (dataAdapter.SelectCommand == null)
				return;

			//'Getting table name
			commandText = dataAdapter.SelectCommand.CommandText.ToUpper();

			tableName = "";
			pos = commandText.IndexOf("FROM");
			if (pos > 0)
			{
				pos += 5;
				textLen = commandText.Length;

				for(i = pos; i < textLen; i++)
				{
					if(commandText[i] != ' ')
					{
						for(k = i; k < textLen; k++)
						{
							if(commandText[k] == ' ' || commandText[k] == ';')
								break;
						}

						tableName = commandText.Substring(i, k - i);
						break;
					}
				}

				if(tableName == "")
					return;
			}
			else
				return;
			
			//Create data reader
			if( dataAdapter.SelectCommand.Connection.State != ConnectionState.Open )
			{
				needClose = true;
				dataAdapter.SelectCommand.Connection.Open();
			}
			else
				needClose = false;

			try
			{
				reader = dataAdapter.SelectCommand.ExecuteReader(CommandBehavior.KeyInfo);

				try
				{
					//Creating where expression
					primaryKey = "";
					for(i = 0; i < reader.Columns.Count; i++)
					{
						if( reader.Columns[i].Unique )
						{
							if(primaryKey != "")
								primaryKey += " and ";

							columnName = reader.Columns[i].Name;
							if(reader.Columns[i].ReservedWord)
								columnName = "[" + columnName + "]";

							primaryKey += columnName + " = @" + reader.Columns[i].Name;
						}
					}

					if(primaryKey == "")
					{
						throw new VistaDBException(VistaDBErrorCodes.TableDoesNotHaveUniqueColumn);
					}

					//Creation of VistaDBCommand's

					///////////////////////////////////////////////////////
					///////////////////////Update Command//////////////////
					///////////////////////////////////////////////////////
					text = "update " + tableName + " set ";

					//Create set expression
					s = "";

					for(i = 0; i < reader.Columns.Count; i++)
					{
						fieldType = reader.Columns[i].VistaDBType;

						if(!reader.Columns[i].Unique && !reader.Columns[i].Identity)
						{
							columnName = reader.Columns[i].Name;
							if(reader.Columns[i].ReservedWord)
								columnName = "[" + columnName + "]";

							text = text + s + columnName + " = @" + reader.Columns[i].Name;
							s = ",";
						}
					}

					//Create where expression
					text = text + " where " + primaryKey;

					updateCommand = new VistaDBCommand(text);
					updateCommand.Connection = dataAdapter.SelectCommand.Connection;
					updateCommand.CommandTimeout = dataAdapter.SelectCommand.CommandTimeout;
					updateCommand.Transaction = dataAdapter.SelectCommand.Transaction;

					//Create parameters
					for(i = 0; i < reader.Columns.Count; i++)
					{
						fieldType = reader.Columns[i].VistaDBType;

						parameter = new VistaDBParameter("@" + reader.Columns[i].Name, fieldType, reader.Columns[i].Name);
						parameter.SourceVersion = DataRowVersion.Current;
						updateCommand.Parameters.Add(parameter);
					}

					/////////////////////////////////////////////////////////
					//////////////////////Insert(Command)////////////////////
					/////////////////////////////////////////////////////////
					text = "insert into " + tableName + "(";

					//Create into and values expression
					s = "";
					s2 = " VALUES (";

					for(i = 0; i < reader.Columns.Count; i++)
					{
						fieldType = reader.Columns[i].VistaDBType;

						if (!reader.Columns[i].Identity)
						{
							columnName = reader.Columns[i].Name;
							if(reader.Columns[i].ReservedWord)
								columnName = "[" + columnName + "]";

							text = text + s + columnName;
							s2 = s2 + s + " @" + reader.Columns[i].Name;
							s = ",";
						}
					}

					text = text + ")" + s2 + ")";

					insertCommand = new VistaDBCommand(text,
						(VistaDBConnection)(dataAdapter.SelectCommand.Connection));
					insertCommand.Connection = dataAdapter.SelectCommand.Connection;
					insertCommand.CommandTimeout = dataAdapter.SelectCommand.CommandTimeout;
					insertCommand.Transaction = dataAdapter.SelectCommand.Transaction;

					//'CREATE(PARAMETERS)
					for(i = 0; i < reader.Columns.Count; i++)
					{
						fieldType = reader.Columns[i].VistaDBType;

						if(!reader.Columns[i].Identity)
						{
							parameter = new VistaDBParameter("@" + reader.Columns[i].Name, fieldType, reader.Columns[i].Name);
							parameter.SourceVersion = DataRowVersion.Current;
							insertCommand.Parameters.Add(parameter);
						}
					}

					// Delete Command
					text = "delete from " + tableName + " where " + primaryKey;
					deleteCommand = new VistaDBCommand(text);

					// Create parameters
					for(i = 0; i < reader.Columns.Count; i++)
					{
						if( reader.Columns[i].Unique )
						{
							fieldType = reader.Columns[i].VistaDBType;
							parameter = new VistaDBParameter("@" + reader.Columns[i].Name, fieldType, reader.Columns[i].Name);
							parameter.SourceVersion = DataRowVersion.Original;
							deleteCommand.Parameters.Add(parameter);
						}
					}

					deleteCommand.Connection = dataAdapter.SelectCommand.Connection;
					deleteCommand.CommandTimeout = dataAdapter.SelectCommand.CommandTimeout;
					deleteCommand.Transaction = dataAdapter.SelectCommand.Transaction;
				}
				finally
				{
					reader.Close();
				}
			}
			finally
			{
				if( needClose )
					dataAdapter.SelectCommand.Connection.Close();
			}
		}
		/// <summary>
		/// Overloaded constructor.
		/// </summary>
		/// <param name="commText">V-SQL query text</param>
		/// <param name="connString">Connectionstring to a VistaDB database</param>
		public VistaDBDataAdapter( string commText, string connString )
		{
			VistaDBConnection conn = new VistaDBConnection(connString);
			selectCommand = new VistaDBCommand(commText, conn);
		}
		/// <summary>
		/// Overloaded constructor.
		/// </summary>
		/// <param name="commText">V-SQL query text</param>
		/// <param name="conn">VistaDBConnection object</param>
		public VistaDBDataAdapter( string commText, VistaDBConnection conn)
		{
			selectCommand = new VistaDBCommand(commText, conn);
		}
		/// <summary>
		/// Overloaded constructor.
		/// </summary>
		/// <param name="comm">VistaDBCommand select command object</param>
		public VistaDBDataAdapter( VistaDBCommand comm )
		{
			selectCommand = comm;
		}
		/// <summary>
		/// Overloaded. Releases the resources used by the component.
		/// </summary>
		/// <param name="disposing"></param>
		protected override void Dispose(bool disposing)
		{
			selectCommand = null;
			insertCommand = null;
			updateCommand = null;
			deleteCommand = null;
		}
 /// <summary>
 /// Overloaded constructor.
 /// </summary>
 /// <param name="comm">VistaDBCommand select command object</param>
 public VistaDBDataAdapter(VistaDBCommand comm)
 {
     selectCommand = comm;
 }
		/// <summary>
		/// Overloaded constructor.
		/// </summary>
		public VistaDBCommandBuilder()
		{
			VistaDBErrorMsgs.SetErrorFunc();

			//'This call is required by the Component Designer.
			InitializeComponent();

			dataAdapter = null;
			insertCommand = null;
			updateCommand = null;
			deleteCommand = null;
		}
		/// <summary>
		/// Overloaded constructor.
		/// </summary>
		public VistaDBCommandBuilder(VistaDBDataAdapter adapter)
		{
			VistaDBErrorMsgs.SetErrorFunc();

			dataAdapter = null;
			insertCommand = null;
			updateCommand = null;
			deleteCommand = null;

			RegisterDataAdapter(adapter);
		}
 /// <summary>
 /// Overloaded constructor.
 /// </summary>
 /// <param name="commText">V-SQL query text</param>
 /// <param name="conn">VistaDBConnection object</param>
 public VistaDBDataAdapter(string commText, VistaDBConnection conn)
 {
     selectCommand = new VistaDBCommand(commText, conn);
 }