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 à â â?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.