 Hey everybody, it's Brian and welcome to the 54th QTutorial with C++ and GUI programming. I apologize I haven't posted in a while. We've had some pretty nasty ice storms here in Michigan. As a result, I had two inches of ice in my driveway, which was not fun to get out of. We just got dug out in just in time for another snowstorm. Yay, Michigan! So anyways, moving right along. Today what we're going to cover is database programming using a DSN. And I've got just a standard, you know, blank project setup here. But you notice I have a reference to SQL in our project file. And we're just going to start cranking out some code here. So let's say include, cute SQL, and let's include Qtabug. And this should all be reviewed for you if you've watched my other videos. So we'll say Q, SQL, database. And we're going to call this DB, equal, Q, SQL, database. Whoops. Help if I could type today. This is where we set the database driver. And we say add database. And I'm not going to get into drivers too much in this conversation. Because I think it's pretty in depth. But really all you need to know is your driver allows you to connect to the database. When in doubt, use QODBC because it's almost universal. And I know I'm going to get a lot of hate mail for saying that. But in my experience, it is almost universal. Unless you need a specific driver for something. All right. Now we're going to db.sethostname. And I'm using SQL Express. So we're going to just say localhost, SQL Express. Remember to put the double slashes in there. Otherwise it treats it as an escape character. And then let's say db.setdatabasename. And in our previous tutorials, we've used a connection string. And let me copy and paste that out from the other tutorial. It looks like this. Driver equals SQL native client, blah, blah, blah, blah, blah, blah, blah, blah. Basically we're not going to do that because it's ugly. And we don't like it. And there's a better way. It's called a dsn. So we're going to give it a name. And we'll say my dsn64. And then we're just going to say, you know, if db.open, then do something. And we're going to say, qdebug opened. And we're just going to db.close. Because we're not really going to do anything in this tutorial. I'm just showing you how to use a dsn connection. Say else. And I realize it's a lot of typing just to get to the heart of the matter. So bear with me here. And we're just going to print out the db last error. And that'll just tell us, you know, what happened, why we couldn't connect. Now we haven't created a dsn yet. We don't even really know what a dsn is. So we're going to go ahead and save and run. And once this compiles, what you'll get is this ugly error message that says, odbc driver manager that specified dsn contains an architecture mismatch between the driver and application unable connect. You'll get some variant. Basically, whenever you see unable connect, that means we didn't do something right here. In our case, we haven't really done anything wrong yet because we haven't done anything. But you see how it says my dsn64. That's going to take place of our connection string. It's going to tell it how to connect to the database. So what we need to do is create a dsn. To do that, you open your control panel and select, you know, large icons, administrative tools, and then data sources, odbc. And you can see how there's a user dsn system file. And here's all the drivers I have installed. We're just going to create a dsn in the system, SQL server, and we'll say my dsn64. And I'm going to show you a problem that people typically run into with 64-bit windows. I select my server, and then select your connection settings. I'm just going to leave everything as default. And then you can test the connection. You see how it says test completed successfully. So we know this dsn works. We know that we have a functional connection here, my dsn64. Let's close that. Otherwise that. So we know we have a functional connection. Let's fire this up and see what happens. It doesn't look good. We got another error message. Now, what's going on here is a bug a lot of people are probably having right now. I know I beat my head against the wall when I was working with VM the other day. But basically, I'm on 64-bit windows. I'm running a 32-bit Qt creator, which means I'm creating a 32-bit application. That being said, I need 32-bit drivers for my database. I just tried using a 64-bit. Microsoft, of course, has realized this problem. And they have included the 32-bit ODBC editor. And I'm just going to paste it in here so you can see. It's percent system drive percent. That's just an environmental variable. You could just replace this with C drive. Windows, syswow64, odbcad32.exe. That will give you the 32-bit ODBC editor. So just paste that into your run. And there is our 32-bit ODBC editor. And you notice how the DSN is missing, the one that we just created. Because this is the 32-bit, not the 64-bit. Very confusing. I know it looks the same. It acts the same, but 164-bit, once 32-bit. So bear that in mind if you're working and trying to get a DSN to work. So let's actually create one here. Notice how I have a lot more drivers in 32-bit mode. And we'll say myDSN32. And let's just give it our server name here. And then you can modify your connection settings. I'm just going to leave everything default. Test it so you know it works. Did it successfully? Alright. Now let's change this 64 to 32. That way we know we're using our myDSN32, not the 64-bit. One thing you should do and get in the habit of is don't give DSNs the same name between 32 and 64-bit. Because then you're not really sure which one you're using. Let's compile and run this. And voila, magically, database was opened. So that is how you use a DSN connection using Qt Creator. I hope you found this tutorial educational and entertaining. And thank you for watching. And if you're looking out your window and you see snow, think of me because I'm going to be out snow blowing my driveway for about three days. We're supposed to get just a ton of snow. So thank you for watching.