I am a Delphi developer. For Delphi, I am almost mastering every part of it. However, for C #, I don't know anything. However, I understand that in the future, C # will be very important. So, this is my first time I tried to use C # and try to learn from it through constant learning. I hope you can also master it.
I am using the Borland C # Builder Personal Edition. I have designed a simple EMP table using the local database file format Paradox. Regarding how to design a parallel knowledge, I don't plan to explain, I hope you have learned these knowledge before.
After designing the form, open C # Builder, click File - New - C # Application, and then give your project and specify the storage path.
You will get the following results after completion:
If you haven't installed an ODBC component, click Component - Installed .Net Components, the following form will be opened to ensure that the ODBC component is selected.
If you haven't changed the layout of C # Builder, you will see the toolbox in the bottom right of the screen. In the editing box below the Toolbox, when you enter the name of the component, C # Builder will automatically filter out these components. At first I didn't like this feature too much, but it looks good. Especially in Delphi has nearly 30 or more component pages, using this feature in Delphi will greatly accelerate the progress of development. I think, if Delphi implements this feature, I will definitely update my Delphi.
Next, click ODBC Connection (ODBCConnection), then click Form, a zone containing the ODBCConnection component appears below Form. As far as I guess, all non-visual components are placed in this area. Putting down the selected component will clear the lookup text I just entered (this doesn't seem to be very good!), So I have to find ODBC again, then click ODBCCommand, and place it to the area you have just appeared. When this area appears, you can place the component in this area like placing the components in the Form.
I hope you can like these pictures, I will still put more ... J
Click ODBCCONNECTION1 and select Connectionstring. You must have guessed, the connection string will place it. If you use BDE and directly access components, you have hardly use odbc (good, I like a little bit); however, this makes I can't determine what these connection strings are sides. Although ADO and ODBC connect strings are similar, they are not exactly consistent. There is a good reference for the connection string: http://www.connectionstrings.com/.
Now I need to use Paradox! Ok, we create a DSN in the ODBC Data Source Manager, about how I don't want to explain. I named my DSN "Article". Then, the connection string will be like this: "DSN = Article; UID = UserName; PWD =;".
Now we turn to GUI design. I hope that my program is moving to adjust the size, so I placed a panel and a listbox on the form. If you don't like to find them by entering text, you can find them in Windows Form. Finally, add a button.
In C #, you can use a "Dock" property, corresponding to the positioning (Align) attribute in Delphi. This is a quite rod, and each of my programs will be used. For Panel, I chose Upper (Altop, which is equivalent to delphi), put it on the top of Form; for ListBox, I chose Center (equivalent to the AlClient in Delphi) to occupy all the remaining parts of Form. Below is the process of adjusting the GUI:
Now click ODBCCommand1, double-click the Connect in the object viewer, and ODBCConnection1 will be automatically selected, and then click CommandText, enter "Select * from EMP".
Now we should add some code. Double click on Button in Form. Small knowledge: As in Delphi, you can switch between code and forms using F12.
In your code, we need to tell the unit that we have used some ODBC programs, so we need to add "Using System.Data.odbc under other USING statements.". It should be noted that C # is sensitive.
The built-in code will help you very much. Keep in mind that even if some processes don't need any parameters, it is also necessary to add () when calling them. And for strings, you should use "instead of". How should our code written? Ok, let's remove the characters in ListBox, open the database connection, then we can Use a Reader to read the data from the data table. I must admit that I know too much for Reader, but I believe I will understand Reader immediately. I'm going to go to ..., good, let us read these data and make them The format is added to ListBox, you can refer to the following code:
In the above code, you may have discovered that there will be some comments, they come from one of the routines in the help. Know how to get help will help you successfully master C #. So how do you do it? Ok, select those items you don't understand, then press F1. For example, I selected the component odbccommand1 and presses F1, and the following form will appear:
Choose an ODBCCommand class, then you will see all you want to know: information about this class and its properties and methods. Soon, I found Reader is what I need for my program.
Then click F9 to run this program, then press Button, you will get the following results:
I hope that you can read this article, just like the mood of this article. There is no doubt that I will continue to use C #. This will be a snatch for a small dish for programmers who learn C # from other languages. If you already know how to use help, then you have mastered the most important knowledge. I will be happy to hear the comments on this article, no matter whether it is good, I hope to know. If there is any comment on this article, you can send it to Fredrik.larsson@larsson.tc.
Translation: The source code of this article can be obtained from http://www.larsson.tc/develop/csharp/simpledbapp/simpledbapp.zip.