http://www.developer.com/net/vb/article.php/1539781/Quick-Start-Guide-to-SQL-Server-7----Part-2.htm
Welcome to the second part of our quick start SQL Server 7 guide. Get ready to fire up that printer for yet another healthy dollop of no-nonsense, step-by-step, print-and-collect info. It's the only SQL Server guide of its kind on the Web. If you missed part one, check it out here. So what are you waiting for? Let's dive straight into the content! Once you have information in a database, you'll probably want to access it from your application. But to gain access, you'll first require a username and password login. Then you'll need to grant that login special rights to your tables and so on. We'll be covering all that in this section. To create a Login: To grant Table access to a user: You will typically assign a username and password combination for either an entire application (ie, one generic login with appropriate permissions), or on an individual user basis. Most tables in your database will typically hold at least one index. A table index is rather like the index of a book. It allows for the fast retrieval of specific information. Adding an index to a field makes SQL Server go and organise all the information within that field, sorting and making it quicker to access that rows' data in future. But unfortunately indexes in SQL Server aren't quite as simple as Access. Here, we have two different types of index: You can use both clustered and non-clustered indexes within the same table. And even though SQL Server does a lot of behind-the-scenes work to implement these, you should note that this doesn't affect the way you retrieve records via Visual Basic. It's also worth pointing out that you can involve more than one field within an index if you wish. For example, you may want to index both the Surname and Forename fields together meaning searches on both of those fields will be lightning fast. But adding too many indexes also slows down data addition, as after every new record insert, SQL Server needs to reorganise the index. So use them in moderation. It's also worth noting that, just as in Access, indexes can be unique - or not. If an index is unique, it does not allow duplicates (sometimes called duplicate keys). To create an Index: To run the Index Wizard: To manage your Indexes: If you're working on a large project and are unsure where you should add indexes, the Index Tuning Wizard can take frequently used SQL statements and decide for you. We won't be covering this feature in-depth here - for more information, check out Books Online. To run the Index Tuning Wizard: Relationships in a database allow you to define how information in one table links to information in others. This is very easy using Microsoft Access, and not that much more difficult with SQL Server, despite a few changes in terminology. Let's quickly define the two main types of relationship: You typically make these relationships work by giving each record a unique value of some sort. For example, say you run a vetinary hospital. You'd probably have a Customer table, containing your customer name and addresses, along with a unique CustomerID number. You might also have a Pets table, containing the name and breeds of each individual pet the hospital deals with. You'd probably also throw a CustomerID field into that Pets table, so you can tell which customer owns which pet. Adding relationships to your table ensures data integrity. If we didn't add any relationships to the above scenario, something might go wrong and a particular Pet record might be given an invalid CustomerID. Relationships protect that from happening. They could also stop someone accidentally deleting a Customer that has related Pet records. In this case, you might consider adding a one-to-many relationship between the main Customer table and the Pets table. In other words, the relationship is between the CustomerID field in the Customer table (the 'one' part of the relationship) and the CustomerID field in the Pets table (the 'many' part of the relationship). Then if you tried to add a pet with a CustomerID that didn't exist in the Customer table, the record would be rejected. And these same rules could also stop customers from being deleted when they still have entries in the Pets table. In brief once more; relationships help maintain data integrity. For more information about relationships, check out the coverage in part 5 of our VB Database Tutorial here. To create a relationship: Naturally, you can use the DAO and ADO controls to access your SQL Server data. But most users prefer to get at it via pure code after all, not only does it reduce the need to use a form and bound controls, it's also a lot more flexible. We're now going to look at a skeletal piece of commented ADO code for accessing your SQL Server records from within Visual Basic. Note that you must have a reference (Project, References) to the Microsoft ActiveX Data Objects Library before running this code. Most of this is pretty standard stuff. However there are a few lines of code you may wish to fine tune to suit your individual needs, particularly if you're working in a multi-user, real-time environment. Unfortunately, this is neither an ADO nor general data access tutorial - and some of the following issues are simply raised for completeness. If it's your first encounter, you may find it all a little strange. Thankfully the ADO help file can help out, with its own mini tutorial. To view it, find and run the file 'ADO210.CHM' or 'ADO200.CHM'. Alternatively, just stick with the template. CursorLocation Property First off, we have the CursorLocation property. The cursor represents where in the recordset you currently are. It's a little like Word's own flashing cursor, which shows you where you are in the current document. You can set this to either: If you're in doubt, go for adUseClient. Cursor Type The third argument in this code asks for the type of cursor you want. You can replace the current adOpenDynamic with any of the following: Lock Type That fourth argument in this code asks for a 'lock type'. The current adLockPessimistic is interchangeable with any of the following: Cache Size Another interesting recordset property is CacheSize. This determines how many rows of data are buffered on the client machine. Increasing this from the default of one allows the server to work more efficiently by returning a whole bunch of rows to the client at once, as opposed to one per shot. However, don't forget - this means the information you store locally may not always the most up-to-date from the server. So in a multi-user environment, this could lead to update conflicts. Check us out in a fortnight for the third part in our print-and-collect series. We'll be covering; views, defaults, user defined types and rules!
Quick Start Guide to SQL Server 7 -- Part 2
November 19, 2002
Private Sub Command1_Click()Dim objConn As New ADODB.ConnectionDim objRS As New ADODB.RecordsetDim objErr As ADODB.ErrorobjConn.Open _"Driver=SQL Server;Server=COLOSSI;Database=Blossom; _ User ID=KarlMoore;Password=TEST"' Open the SQL Server connectionobjConn.CursorLocation = adUseClientIf objConn.State = adStateOpen Then' If everything is OK and we have a connectionobjRS.CacheSize = 10objRS.Open "Select * from tblCustomers", objConn, _ adOpenStatic, adLockOptimistic' Open the recordset object, selecting everything' from tblCustomers - passing our objConn connectionIf Not objRS.EOF And Not objRS.BOF Then' If not at the End Of File (EOF) nor' Beginning Of File (BOF) - in other words,' if we actually have records here - then...MsgBox objRS.Fields("CustomerID")' Display one of our fields just to prove' everything went A-OKobjRS.Fields("CustomerID") = 123objRS.UpdateEnd IfobjRS.Close' Close the recordsetElse' If the connection didn't open A-OK, thenFor Each objErr In objConn.ErrorsMsgBox objErr.DescriptionNext' Display all the errors in the connection objectEnd IfSet objRS = NothingobjConn.CloseSet objConn = Nothing' Tidy up the references, etc.End SubobjConn.CursorLocation = adUseClient
objRS.Open "Select * from tblCustomers", objConn,adOpenDynamic, adLockOptimistic
objRS.Open "Select * from tblCustomers", objConn,adOpenDynamic, adLockOptimistic
objRS.CacheSize = 10