Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘file’

How To Access and Modify SQL Server BLOB Data by Using the ADO Stream Object

Symptoms
The Stream object introduced in ActiveX Data Objects (ADO) 2.5 can be used to greatly simplify the code that needs to be written to access and modify Binary Large Object (BLOB) data in a SQL Server Database. The previous versions of ADO [ 2.0, 2.1, and 2.1 SP2 ] required careful usage of the GetChunk and AppendChunk methods of the Field Object to read and write BLOB data in fixed-size chunks from and to a BLOB column. An alternative to this method now exists with the advent of ADO 2.5. This article includes code samples that demonstrate how the Stream object can be used to program the following common tasks: Save the data stored in a SQL Server Image column to a file on the hard disk.Move the contents of a .gif file to an Image column in a SQL Server table.
Resolution
The following code samples are based on the data stored in the pub_info table in the SQL Server 7.0 pubs sample database. You need to modify the ADO connection string to point to your SQL Server installation. Example 1 : Saving the Data in a SQL Server Image Column to a File on the Hard Disk The code in this example opens a recordset on the pub_info table in the pubs database and saves the binary image data stored in the logo column of the first record to a file on the hard disk, as follows: Open a new Standard EXE Visual Basic project.On the Project menu, click to select References, and then set a reference to the Microsoft ActiveX Data Objects 2.5 Object Library.Place a CommandButton control on Form1.Make the following declarations in the form’s General declarations section:

Dim cn As ADODB.ConnectionDim rs As ADODB.RecordsetDim mstream As ADODB.Stream Cut and paste the following code into the Click event of the CommandButton that you added to the form:

Set cn = New ADODB.Connectioncn.Open “Provider=SQLOLEDB;data Source=<name of your SQL Server>;Initial Catalog=pubs;User Id=<Your Userid>;Password=<Your Password>”Set rs = New ADODB.Recordsetrs.Open “Select * from pub_info”, cn, adOpenKeyset, adLockOptimisticSet mstream = New ADODB.Streammstream.Type = adTypeBinarymstream.Openmstream.Write rs.Fields(“logo”).Valuemstream.SaveToFile “c:\publogo.gif”, adSaveCreateOverWriters.Closecn.Close Save and run the Visual Basic project.Click the CommandButton to save the binary data in the logo column of the first record to the file c:\publogo.gid. Look for this file in Windows Explorer and open it to view the saved image.
The code in this example declares an ADODB Stream object and sets its Type property to adTypeBinary to reflect that this object will be used to work with Binary data. Following this, the binary data stored in the logo column of the first record in the pub_info table is written out to the Stream object by calling its Write method. The Stream object now contains the binary data that is saved to the file by calling its SaveToFile method and passing in the path to the file. The adSaveCreateOverWrite constant passed in as the second parameter causes the SaveToFile method to overwrite the specified file if it already exists.Example 2 : Transfer the Image Stored in a .gif File to an Image Column in a SQL Server Table The code in this example saves an image stored in a .gif file to the logo column in the first record of the pub_info table by overwriting its current contents, as follows: Open a new Standard EXE Visual Basic project.On the Project menu, click to select References, and then set a reference to the Microsoft ActiveX Data Objects 2.5 Object Library.Place a CommandButton on Form1. Make the following declarations in the form’s General declarations section:

Dim cn As ADODB.ConnectionDim rs As ADODB.RecordsetDim mstream As ADODB.Stream Cut and paste the following code in the Click event of the CommandButton that you added to the form:

Set cn = New ADODB.Connectioncn.Open “Provider=SQLOLEDB;data Source=<name of your SQL Server>;Initial Catalog=pubs;User Id=<Your Userid>;Password=<Your Password>”Set rs = New ADODB.Recordsetrs.Open “Select * from pub_info”, cn, adOpenKeyset, adLockOptimisticSet mstream = New ADODB.Streammstream.Type = adTypeBinarymstream.Openmstream.LoadFromFile “<path to .gif file>”rs.Fields(“logo”).Value = mstream.Readrs.Updaters.Closecn.Close Save and run the Visual Basic project.Click on the CommandButton to run the code to stream the contents of the .gif file to the ADO Stream object, and save the data in the Stream to the logo column in the first record of the recordset.Verify that the image in the logo column has been modified by using the code in Example 1.

“Differences Between Visual Basic .NET and Visual C# .NET” white paper is available

Symptoms
This article points to the “Differences Between Microsoft Visual Basic .NET and Microsoft Visual C# .NET” white paper.
Resolution
Because of the previous differences between Visual Basic and C/C++, many developers assume incorrectly about the capabilities of Visual Basic .NET. Many Visual Basic developers think that Visual C# is a more powerful language than Visual Basic. In other words, Visual Basic developers assume that you can do many things in Visual C# that you cannot do in Visual Basic .NET, just as there are many things that you can do in C/C++ but cannot do in Microsoft Visual Basic 6.0 or earlier. This assumption is incorrect.
Although there are differences between Visual Basic .NET and Visual C# .NET, both are first-class programming languages that are based on the Microsoft .NET Framework, and they are equally powerful. Visual Basic .NET is a true object-oriented programming language that includes new and improved features such as inheritance, polymorphism, interfaces, and overloading. Both Visual Basic .NET and Visual C# .NET use the common language runtime. There are almost no performance issues between Visual Basic .NET and Visual C# .NET. Visual C# .NET may have a few more “power” features such as handling unmanaged code, and Visual Basic .NET may be skewed a little toward ease of use by providing features such as late binding. However, the differences between Visual Basic .NET and Visual C# .NET are very small compared to what they were in earlier versions.
The “Differences Between Microsoft Visual Basic .NET and Microsoft Visual C# .NET” white paper describes some of the differences between Visual Basic .NET and Visual C# .NET. However, remember that the .NET Framework is intended to be language independent. When you must select between Visual Basic .NET and Visual C# .NET, decide primarily based on what you already know and what you are comfortable with. It is easier for Visual Basic 6.0 developers to use Visual Basic .NET and for C++/Java programmers to use Visual C# .NET. The existing experience of a programmer far outweighs the small differences between the two languages.
No matter which language you select based on your personal preference and past experience, both languages are powerful developer tools and first-class programming languages that share the common language runtime in the .NET Framework.
The following file is available for download from the Microsoft Download Center:

