Saturday, August 22, 2020

How to Connect a Database and Add/Update/Delete/Record Free Essays

The most effective method to Connect to a Database and Add/Update/Delete Record In this instructional exercise I will disclose to you on the most proficient method to interface with an Access database and permit you to Add/Update/Deleteâ a record. To completely comprehend these instructional exercises pleaseâ downloadâ the source code How to Add/Update/Delete Record utilizing MS Access Database. This source code is a piece of the Hotel Reservation Systemâ that I am as of now working. We will compose a custom paper test on The most effective method to Connect a Database and Add/Update/Delete/Record or on the other hand any comparative subject just for you Request Now Toward the finish of this instructional exercise you will gain proficiency with the essential of database programming. I might want, be that as it may, to underscore particularly for novices that single direction to get the hang of writing computer programs is to realize how to investigate a program and give a portion of your opportunity to perusing. Don’t be terrified on how short or long an article ought to be. The significant is toward the finish of the instructional exercise you will gain some new useful knowledge! In the event that you definitely know the theme, at that point don’t trouble to examine this once more. Chapter by chapter list 1. Presentation 2. Let’s begin 3. Database Connection 4. Include and Update a Record 5. Erase a Record 6. Last Thoughts Introduction Before I began learning VB. NET one of the subject that I scan for in the web is on the most proficient method to interface with the database and roll out certain improvements to the table. Despite the fact that there’s a great deal of results, however I can't discover one that suit to my necessities. The vast majority of the instructional exercise is utilizing simplified highlights of vb. net supervisor. Indeed, this is alright by and large yet imagine a scenario in which you’d like to control the information by code. Along these lines, I made this instructional exercise so apprentice software engineer will gain from this. Let’s begin It is significant that you utilize your sound judgment to comprehend the rationale of database programming. There’s a ton of highlights worked in to Visual Basic Editor that most software engineer particularly amateur who ignore it. One of the most loved instruments I generally utilized is the DEBUGGER. In the event that you just knew how significant a debugger is, at that point you don't have to examine this instructional exercise. Why? Since you can bounce immediately to the source code and begin terminating the F8 order from your console and break down each line as you step through the code. At any rate tenderfoot is a fledgling. You have to begin without any preparation. On the off chance that you have just downloaded the source code, at that point open it in the visual fundamental . net manager by double tapping the â€Å"HowtoAddUpdateDeleteRecord. sln†. In the event that you need to realize what is the article that runs the first occasion when you start the program (by squeezing F5) at that point double tap the â€Å"My Project† at the Solution Explorer. Take a gander at the Startup Form. You will see that the worth is â€Å"frmCustomersList†. Presently, click this item in the Solution Explorer and snap the View Code at the toolbar. Search for the Load occasion comparable underneath: Private Sub frmCustomersList_Load(ByVal sender As System. Object, ByVal e As System. EventArgs)Handles MyBase. Burden  â â â â â â sSql = â€Å"SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address FROM Customers ORDER BY CustomerID ASC†         Call FillList() FillListView(lvList, GetData(sSql)) End Sub frmCustomersList_Load is the second methodology that runs when you hit the F5 Key from your console. In the event that you’d like to know how this code is executed, at that point press F8. In all honesty F8 is the response to all your programming question. Furthermore, I truly would not joke about this. At the point when I began programming everything I do is to scan with the expectation of complimentary source code and begin utilizing the investigating apparatus. That’s why Visual Basic is being named as Rapid Application Development or RAD. On the off chance that you follow the debugger the main line it executes is the Private Sub frmCustomersList_Resize(ByVal senderAs Object, ByVal e As System. EventArgs) then followed by frmCustomersList_Load which is really the significant system to note here. Another significant troubleshooting apparatus is â€Å"Toggle Breakpoint†. You will be incited to your code on the off chance that one of the line is set apart by switch break point. This should be possible by squeezing the F9 key or tapping the Debug menu at that point Toggle Breakpoint. This instrument is significant if the structure is as of now stacked and you need to tract the execution of a code say inside an order button. For instance. Open the form frmCustomersList and double tap the include catch and move the up bolt key once and press F9. You willl have an image as demonstrated as follows: [inline:Toggle Breakpoint. jpg] Presently, when you run the program and snap the Add button you will be coordinated to the code editorial manager window. This case you will perceive what's going on when you are executing the program. Isn’t it decent? Database Connection In request to associate with the database you need an association string this way: Public Const cnString As String = â€Å"Provider=Microsoft. Stream. OLEDB. 4. 0;Persist Security Info=False;Data Source=.. /information/test. mdb† Then open it by utilizing this order: Dim cnHotel As OleDbConnection cnHotel = New OleDbConnection With cnHotel Ifâ . State = ConnectionState. Open Then . Close() .ConnectionString = cnString .Open() End With You need this whether you use OleDbDataReader, ExecuteNonQuery or OleDbCommandBuilder to peruse or compose into the database table. To find out about this class simply click this order and press F1 key to open the assistance documents. Be certain you introduced the MSDN. Since you have effectively open the association with your database this is currently an opportunity to fill the ListView with information. This should be possible by calling a capacity like: FillListView(lvList, GetData(sSql)) The line of code will at that point execute a capacity: Fill ListView control with information Public Sub FillListView(ByRef lvList As ListView, ByRef myData As OleDbDataReader)         Dim itmListItem As ListViewItem Dim strValue As String Do While myData. Peruse itmListItem = New ListViewItem() strValue = IIf(myData. IsDBNull(0), â€Å"†, myData. GetValue( 0))  â â â â â â â â â â itmListItem. Content = strValue For shtCntr = 1 To myData. FieldCount() †1                 If myData. IsDBNull(shtCntr) Then  â â â â â â â â â â â â â â â â â â itmListItem. SubItems. Add(â€Å"†)                 Else itmListItem. SubItems. Add(myData. GetString(shtCntr))                 End If Next shtCntr lvList. Things. Add(itmListItem) Loop End Sub Again so as to perceive how this code is being executed simply run the program utilizing the investigating apparatus (either F8 or F9). The remainder of the technique is executed just when they are called. For instance, the code beneath is executed just when you click the Add button. Private Sub btnAdd_Click(ByVal sender As System. Object, ByVal e As System. EventArgs) HandlesbtnAdd. Snap         Dim CustomerID As String frmCustomers. State = gModule. FormState. adStateAddMode         For Each sItem As ListViewItem In lvList. SelectedItems  â â â â â â â â â â CustomerID = sItem. Content Next frmCustomers. CustomerID = CustomerID frmCustomers. ShowDialog() Call FillList() End Sub This code will open the form frmCustomers in include mode and will execute additionally its own Load Event. On the off chance that you need to open the form frmCustomers in alter mode, at that point simply double tap the thing in a ListView. The code being executed are: Private Sub lvList_DoubleClick(ByVal sender As Object, ByVal e As System. EventArgs) HandleslvList. DoubleClick         Dim CustomerID As String For Each sItem As ListViewItem In lvList. SelectedItems  â â â â â â â â â â CustomerID = sItem. Content Next With frmCustomers .State = gModule. FormState. adStateEditMode  â â â â â â â â â â . CustomerID = CustomerID .ShowDialog() Call FillList() End With frmCustomers = Nothing End Sub The two system appears convey a similar idea, by opening a structure, aside from they change on the catch summon for execution. The line frmCustomers. State = gModule. FormState. adStateAddMode will advise the objective structure to open the association with the database in include mode and frmCustomers. State = gModule. FormState. adStateEditMode sick open the database in alter mode. Include and Update a Record Now, how to spare the information in textboxes inside the structure? This should be possible by calling a system calledbtnSave_Click. This strategy is terminated when the Save button is clicked. Private Sub btnSave_Click(ByVal sender As System. Object, ByVal e As System. EventArg s) HandlesbtnSave. Snap         Dim dt As DataTable = dsCustomers. Tables(â€Å"Customers†)         If txtCustomerID. Content = â€Å"†Ã‚ Or txtCompanyName. Content = â€Å"†Ã‚ Then  â â â â â â â â â â MsgBox(â€Å"Please top off Customer ID or Company Name data. â€Å", MsgBoxStyle. Basic)             Exit Sub End If Try If State = gModule. FormState. adStateAddMode Then  â â â â â â â â â â â â â â â ‘ include a line Dim newRow As DataRow newRow = dt. NewRow() newRow(â€Å"CustomerID†) = txtCustomerID. Content  â â â â â â â â â â â â â â dt. Lines. Add(newRow) End If Withâ dt .Rows(0)(â€Å"CustomerID†) = txtCustomerID. Content  â â?

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.