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 associate 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 theme just for you Request Now Toward the finish of this instructional exercise you will get familiar with the essential of database programming. I might want, in any case, to accentuate particularly for amateurs that single direction to get the hang of writing computer programs is to realize how to troubleshoot a program and dedicate a portion of your opportunity to perusing. Donââ¬â¢t be alarmed 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 subject, at that point donââ¬â¢t trouble to examine this once more. Chapter by chapter guide 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 point 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. A large portion of the instructional exercise is utilizing simplified highlights of vb. net manager. All things considered, this is alright as a rule however imagine a scenario in which youââ¬â¢d like to control the information by code. Along these lines, I made this instructional exercise with the goal that novice developer will gain from this. Letââ¬â¢s begin It is significant that you utilize your presence of mind to comprehend the rationale of database programming. Thereââ¬â¢s a ton of highlights worked in to Visual Basic Editor that most software engineer particularly tenderfoot who neglect it. One of the most loved instruments I typically utilized is theà DEBUGGER. On the off chance that you just knew how significant a debugger is, at that point you don't have to contemplate this instructional exercise. Why? Since you can hop immediately to the source code and begin terminating the F8 order from your console and dissect each line as you step through the code. At any rate fledgling is an amateur. 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 supervisor by double tapping the ââ¬Å"HowtoAddUpdateDeleteRecord. slnâ⬠. In the event that you need to comprehend 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 technique that runs when you hit the F5 Key from your console. On the off chance 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 instrument. Thatââ¬â¢s why Visual Basic is being named as Rapid Application Development or RAD. In the event 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 technique to note here. Another significant investigating instrument is ââ¬Å"Toggle Breakpointâ⬠. You will be provoked 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 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 interface 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 records. Be certain you introduced the MSDN. Since you have effectively open the association with your database this is presently 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 troubleshooting instrument (either F8 or F9). The remainder of the method 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 strategy appears convey a similar idea, by opening a structure, with the exception of they shift on the catch conjure 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 technique calledbtnSave_Click. This system is terminated when the Save button is clicked. Privateà Subà btnSave_Click(ByValà senderà Asà System. Object,à ByValà eà Asà Sy stem. EventArgs)à 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. Columns. Add(newRow) Endà If Withâ dt .Rows(0)(ââ¬Å"CustomerIDâ⬠) = txtCustomerID. Content à â â?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.