Collapse this imageExpand this image
Download the “Differences between Microsoft Visual Basic .NET and Microsoft Visual C# .NET” white paper package now.(http://download.microsoft.com/download/6/3/5/6354bf47-c597-4029-89e9-2495e7539ab9/vbcsharpwp.exe)Release Date: June 18, 2002
For more information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591?(http://support.microsoft.com/kb/119591/)How to obtain Microsoft support files from online servicesMicrosoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help prevent any unauthorized changes to the file.

BUG: Visual Basic Does Not Create an EXE File After Compiling

Symptoms
An EXE file is not created even though no errors are produced aftercompiling a Visual Basic application that includes a resource file.
Resolution
This will occur when including a resource file which contains a “Version”resource.

“Differences Between Visual Basic .NET and Visual C# .NET” white paper is available

Symptoms
This article points to the “Differences Between Microsoft Visual Basic .NET and Microsoft Visual C# .NET” white paper.
Resolution
Because of the previous differences between Visual Basic and C/C++, many developers assume incorrectly about the capabilities of Visual Basic .NET. Many Visual Basic developers think that Visual C# is a more powerful language than Visual Basic. In other words, Visual Basic developers assume that you can do many things in Visual C# that you cannot do in Visual Basic .NET, just as there are many things that you can do in C/C++ but cannot do in Microsoft Visual Basic 6.0 or earlier. This assumption is incorrect.
Although there are differences between Visual Basic .NET and Visual C# .NET, both are first-class programming languages that are based on the Microsoft .NET Framework, and they are equally powerful. Visual Basic .NET is a true object-oriented programming language that includes new and improved features such as inheritance, polymorphism, interfaces, and overloading. Both Visual Basic .NET and Visual C# .NET use the common language runtime. There are almost no performance issues between Visual Basic .NET and Visual C# .NET. Visual C# .NET may have a few more “power” features such as handling unmanaged code, and Visual Basic .NET may be skewed a little toward ease of use by providing features such as late binding. However, the differences between Visual Basic .NET and Visual C# .NET are very small compared to what they were in earlier versions.
The “Differences Between Microsoft Visual Basic .NET and Microsoft Visual C# .NET” white paper describes some of the differences between Visual Basic .NET and Visual C# .NET. However, remember that the .NET Framework is intended to be language independent. When you must select between Visual Basic .NET and Visual C# .NET, decide primarily based on what you already know and what you are comfortable with. It is easier for Visual Basic 6.0 developers to use Visual Basic .NET and for C++/Java programmers to use Visual C# .NET. The existing experience of a programmer far outweighs the small differences between the two languages.
No matter which language you select based on your personal preference and past experience, both languages are powerful developer tools and first-class programming languages that share the common language runtime in the .NET Framework.
The following file is available for download from the Microsoft Download Center:

Collapse this imageExpand this image
Download the “Differences between Microsoft Visual Basic .NET and Microsoft Visual C# .NET” white paper package now.(http://download.microsoft.com/download/6/3/5/6354bf47-c597-4029-89e9-2495e7539ab9/vbcsharpwp.exe)Release Date: June 18, 2002
For more information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591?(http://support.microsoft.com/kb/119591/)How to obtain Microsoft support files from online servicesMicrosoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help prevent any unauthorized changes to the file.

PRB: Error Message “Missing or Not Registered VB6tmpl.tlb” When You Start Visual Basic 6.0

Symptoms
When you start Visual Basic 6.0 under a new user account or after a new install, and you try to run the Visual Basic 6.0 executable file (VB6.exe), you get the following error message:

Visual Basic was not able to start up due to invalid system configuration. Missing or not registered VB6tmpl.tlb.You are unable to continue and Visual Basic shuts down.
Resolution
The main Visual Basic type library is either missing or mis-registered for the current user. This message reflects the original working name of this file but in released versions of Visual Basic this file was renamed VB6.olb and is located in the same directory as the Visual Basic 6.0 executable (VB6.exe).
The registry should contain a path to this file under the following key:
HKEY_CLASSES_ROOT\TypeLib\{FCFB3D2E-A0FA-1068-A738-08002B3371B5}\6.0\9\win32If the key is missing or pointing to a location where the file cannot be found, Visual Basic returns the preceding error message during startup.

PRB: CompactDatabase Method Requires Locale to Convert 2.0 MDB

Symptoms
When attempting to use the Data Access Object (DAO) 3.5 CompactDatabasemethod to convert a Jet 2.0 .mdb file to a Jet 3.0 file format, theresulting .mdb file is still in a Jet 2.0 file format.
Resolution
DAO 3.5 requires you to provide explicitly the locale argument of theCompactDatabase when converting .mdb file formats.